You need to sign in to do that
Don't have an account?
niki s 7
Before insert trigger code optimization
Hello I am writing a trigger on contact object where if I select account lookup field than account s country also get populated in contact object.
code is working fine just need to optimised it .
public class UpdateContactRecordsFromAccount {
public void beforeInsert(List<Contact > neWValues)
{
Set<ID> id_po = new Set<ID>();
for(Contact po : newValues){
id_po.add(po.AccountId);
}
List<Account> AccountList = [select id , Country__c from Account where Id in : id_po];
for(Contact con : neWValues){
for(Account acc : AccountList)
{
con.Country__c = acc.Country__c;
}
}
}
}
Please help
code is working fine just need to optimised it .
public class UpdateContactRecordsFromAccount {
public void beforeInsert(List<Contact > neWValues)
{
Set<ID> id_po = new Set<ID>();
for(Contact po : newValues){
id_po.add(po.AccountId);
}
List<Account> AccountList = [select id , Country__c from Account where Id in : id_po];
for(Contact con : neWValues){
for(Account acc : AccountList)
{
con.Country__c = acc.Country__c;
}
}
}
}
Please help
I think the above code is not correct trigger syntex. check link...
https://www.sfdcpoint.com/salesforce/apex-trigger-in-salesforce/
mark if helps.
Thank You
Can I optimized it more??
Please use the below code:
Let me know if there is an issue.
Thanks,
Abhishek Bansal.
abhibansal2790@gmail.com