function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MokshadaMokshada 

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;
    }
        }
    }
}
Best Answer chosen by Mokshada
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mokshada,

The flow should be record triggered flow on Account object .

1) Start element:

OR(ISNEW(), {!$Record.Name}!= {!$Record__Prior.Name})

User-added image2) Add decision box to check if it is due to insert or update.

User-added image3) Add create record element and give details as below .

User-added image4) On the other side of decision box add get record element as below.

User-added image
User-added image
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

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Are you okay with two different flows for the above trigger?

Thanks,
 
MokshadaMokshada
HI,
I need it in one flow.  Can't we do it in single flow?
if not then I am okay with two.



Thanks,
​​​
​​​
MokshadaMokshada
Hi Praveen,
can you please help?

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mokshada,

I am working on it and will share the flow for the same.

Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mokshada,

The flow should be record triggered flow on Account object .

1) Start element:

OR(ISNEW(), {!$Record.Name}!= {!$Record__Prior.Name})

User-added image2) Add decision box to check if it is due to insert or update.

User-added image3) Add create record element and give details as below .

User-added image4) On the other side of decision box add get record element as below.

User-added image
User-added image
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.
 
This was selected as the best answer
Sai PraveenSai Praveen (Salesforce Developers) 
Cont....

Now add decision element next to get record element as below.

User-added image
Now add create record element as below.

User-added image

The entire flow looks as below.

User-added image

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
MokshadaMokshada
Hi Praveen,
you have done everything in single flow right?

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mokshada,

Yes all is done in single flow.

Thanks,