You need to sign in to do that
Don't have an account?
soql in clause with trigger.new
Hello everyone,
I recently went through a trigger which had a SOQL query like this:
Here I see that in the WHERE clause of the SOQL, user directly passed Trigger.new with IN clause. Is this the correct method? if yes, Is this method encouraged? How can you pass a list of object in the IN clause?
Till now I would have used either a list of Ids to pass in the IN clause or Trigger.newMap.Keyset().
This is also mentioned in on of the trails: https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_triggers/units/apex_triggers_bulk
I recently went through a trigger which had a SOQL query like this:
trigger SoqlTriggerBulk on Account(after update) { // Perform SOQL query once. // Get the accounts and their related opportunities. List<Account> acctsWithOpps = [SELECT Id,(SELECT Id,Name,CloseDate FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]; // Iterate over the returned accounts for(Account a : acctsWithOpps) { Opportunity[] relatedOpps = a.Opportunities; // Do some other processing } }
Here I see that in the WHERE clause of the SOQL, user directly passed Trigger.new with IN clause. Is this the correct method? if yes, Is this method encouraged? How can you pass a list of object in the IN clause?
Till now I would have used either a list of Ids to pass in the IN clause or Trigger.newMap.Keyset().
This is also mentioned in on of the trails: https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_triggers/units/apex_triggers_bulk
https://trailhead.salesforce.com/en/modules/apex_triggers/units/apex_triggers_bulk
I don't need to write query in your trigger you can get those Account records in Trigger,New. Use below code it should work fine.
Please let me know if helped.
Regards,
Ramakant