Last active
July 10, 2018 22:43
-
-
Save SalesforceBobLightning/ff1704ddc12864567a04751c5304ab0c to your computer and use it in GitHub Desktop.
InvocableMethod for updating grand child records in Salesforce Process Builder
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 class UpdateGrandChildRecordsAction { | |
| private final static String TARGET = '{0}'; | |
| private final static String REPLACEMENT = '\'\'{0}\'\''; | |
| private final static String FORMAT = 'SELECT Id, {0} FROM {1} WHERE Id = \'\'{2}\'\''; | |
| @InvocableMethod( | |
| label='Update Grand Child Records Action' | |
| description = 'Query child and grand child records and then update grand child records' | |
| ) | |
| public static void updateRecords(List<Request> requests) { | |
| for(Request request : requests){ | |
| processRequest(request); | |
| } | |
| } | |
| private static void processRequest(Request request) { | |
| String queryString = request.queryString.replace(TARGET, REPLACEMENT); | |
| queryString = String.format(queryString, new String[]{request.filterValue}); | |
| List<sObject> childRecords = Database.query(queryString); | |
| for(sObject child : childRecords){ | |
| queryString = getQueryString(request.objectName, request.fieldName, (String) child.get(request.childFilterField)); | |
| List<sObject> grandChildRecords = Database.query(queryString); | |
| if (grandChildRecords.size() > 0) { | |
| SObject grandChild = grandChildRecords[0]; | |
| grandChild.put(request.fieldName, request.fieldValue); | |
| update grandChild; | |
| } | |
| } | |
| } | |
| private static String getQueryString(String objectName, String fieldName, String recordId) { | |
| List<String> args = new String[]{fieldName, objectName, recordId}; | |
| return String.format(FORMAT, args); | |
| } | |
| public class Request { | |
| @InvocableVariable( | |
| label = 'SOQL Query' | |
| description = 'SOQL Query with {0} for the filter value' | |
| required=true | |
| ) | |
| public String queryString; | |
| @InvocableVariable( | |
| label = 'Filter Value' | |
| description = 'Value used to filter the SOQL query' | |
| required=true | |
| ) | |
| public String filterValue; | |
| @InvocableVariable( | |
| label = 'Child Filter Field' | |
| description = 'Child Field from the SOQL query to find record to be updated' | |
| required=true | |
| ) | |
| public String childFilterField; | |
| @InvocableVariable( | |
| label = 'Object Name' | |
| description = 'The name of the object with the field to be updated e.g. Account or Custom_Object__c' | |
| required=true | |
| ) | |
| public String objectName; | |
| @InvocableVariable( | |
| label = 'Field Name' | |
| description = 'Name of the field to be updated e.g. Name or Custom_Field__c' | |
| required=true | |
| ) | |
| public String fieldName; | |
| @InvocableVariable( | |
| label = 'Field Value' | |
| description = 'Field value to be stored' | |
| required=true | |
| ) | |
| public String fieldValue; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment