0% found this document useful (0 votes)
18 views

Helper

The document describes JavaScript methods used to filter records in a Lightning Web Component. It takes a filter string as input, parses it to extract field, operator, and value details, and uses that data to build a list of filter objects. It then makes Apex calls to determine the field types and populate dropdown options for the filter operators before applying the filters to query records.

Uploaded by

himanshusfdc2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Helper

The document describes JavaScript methods used to filter records in a Lightning Web Component. It takes a filter string as input, parses it to extract field, operator, and value details, and uses that data to build a list of filter objects. It then makes Apex calls to determine the field types and populate dropdown options for the filter operators before applying the filters to query records.

Uploaded by

himanshusfdc2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

({

getFilteredRecords: function(component, event, helper){


var inputString = component.get("v.addFilterForSubmission");
if (inputString !== null && inputString !== undefined && inputString.trim()
!== '') {
this.listOfFilters = [];
this.listOfFieldApi = [];
var checkInputString = inputString.trim();
if (checkInputString.toLowerCase().startsWith("and")||
checkInputString.toLowerCase().startsWith("or")) {
var validInputFormat = inputString.match(/^(\w+)\((.+)\)$/);
if (validInputFormat) {
var conditionOperator = validInputFormat[1].toUpperCase();
component.set("v.conditionOperator", conditionOperator);
if (this.checkFormat(validInputFormat[2])) {
var conditionFilter =
helper.removeCharactersAfterLastClosingBracket(component,
validInputFormat[2]).trim();
var parts = conditionFilter.split(',');
for (var i=0; i<parts.length; i++) {
var splittedval = parts[i].slice(1, -1).split(';');
var field = splittedval[0].trim();
var operator = splittedval[1].trim();
var value = splittedval[2].trim();
let filterObject = {
fieldApi: field,
operatorValue: operator,
fieldValue: value
};

this.listOfFilters.push(filterObject);
this.listOfFieldApi.push(filterObject.fieldApi);
}
component.set("v.listOfFieldApi", this.listOfFieldApi);
this.getFieldType(component, event, helper,
this.listOfFieldApi, this.listOfFilters);
} else {
helper.displayToast();
}
} else {
helper.displayToast();
}
} else if (this.validateInputFormat(inputString)) {
var splittedval = inputString.split(';');
var field = splittedval[0].trim();
var operator = splittedval[1].trim();
var value = splittedval[2].trim();
let filterObject = {
fieldApi: field,
operatorValue: operator,
fieldValue: value
};
this.listOfFilters.push(filterObject);
this.listOfFieldApi.push(filterObject.fieldApi);
component.set("v.listOfFieldApi", this.listOfFieldApi);
this.getFieldType(component, event, helper, this.listOfFieldApi,
this.listOfFilters);
} else {
helper.displayToast();
}
} else {
this.getRecordId(component, "");
}
},

getRecordId: function(component, whereClauseValue) {


let orderBy = component.get('v.orderBy');
var action = component.get("c.getRecordId");
action.setParams({
currentRecordId: component.get("v.recordId"),
orderBy : orderBy ? orderBy : 'CreatedDate',
order : orderBy ? 'Asc' : 'Desc',
whereClause : whereClauseValue
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
let submissionData = response.getReturnValue();
component.set("v.submissionRecordData", submissionData);
if (submissionData.displayOptions) {
if (submissionData.options.length > 0) {
component.set('v.disableActions' , false);
}
else {
component.set('v.disableActions' , true);
this.displayToast();
}
component.set("v.recordId", submissionData.submissionId);
}
component.set("v.isInitialized", true);
} else if (state === "ERROR") {
this.displayToast();
} else{
this.displayToast();
}
});
$A.enqueueAction(action);
},

// LWC JavaScript method to remove characters after the last closing bracket
removeCharactersAfterLastClosingBracket: function(component,inputString) {
const lastIndexClosingBracket = inputString.lastIndexOf(')');
if (lastIndexClosingBracket !== -1) {
// Remove characters after the last closing bracket
const resultString = inputString.substring(0, lastIndexClosingBracket +
1);
return resultString;
}
// If no closing bracket is found, return the original string
return inputString;
},

checkFormat: function(inputStringToValidate) {
var inputString = inputStringToValidate;
const failFormat = /\([^)]*;[^)]*;[^)]*\)\([^)]*\)/;
const acceptedFormat = /\([^)]*\),\([^)]*\)/;
if (failFormat.test(inputString)) {
return false;
} else if (acceptedFormat.test(inputString)) {
var validFormat = true;
var parts = inputString.split(',');
for (var i = 0; i < parts.length; i++) {
const regex = /\([^()]*;[^()]*;[^()]*\)/;
if (!regex.test(parts[i])) {
validFormat = false;
}
}
return validFormat;
} else {
return false;
}
},

validateInputFormat: function(inputString) {
const pattern = /^[^;]+;[^;]+;[^;]+$/;
var isValidFormat = pattern.test(inputString);
if (isValidFormat) {
return true;
} else {
return false;
}
},

getFieldType: function(component, event, helper, listOfFieldApi, listOfFilters)


{
var fieldType = component.get("c.getFieldType");
fieldType.setParams({
sObjectName: 'ampi__Submission__c',
fieldList : (component.get("v.listOfFieldApi"))
});
fieldType.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
let typeOfField = response.getReturnValue();
helper.populateFilterOperatorOptions(component, typeOfField,
listOfFilters);
} else if (state === "ERROR") {
var errors = response.getError();
this.displayToast();
} else {
this.displayToast();
}
});
$A.enqueueAction(fieldType);
},

populateFilterOperatorOptions: function(component, typeOfFieldMap,


listOfFilters) {
var particularFilterOptions = [];
for (var i = 0; i < listOfFilters.length; i++) {
var fieldApi = listOfFilters[i].fieldApi;
var typeOfField = (typeOfFieldMap[fieldApi]);
var options = [];
var opratorValue ;
options.push(this.getComboboxData('NONE', '', false));
var dataType = typeOfField.toLowerCase();
if (dataType === 'multipicklist') {
options.push(this.getComboboxData('Includes', 'INCLUDES', false));
options.push(this.getComboboxData('Excludes', 'EXCLUDES', false));
} else if (dataType === 'phone') {
options.push(this.getComboboxData('Equals', '=', false));
options.push(this.getComboboxData('Not equal to', '!=', false));
options.push(this.getComboboxData('Contains', 'LIKE', false));
options.push(this.getComboboxData('Does not contain', 'NOT LIKE',
false));
} else if (dataType === 'string' || dataType === 'picklist') {
options.push(this.getComboboxData('Equals', '=', false));
options.push(this.getComboboxData('Not equal to', '!=', false));
options.push(this.getComboboxData('Starts with', 'LIKE%', false));
options.push(this.getComboboxData('Contains', 'LIKE', false));
options.push(this.getComboboxData('Does not contain', 'NOT LIKE',
false));
} else if (
dataType === 'date' ||
dataType === 'datetime' ||
dataType === 'integer' ||
dataType === 'double' ||
dataType === 'currency' ||
dataType === 'percent'
) {
options.push(this.getComboboxData('Equals', '=', false));
options.push(this.getComboboxData('Not equal to', '!=', false));
options.push(this.getComboboxData('Less than', '<', false));
options.push(this.getComboboxData('Greater than', '>', false));
options.push(this.getComboboxData('Less or equal', '<=', false));
options.push(this.getComboboxData('Greater or equal', '>=',
false));
} else {
options.push(this.getComboboxData('Equals', '=', false));
options.push(this.getComboboxData('Not equal to', '!=', false));
}
if (options.length != 0) {
var operatorLabel = listOfFilters[i].operatorValue;
var value = listOfFilters[i].fieldValue;
if (operatorLabel && operatorLabel != "") {
for (let i = 0; i < options.length; i++) {
if (options[i].label === operatorLabel) {
opratorValue = options[i].value;
}
}
} else {
this.displayToast();
}
let objTestvalue = {
options: options,
operatorLabel: operatorLabel,
operatorValue: opratorValue,
value: value,
typeOfField: typeOfFieldMap[fieldApi],
fieldApiValue: fieldApi
}
particularFilterOptions.push(objTestvalue);
}
}
this.updatefilterCriteriaJSON(particularFilterOptions, component);
},

getComboboxData: function(label, value, isSelected) {


return {
label : label,
value : value,
isSelected : isSelected
}
},

getFilterData: function() {
return {
value : '',
operators : [],
operatorLabel : '',
operator : '',
fieldType : '',
fieldLabel : '',
field : ''
}
},

updatefilterCriteriaJSON: function(particularFilterOptions, component) {


let filters = [];
for (var i=0; i<particularFilterOptions.length; i++) {
let filter = this.getFilterData();
filter.value = particularFilterOptions[i].value;
filter.operatorLabel = particularFilterOptions[i].operatorLabel;
filter.operator = particularFilterOptions[i].operatorValue;
filter.fieldType = particularFilterOptions[i].typeOfField ?
particularFilterOptions[i].typeOfField.toUpperCase() : 'STRING';
let fieldLabel = 'STATUS';
filter.fieldLabel = filter.fieldType === 'REFERENCE'
? fieldLabel.substring(0, fieldLabel.length - 5) : fieldLabel;
filter.field = particularFilterOptions[i].fieldApiValue;
filter.operators = particularFilterOptions[i].options;;
filters.push(filter);
}
this.generateWhereClauseData(component, filters);
},

generateWhereClauseData: function(component, filters) {


var getWhereClause = component.get("c.generateWhereClause");
getWhereClause.setParams({
sourceObject: 'SUBMISSION',
filterOperator : component.get('v.conditionOperator'),
filterOptions : JSON.stringify(filters)
});
getWhereClause.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var whereClause =
this.removeCharactersAfterLastClosingBracket(component,
response.getReturnValue()).trim();
this.getRecordId(component, whereClause);
} else if (state === "ERROR") {
this.displayToast();
}
});
$A.enqueueAction(getWhereClause);
},

showConfirmation : function(component, modalId) {


var modalDiv = component.find(modalId);
$A.util.removeClass(modalDiv, 'slds-hide');
},

hideConfirmation : function(component, modalId) {


var modalDiv = component.find(modalId);
$A.util.addClass(modalDiv, 'slds-hide');
},

displayToast : function() {
$A.get("e.force:closeQuickAction").fire();
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": 'Error!',
"message": $A.get("$Label.c.SUBMISSION_FILTER_ERROR"),
"duration" : '2000',
"type": 'error'
});
toastEvent.fire();
}
})

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy