Thank you for your support. Coming to your question This trigger will update the description field of an Account record with the Description of its most recently updated related Contact record.
Nice but I think the question itself not very clear. account may have multiple contact then which contact description it should update. trigger.new() may have multiple contact related to one account then it will override the description each time. if someone reparent the account or edit the contact and remove the account selection, then it will not be able to handle it.
Hi Soumendu thanks for your comment.I appreciate your feedback. See it is a very simple example for beginners to demonstrate how we can update parent by child. In case of reparenting account we need to fetch and store both old parent and new parent account ids in set which i have used and explained in my further videos.
Hi I got doubt like instead of map can we use listAc list=[select Id,AccountId,description,account.description from contact where I’d in:ContId]; Then we can itereate through list like for(Account acc:Ac list)
Hi, Thank you very much for your lectures. I have a small doubt could you please explain it. what if an account contains multiple contacts associated with it, which contact description is going to be in account description filed.
Hi, will this work? public void conmethod(list nlcon, map omcon){ set accId = new set(); map accdes = new map(); for(contact c : nlcon){ if(c.description != omcon.get(c.id).description && c.accountId!=null){ accId.add(c.accountId); accdes.add(c.accountId, c.description); } }
list lacc = [select id,description from account where Id in : accId]; list llacc = new list(); for(account a : lacc){ a.description = accdes.get(a.id); llacc.add(a); } update llacc; }
There are some issue in it 1) you are using add in Map 2)This can be done without using any SOQL query.Reduce SOQL query wherever possible Please find : trigger contactdescription on contact (after insert, after update){ map accmap = new map(); list acclisttoupdate = new list(); for(contact con: trigger.new){ if(trigger.isAfter){ if(con.description != null && con.AccountId !=null && (trigger.isupdate && con.description != trigger.oldmap.get(con.id).description || trigger.isInsert) ){ accmap.put(con.AccountId,con.Account); } } } if(!accmap.isEmpty()){ for(contact con:trigger.new){ Account acc = new Account(); acc.id = con.AccountId; acc.description = con.description; acclisttoupdate.add(acc); } if(!acclisttoupdate.isEmpty()){ update acclisttoupdate; } } }
Hi Please comment what is wrong if i do like below : List updateaccList = new List(); if(trigger.isupdate){ for(Contact c: trigger.new){ if(c.Description!=trigger.Oldmap.get(c.id).description){ Account a = new Account(id=c.AccountId); a.description=c.Description; updateaccList.add(a); } } update updateaccList; }
hi buddy sorry for late reply, you can apply null checks on trigger.new and list of account before updating it also it is a good practice to use handler class
Do we need to take all the Account IDs separately in the Set? My below code works fine. trigger contactTrigger on Contact (after update) { if(trigger.isAfter && trigger.isUpdate) { if(!trigger.new.isEmpty()) { List updateAccount = new List(); for(Contact con : Trigger.new) { if(con.Description != Trigger.OldMap.get(con.Id).description && con.AccountId != null) { Account acc = [Select id,Description from Account where id = :con.AccountId]; acc.Description = con.Description; updateAccount.add(acc); } } if(!updateAccount.isEmpty()) update updateAccount; } } }
Amazing example! Thanks!
thank you so much
Amazing.. and useful content for live coding in SFDC Interviews....In the above example what if single account has multiple contacts???
Good Question.
I think the Account Description must be same as Contact whose description will be updated at last,
Thank you for your support.
Coming to your question This trigger will update the description field of an Account record with the Description of its most recently updated related Contact record.
@stdcninjas Very good, at every needed point you are adding null check. Good practice
Nice but I think the question itself not very clear. account may have multiple contact then which contact description it should update. trigger.new() may have multiple contact related to one account then it will override the description each time.
if someone reparent the account or edit the contact and remove the account selection, then it will not be able to handle it.
Hi Soumendu thanks for your comment.I appreciate your feedback. See it is a very simple example for beginners to demonstrate how we can update parent by child. In case of reparenting account we need to fetch and store both old parent and new parent account ids in set which i have used and explained in my further videos.
Interesting Scenario - Can you please create a video with this exact scenario using Trigger Handler and Trigger separately as it is Best Practice.
Sorry for late reply
handler class -> public class ContactTriggerHandler
{
public static void trgMethod(List newConList,Map oldConMap)
{
Set accIds = new Set();
Contact oldCon = new Contact();
List accList = new List();
if(!newConList.isEmpty())
{
for(Contact newCon : newConList)
{
if(oldConMap != null)
{
oldCon = oldConMap.get(newCon.Id);
}
if(newCon.AccountId != null && (oldCon == null || oldCon.Description != newCon.Description))
{
accIds.add(newCon.AccountId);
}
}
}
if(!accIds.isEmpty())
{
Map accMap = new Map([Select Id,Description from Account where Id IN : accIds]);
for(Contact cont : newConList)
{
if(cont.AccountId != null && accMap.containsKey(cont.AccountId))
{
Account acc = accMap.get(cont.AccountId);
acc.Description = cont.Description;
accList.add(acc);
}
}
}
if(!accList.isEmpty())
{
update accList;
}
}
}
trigger -> trigger conTrg on Contact (after insert, after update)
{
if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
{
ContactTriggerHandler.trgMethod(trigger.new,trigger.oldMap);
}
}
I am new to coding, if you explain when to use Map and when to use set that would be helpful
I will create a video on this for sure.
Thanks , I could crack a problem in my project using your solution, it saved my day thanks
Thank you so much sir
Hi I got doubt like instead of map can we use listAc list=[select Id,AccountId,description,account.description from contact where I’d in:ContId];
Then we can itereate through list like for(Account acc:Ac list)
Hi , if we will use List then we have to use nested for loop which is not a good practice so to avoid problem of nested for loop we are using map.
Great session , but If more than one contact is there, how it will get updated?
Thanks, it will take the description from latest updated contact
Hi,
Thank you very much for your lectures.
I have a small doubt could you please explain it.
what if an account contains multiple contacts associated with it, which contact description is going to be in account description filed.
Hi Sabareesh in that case the latest Contact's description will be populated in Account's Description
Hi for the First For loop you filter all the records and then
Again use trigger.new for which is unfiltered.
Can you explain on this?
Very nice sir ❤
thanks buddy
Thank you very much sir
Why we didn't create map first, like we did in parent to child
Amit there could be many different ways to write a trigger.
Hi Ninja Is it mandatory to fetch the description of Parent record to update ?
Sorry for late reply, yes it is required otherwise you will get soql error
You can avoid the other for loop by below code
public class UpdateContact {
public static void UpdateDescription(Map oldContactMap,Map newContactMap){
List accList = new List();
for(Contact con : newContactMap.values()){
if(!con.Description.Equals(oldContactMap.get(con.Id).Description)){
Account acc = new Account();
acc.Id = con.AccountId;
acc.Description =con.Description;
accList.add(acc);
}
}
if(!accList.isEmpty()){
Update accList;
}
}
}
Hi, will this work?
public void conmethod(list nlcon, map omcon){
set accId = new set();
map accdes = new map();
for(contact c : nlcon){
if(c.description != omcon.get(c.id).description && c.accountId!=null){
accId.add(c.accountId);
accdes.add(c.accountId, c.description);
}
}
list lacc = [select id,description from account where Id in : accId];
list llacc = new list();
for(account a : lacc){
a.description = accdes.get(a.id);
llacc.add(a);
}
update llacc;
}
Hello, Yes it will work correctly but please use best practices like null checks on list.
There are some issue in it
1) you are using add in Map
2)This can be done without using any SOQL query.Reduce SOQL query wherever possible
Please find :
trigger contactdescription on contact (after insert, after update){
map accmap = new map();
list acclisttoupdate = new list();
for(contact con: trigger.new){
if(trigger.isAfter){
if(con.description != null && con.AccountId !=null && (trigger.isupdate && con.description != trigger.oldmap.get(con.id).description || trigger.isInsert) ){
accmap.put(con.AccountId,con.Account);
}
}
}
if(!accmap.isEmpty()){
for(contact con:trigger.new){
Account acc = new Account();
acc.id = con.AccountId;
acc.description = con.description;
acclisttoupdate.add(acc);
}
if(!acclisttoupdate.isEmpty()){
update acclisttoupdate;
}
}
}
Thanks for sharing ,sorry i missed that thing.
Nice Raja bhai your code is Very Simple thx for the code Man !!!
Are you salesforce developer ?? @@rajajha6386
It is child to parent scenario not parent to child
Hi Chitresh i think you got confused in video title it is mentioned that update parent by child .
Ohh sorry it's my mistake
Can you make video on dispatcher framework
Hi I am following the same code but still my trigger is not working what could be the issue?
show me your code please
@@sfdcninjas problem has solved bro.
that’s good and Sorry for late reply buddy
Simpler solution
trigger conTrigger on contact(after update){
Map accountMapToUpdate = new map();
for(Contact con:trigger.new){
if(con.description__c!=null && con.description__c!=trigger.oldMap.get(con.Id).description__c){
accountMapToUpdate.put(con.accountId,con.description__c);
}
}
List accountsToUpdate = new List();
if(!accountMapToUpdate.isEmpty()){
for(Id accid: accountMapToUpdate.keySet()){
Account acc = new Account(
Id = accid;
acc.Acc_description__c = accountMapToUpdate.get(accid)
)
accountsToUpdate.add(acc);
}
}
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
If we write accMap.get(cont.accountId). description=cont.description
Will that work?
yes that will work
@@sfdcninjas If I do that Like accMap.get(cont.accountId). description=cont.description ,
Then What should i add in ListTobeUpdated(????)
if u using this logic how u gonna store this in Account object and later how you gonna add it in collection to perform DML.
I think here we get null pointer exception
Because what if that cont.account id doest not exists in accmap
Hi Please comment what is wrong if i do like below :
List updateaccList = new List();
if(trigger.isupdate){
for(Contact c: trigger.new){
if(c.Description!=trigger.Oldmap.get(c.id).description){
Account a = new Account(id=c.AccountId);
a.description=c.Description;
updateaccList.add(a);
}
}
update updateaccList;
}
what if contact doesn't have an account mapped? it will thrown an exception
trigger updateDiscription on Contact (after update) {
If(trigger.isAfter && trigger.isupdate){
List acc= new List();
for(contact c : trigger.new){
IF(c.Description != trigger.oldmap.get(c.Id).Description){
account a = new account();
a.id = c.AccountId;
a.Description=c.Description;
acc.add(a);
}
}
update acc;
}
}
This code is also working am i missing any best practice hear?
hi buddy sorry for late reply, you can apply null checks on trigger.new and list of account before updating it also it is a good practice to use handler class
Do we need to take all the Account IDs separately in the Set?
My below code works fine.
trigger contactTrigger on Contact (after update) {
if(trigger.isAfter && trigger.isUpdate)
{
if(!trigger.new.isEmpty())
{
List updateAccount = new List();
for(Contact con : Trigger.new)
{
if(con.Description != Trigger.OldMap.get(con.Id).description && con.AccountId != null)
{
Account acc = [Select id,Description from Account where id = :con.AccountId];
acc.Description = con.Description;
updateAccount.add(acc);
}
}
if(!updateAccount.isEmpty())
update updateAccount;
}
}
}
sorry for late reply, yes it is a best practice to use collection for these type of requirements.
Thank you so much sir
Most welcome