Trigger scenario(when account address is updated related contact address also updates)
ฝัง
- เผยแพร่เมื่อ 12 เม.ย. 2020
- HI,everyone
here you are going to learn about Trigger scenario(when account address is updated related contact address also updates)
this is the most asked question in interview
so like share and subscribe and don't forget to hit that bell icon - วิทยาศาสตร์และเทคโนโลยี
Thanks for your scenario...In my working environment I got a similar kind of requirement I did it in a different way but coming to test class it's not working. Based on your video I completed the task successfully with 100% coverage....Thanks for you & your video.
Well done
Do subscribe the channel so u will get the notification of upcoming videos
Awsm❤️
Thanks for the video
I think this can be done in a single for loop using Trigger.newMap -> Query all contacts where AccountId in Trigger.newMap.keySet() -> iterate on contacts { get the account sobject using contact.AccountId and trigger.newMap -> update the contact address info -> add contact to a list -> update DML after the loop. Also instead of hardcoding fields like MailingAddress and BillingAddress we can also a custom metadata type or custom setting to save the account to contact mapping so that in future if there are more fields to map we don't have to update the trigger every time.
Hope this helps! thanks!
Sample Code -
trigger AccountTrigger on Account(after update){
try{
List listToUpdate = new List();
Map accountMap = trigger.newMap;
List relatedContactList = [SELECT Id,AccountId From Contact WHERE AccountId in :accountMap.keySet());
List accountContactMappingList = getAccountContactMapping();
for(Contact relatedContact : relatedContactList){
Id accountId = relatedContact.AccountId;
Account acc = accountMap.get(accountId);
if(acc != null){
for(AccountContactMapping__mdt accConMapping : accountContactMappingList){
String accountFieldApiName = accConMapping.AccountFieldApiName__c;
String contactFieldApiName = accConMapping.ContactFieldApiName__c;
con.put(contactFieldApiName,account.get(accountFieldApiName));
}
listToUpdate.add(con);
}
}
if(!listToUpdate.isEmpty()){
update listToUpdate;
}
}catch (Exception e){
System.debug('e.getMessage()------>>>'+e.getMessage());
System.debug('e.getStackTraceString()------>>>'+e.getStackTraceString());
//Make an entry to an object called ErrorLog__c //Logger.logException('AccountTrigger','updateRelatedContacts',e.getMessage(),e.getStackTraceString(),System.now(),userInfo.getUserId());
}
}
static List getAccountContactMapping(){
if(!Test.isRunningTest()){
return AccountContactMapping__mdt.getAll().values();
}else{
return List{new AccountContactMapping__mdt(AccountFieldApiName__c='Billing Address',ContactFieldApiName__c='BillingAddress')}
}
}
Thank you for a very useful video
great
Thanks ma'am
Hi Yamini,
I have Query like How many times gets changed in billing address it will store in account object one custom field.
Hello! explanation was absolutely great 👍 thank you...
Could you please be little better louder ...
Can you upload more videos on trigger for different scenarios
Hi ua trigger is working fine but writing for loop inside the for loop is not a best practice right
Yes, for look inside for loop cause recursion in trigger
My client in USA and I am India my currecy symbol change how to automate dollar symbol send that time
How to write test class for this trigger?
Doubt doubt
Why do we need to fetch the deleted contacts here
mam plz explain with handler class also
Similer same..but in method we take parameters as list aclist ..then logic..same..
After in trigger calling handler method with (trigger.new)
voice is very slow.
Do you think it will work ?you didnt include the condition to check whether the address field has updated
Yes i was also thinking about the same