You need to sign in to do that
Don't have an account?
MrHammy
Need to bulkify
Hi!
I am setting up a class, called via a trigger, to set an email address. I need to go from my custom object Product_Assessment__c to its parent case and then on to the contact, retrieve the email and populate it. I have the class working but it is not bulkified, my loop as 2 SOQL query's in it and as we all know that's just BAD! I know I need to build up a map to return the correct email address but am struggling a bit. can someone lend a hand?
Thanks for your time!
I am setting up a class, called via a trigger, to set an email address. I need to go from my custom object Product_Assessment__c to its parent case and then on to the contact, retrieve the email and populate it. I have the class working but it is not bulkified, my loop as 2 SOQL query's in it and as we all know that's just BAD! I know I need to build up a map to return the correct email address but am struggling a bit. can someone lend a hand?
Thanks for your time!
public with sharing class Product_Assessment_set_contact_email { // These variables store Trigger.oldMap and Trigger.newMap Map<Id, Product_Assessment__c> oldpas; Map<Id, Product_Assessment__c> newpas; // This is the constructor // A map of the old and new records is expected as inputs public Product_Assessment_set_contact_email( Map<Id, Product_Assessment__c> oldTriggerpas, Map<Id, Product_Assessment__c> newTriggerpas) { oldpas = oldTriggerpas; newpas = newTriggerpas; } public void set_email(){ for (Product_Assessment__c p : newpas.values()) { case con = [SELECT ContactId FROM Case WHERE Id = :p.case__c ]; contact em = [select email from contact where id = :con.ContactId]; p.email_of_contact__c = em.email; } } }
Please use the below code :
Let me know if you have any issue with the above code.
Thanks,
Abhishek
All Answers
Please use the below code :
Let me know if you have any issue with the above code.
Thanks,
Abhishek
Remember: when you want to bulk things up: all you need to do is gather all your criteria in to one place, and query against it. In this case: you just want case ids - so the easiest way it to make a set of case ids, and query for the values on those cases.
Here is my suggestion - this is UNTESTED and probably has syntax errors:
That will fail - the values were not queried for...
Corrected my logic.