You need to sign in to do that
Don't have an account?
Jeremiah Cochran
0down votefavorite
I have 2 custom objects we will call ObjectOne__c and ObjectTwo__c, these 2 objects DO NOT have a direct relationship to each other. However, I have a business need that requires fields on ObjectTwo__c to be updated to match the fields on ObjectOne__c when ObjectOne__c is updated. Here is a sample of the Trigger that handles this process. My issues is I need to move the SOQL out of the loop to properly handle mass updates, but I can’t figure out how to do that and still get the trigger to properly update all the records in the trigger.
Here is a sample of the code in my trigger:
Move SOQL out of For Loop
0down votefavorite
I have 2 custom objects we will call ObjectOne__c and ObjectTwo__c, these 2 objects DO NOT have a direct relationship to each other. However, I have a business need that requires fields on ObjectTwo__c to be updated to match the fields on ObjectOne__c when ObjectOne__c is updated. Here is a sample of the Trigger that handles this process. My issues is I need to move the SOQL out of the loop to properly handle mass updates, but I can’t figure out how to do that and still get the trigger to properly update all the records in the trigger.
Here is a sample of the code in my trigger:
trigger COSyncTrigger on ObjectOne__c (after update) { //Create list to store the ObjectTwo__c records that will be Updated List < ObjectTwo__c> upObTwo = new List < ObjectTwo__c >(); //Look at records being Updated if(trigger.isUpdate){ for(ObjectOne__c ObjOne : trigger.new){ //Create a list of existing ObjectTwo__c records that match the ObjectOne__c record being updated List < ObjectTwo__c > checkupObjTwo = [Select Id from ObjectTwo__c Where (Account__c = :ObjOne.Account__c And Owner = : ObjOne.Owner And Client__c = : ObjOne. Client__c) ]; //If the Above List is NOT Empty Update the ObjectTwo__c records If(!checkupObjTwo.isEmpty()) { ObjectTwo__c upobjtwo = [Select Id from ObjectTwo__c Where (Account__c = : ObjOne.Account__c And Owner = : ObjOne.Owner And Client__c = : ObjOne. Client__c)]; upobjtwo.Client_Role__c = ObjOne. Client_Role__c; upobjtwo.Client_Status__c = ObjOne. Client_Status__c; //Add the ATM record to the list to be updated later upObTwo.add(upobjtwo); } } //If the List of records to update is NOT Empty update the ObjectTwo__c records if(upObTwo.size() > 0){ system.debug('Count of ObjectTwo__c records to update = '+ upObTwo.size()); update upObTwo; } }I have done some research and understand I need to create a list outside my loop but I can't seem to get it to update ObjectTwo__c records to Match the ObjectOne__c records in the trigger. Please explain how I can accomplish this and provide an example if possible. Thank you in advance.
A user on Salesforce Stack Exchange actually provided me with the solutions I needed. It can be viewed at the link: https://salesforce.stackexchange.com/questions/218155/remove-soql-from-for-loop
Here is the code that worked for me:
All Answers
I hope this below code should help you to avoid SOQL inside the loop. If this works or help you in any way please mark this as best answer. Thank you.
A user on Salesforce Stack Exchange actually provided me with the solutions I needed. It can be viewed at the link: https://salesforce.stackexchange.com/questions/218155/remove-soql-from-for-loop
Here is the code that worked for me:
a,b,c
p,q,r
x,y.z
As per the code now it picks all those records from Object_two__c that matches the field combination of a,q,r / a,q,z / p,b,z / p,q,z / x,b,r/.......etc..
You can try this scenrio and you will come to know what i am saying
@Gururaj B, That is correct. This code will initially pick up ALL ObjectTwo__c records with any combination of the 3 values and assign them to the MAP. As specified in this line of code:
But later we ensure that only the ObjectTwo__c records that match ObjectOne__c are actually updated by using this line of code:
This looks at the MAP and then only pulls the ObjectTwo__c record(s) that exactly match the ObjectOne__c record in the current loop