You need to sign in to do that
Don't have an account?
Mokshada
How to convert this trigger of creating new related contact of account after updating name of account into flow
Here I have a trigger whenever I create a new account it should create new contact with the account name.
if account name is updated of existing account then it will check if related contact exist with new name if not exist then create new contact .
Trigger:
trigger AccountContactTrig on Account (after insert,after update) {
Set<Id> accIds= new Set<Id>();
If(Trigger.isinsert && Trigger.isafter){
List<Contact> cont = new List <Contact>();
for(Account acc : trigger.new){
Contact c = new Contact();
c.AccountId=acc.id;
c.LastName = acc.name;
cont.add(c);
}
insert cont;
}
if(Trigger.isupdate && Trigger.isafter){
Set<String> accnames= new Set<String>();
For(Account acc:Trigger.new){
Account oldacc= Trigger.oldmap.get(acc.id);
if(acc.name!= oldacc.Name){
accnames.add(acc.name);
}
}
Map<Id,Account> mapaccounts = new Map<Id,Account>([Select Name,(Select id, Lastname from contacts where Lastname in: accnames) from Account where id in:Trigger.new]);
List<Contact> cont = new List <Contact>();
if(mapaccounts.size()>0){
for(Account ac: Trigger.new){
if(Ac.Name != trigger.OldMap.get(Ac.Id).Name){
if(mapaccounts.containsKey(ac.id)){
if(mapaccounts.get(ac.id).Contacts.size()==0){
Contact c = new Contact();
c.AccountId=ac.id;
c.LastName = ac.name;
cont.add(c);
}
}
}
insert cont;
}
}
}
}
if account name is updated of existing account then it will check if related contact exist with new name if not exist then create new contact .
Trigger:
trigger AccountContactTrig on Account (after insert,after update) {
Set<Id> accIds= new Set<Id>();
If(Trigger.isinsert && Trigger.isafter){
List<Contact> cont = new List <Contact>();
for(Account acc : trigger.new){
Contact c = new Contact();
c.AccountId=acc.id;
c.LastName = acc.name;
cont.add(c);
}
insert cont;
}
if(Trigger.isupdate && Trigger.isafter){
Set<String> accnames= new Set<String>();
For(Account acc:Trigger.new){
Account oldacc= Trigger.oldmap.get(acc.id);
if(acc.name!= oldacc.Name){
accnames.add(acc.name);
}
}
Map<Id,Account> mapaccounts = new Map<Id,Account>([Select Name,(Select id, Lastname from contacts where Lastname in: accnames) from Account where id in:Trigger.new]);
List<Contact> cont = new List <Contact>();
if(mapaccounts.size()>0){
for(Account ac: Trigger.new){
if(Ac.Name != trigger.OldMap.get(Ac.Id).Name){
if(mapaccounts.containsKey(ac.id)){
if(mapaccounts.get(ac.id).Contacts.size()==0){
Contact c = new Contact();
c.AccountId=ac.id;
c.LastName = ac.name;
cont.add(c);
}
}
}
insert cont;
}
}
}
}
The flow should be record triggered flow on Account object .
1) Start element:
OR(ISNEW(), {!$Record.Name}!= {!$Record__Prior.Name})
2) Add decision box to check if it is due to insert or update.
3) Add create record element and give details as below .
4) On the other side of decision box add get record element as below.
In the above storecontactId is we have to create resource of type variable and type should be text
Will share the remianing part in my next comment.
All Answers
Are you okay with two different flows for the above trigger?
Thanks,
I need it in one flow. Can't we do it in single flow?
if not then I am okay with two.
Thanks,
can you please help?
Thanks
I am working on it and will share the flow for the same.
Thanks,
The flow should be record triggered flow on Account object .
1) Start element:
OR(ISNEW(), {!$Record.Name}!= {!$Record__Prior.Name})
2) Add decision box to check if it is due to insert or update.
3) Add create record element and give details as below .
4) On the other side of decision box add get record element as below.
In the above storecontactId is we have to create resource of type variable and type should be text
Will share the remianing part in my next comment.
Now add decision element next to get record element as below.
Now add create record element as below.
The entire flow looks as below.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
you have done everything in single flow right?
Thanks,
Yes all is done in single flow.
Thanks,