You need to sign in to do that
Don't have an account?
Srinivas223
using map instead of list
I am trying to implement best practices in my trigger. I want to avoid writing a query in the loop but not getting the logic for it. can anyone please help?
I have a CSRid text field on contact and a badge__c text field in Employee object. I want to autofill employee lookup field on contact from contact trigger based on the CSRid on contact. This is the logic I have written which is working fine but couldnot avoid writing a query in the FOR loop. Please help.
Thank you
list<contact> lstConid = new list<contact>();
if(trigger.isUpdate && trigger.isBefore){
for(contact con : trigger.new){
if(con.CSRid__c != null){ //
lstConid.add(con);
}
}
if(!lstConid.isempty()){
for(contact con: lstConid){
if(con.CSRid__c != null){
Employee_Table__c emp = [select id, name from Employee_Table__c where badge__c =: con.CSRid__c limit 1];
if(emp.id != null)
con.SurveyEmployees__c = emp.id;
}
}
}
// map<lstCSRid, Employee_Table__c> CsrEmpMap = new map<lstCSRid, Employee_Table__c>([select Name from Employee_Table__c where badge__c in :lstCSRid]);
}
I have a CSRid text field on contact and a badge__c text field in Employee object. I want to autofill employee lookup field on contact from contact trigger based on the CSRid on contact. This is the logic I have written which is working fine but couldnot avoid writing a query in the FOR loop. Please help.
Thank you
list<contact> lstConid = new list<contact>();
if(trigger.isUpdate && trigger.isBefore){
for(contact con : trigger.new){
if(con.CSRid__c != null){ //
lstConid.add(con);
}
}
if(!lstConid.isempty()){
for(contact con: lstConid){
if(con.CSRid__c != null){
Employee_Table__c emp = [select id, name from Employee_Table__c where badge__c =: con.CSRid__c limit 1];
if(emp.id != null)
con.SurveyEmployees__c = emp.id;
}
}
}
// map<lstCSRid, Employee_Table__c> CsrEmpMap = new map<lstCSRid, Employee_Table__c>([select Name from Employee_Table__c where badge__c in :lstCSRid]);
}
Let us know if this will help you