Created
June 27, 2018 23:12
-
-
Save kiran-machhewar/da2ddd8b2bc392f2258a4ceb7c57b21d to your computer and use it in GitHub Desktop.
LightningUtil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public without sharing class LightningUtilController { | |
| /** | |
| * Method to update sobject record | |
| * @param theSObject | |
| * @return returns the created/updated sobject record | |
| */ | |
| @AuraEnabled | |
| public static SObject upsertSObjectCtrl(SObject theSObject){ | |
| try{ | |
| upsert theSObject; | |
| return theSObject; | |
| }catch(Exception ex){ | |
| handleAuraException(ex); | |
| } | |
| return null; | |
| } | |
| /** | |
| * This method does the exception handling of aura errors. | |
| */ | |
| public static void handleAuraException(Exception ex){ | |
| AuraHandledException auraException = new AuraHandledException(ex.getMessage()); | |
| auraException.setMessage(ex.getMessage()); | |
| if(ex instanceof DmlException ){ | |
| DmlException theDmlException = (DmlException)ex; | |
| auraException = new AuraHandledException(theDmlException.getDMLMessage(0)); | |
| auraException.setMessage(theDmlException.getDMLMessage(0)); | |
| } | |
| throw auraException; | |
| } | |
| /** | |
| * This method returns the sobject records. | |
| */ | |
| @AuraEnabled | |
| public static List<SObject> runSOQLCtrlMethod(String soql){ | |
| try{ | |
| System.debug('SOQL-->'+String.escapeSingleQuotes(soql)); | |
| return Database.query(soql); | |
| }catch(Exception ex){ | |
| handleAuraException(ex); | |
| } | |
| return null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ({ | |
| upsertSObject : function(component, event,helper){ | |
| helper.upsertSObjectJS(component,event); | |
| }, | |
| doInit : function(component, event,helper){ | |
| console.log('Inside Init of LightnigUtil'); | |
| }, | |
| validateForm : function(component,event,helper){ | |
| return helper.validateFormHelper(component,event); | |
| }, | |
| validateResponse : function(component,event,helper){ | |
| return helper.validateResponse(component,event); | |
| }, | |
| runSOQL : function(component,event,helper){ | |
| return helper.runSOQL(component,event); | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ({ | |
| upsertSObjectJS : function(component, event){ | |
| var params = event.getParam('arguments'); | |
| var callback; | |
| if (params) { | |
| callback = params.callback; | |
| } | |
| var action = component.get("c.upsertSObjectCtrl"); | |
| action.setParams({ | |
| theSObject : params.theSObject | |
| }); | |
| action.setCallback(this, function(response) { | |
| if (callback) callback(response); | |
| }); | |
| $A.enqueueAction(action); | |
| }, | |
| validateFormHelper : function(component,event){ | |
| var params = event.getParam('arguments'); | |
| if(params && params.config && params.config.validationFieldAuraIds ){ | |
| let isValidationPassed = true; | |
| let passedComponent = params.config.component; | |
| params.config.validationFieldAuraIds.split(',').forEach(function(auraIdOfInputsToBeValidated){ | |
| if(passedComponent.find(auraIdOfInputsToBeValidated) && passedComponent.find(auraIdOfInputsToBeValidated).length){//if there are any records to iterate | |
| (passedComponent.find(auraIdOfInputsToBeValidated)).forEach(function(inputField){ | |
| if(inputField.get('v.required') && !inputField.get('v.value')){ | |
| inputField.showHelpMessageIfInvalid(); | |
| isValidationPassed = false; | |
| } | |
| }); | |
| }else{ | |
| var singleInputField = passedComponent.find(auraIdOfInputsToBeValidated); | |
| if(singleInputField){ | |
| if(singleInputField.get('v.required') && !singleInputField.get('v.value')){ | |
| singleInputField.showHelpMessageIfInvalid(); | |
| isValidationPassed = false; | |
| } | |
| } | |
| } | |
| }); | |
| return isValidationPassed; | |
| } | |
| }, | |
| validateResponse : function(component,event){ | |
| var params = event.getParam('arguments'); | |
| if(params | |
| && params.config.response | |
| && params.config.component ){ | |
| let isSuccessful = false; | |
| let passedComponent = params.config.component; | |
| let response = params.config.response; | |
| var toastEvent = $A.get("e.force:showToast"); | |
| if(passedComponent.isValid() && response.getState() === "SUCCESS" ){ | |
| isSuccessful = true; | |
| if(params.config.successMessage){ | |
| if(toastEvent){ | |
| toastEvent.setParams({ | |
| "title": "Success!", | |
| "type": "success", | |
| "message": params.config.successMessage | |
| }); | |
| toastEvent.fire(); | |
| } | |
| } | |
| }else{ | |
| isSuccessful = false; | |
| if(params.config.showErrorMessage){ | |
| if(toastEvent){ | |
| toastEvent.setParams({ | |
| "title": "Error!", | |
| "type": "error", | |
| "message": response.getError()[0].message | |
| }); | |
| toastEvent.fire(); | |
| } | |
| } | |
| } | |
| return isSuccessful; | |
| } | |
| }, | |
| runSOQL : function (component,event){ | |
| var params = event.getParam('arguments'); | |
| var action = component.get("c.runSOQLCtrlMethod"); | |
| if(params.callback && params.config.soql){ | |
| var callback; | |
| if (params) { | |
| callback = params.callback; | |
| } | |
| action.setParams({ | |
| soql : params.config.soql | |
| }); | |
| action.setCallback(this, function(response) { | |
| if (callback) callback(response,response.getReturnValue()); | |
| }); | |
| $A.enqueueAction(action); | |
| }else{ | |
| console.log('Required attributes are missing'); | |
| } | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <aura:component description="LightningUtil" controller="LightningUtilController"> | |
| <aura:handler name="init" action="{!c.doInit}" value="{!this}"/> | |
| <!-- Upsert record --> | |
| <aura:method name="upsertSObject" | |
| description="This method will help to upsert the SObject record"> | |
| <aura:attribute name="theSObject" type="Sobject" /> | |
| <aura:attribute name="callback" type="Function" /> | |
| </aura:method> | |
| <!-- Form validation --> | |
| <aura:method name="validateForm" | |
| description="This method will help to validate form elements"> | |
| <aura:attribute name="config" type="Object" /> | |
| </aura:method> | |
| <!-- Form validation --> | |
| <aura:method name="validateResponse" | |
| description="This method will validate response from server and determine if it was successful or not | |
| and shows the success and error messages"> | |
| <aura:attribute name="config" type="Object" /> | |
| </aura:method> | |
| <!-- run SOQL--> | |
| <aura:method name="runSOQL" | |
| description="This method will validate response from server and determine if it was successful or not | |
| and shows the success and error messages"> | |
| <aura:attribute name="config" type="Object" /> | |
| <aura:attribute name="callback" type="Function" /> | |
| </aura:method> | |
| </aura:component> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment