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
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI 

When an Account is updated and the Website is filled in, update all the Profile field on all Contacts to: Profile = Website + ‘/’ + First Letter of First Name + Last Name

Best Answer chosen by SUYASH KUMAR BHARTI
Ajay K DubediAjay K Dubedi
Hi Suyash,

Please use below code it works fine and without nested loop:

Trigger:
trigger filedupdate on Account (After Update)
{   
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        Set<Id> AccountId_Set = new Set<Id>();
        new list<contact>();
        for(Account ac : trigger.new)
        {
            if(ac.website != null)
            {
                AccountId_Set.add(ac.id);
            }
        }
        
        if(AccountId_Set.size()>0)
        {
            List<Contact> Contact_List = [select Id,Firstname,Lastname,Profile__c,Accountid,Account.website from contact where Accountid in :AccountId_Set];
            for(Contact con : Contact_List)
            {
                if(con.FirstName != Null)
                {
                    con.Profile__c = con.account.website + '/' + con.FirstName.substring(0, 1) + con.lastname;
                }
            }
            update Contact_List;
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi suyash,
Try this code for above requirement
trigger filedupdate on Account (after update) {
    set<id> idList = new set<id>();
    list<contact> conlist = new list<contact>();
    for(account acc : trigger.new){
        if(account.website != null){
            idlist.add(acc.id);
        }
        if(idlist.size()>0){
            for(contact c : [select id,firstname,lastname,profile,account.website from contact where accountid in :idlist]){
                c.profile= c.account.website + '/' + c.firstname.left(1) + c.lastname;
                conlist.add(c);
            }
        }
        update conlist;
}
}

Hope this helps you
If this helps you kindly mark it as solved so that it may help others in future.

Thanks and Regards

 
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
Greetings devi 
but this code has a nested loop i think
Ajay K DubediAjay K Dubedi
Hi Suyash,

Please use below code it works fine and without nested loop:

Trigger:
trigger filedupdate on Account (After Update)
{   
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        Set<Id> AccountId_Set = new Set<Id>();
        new list<contact>();
        for(Account ac : trigger.new)
        {
            if(ac.website != null)
            {
                AccountId_Set.add(ac.id);
            }
        }
        
        if(AccountId_Set.size()>0)
        {
            List<Contact> Contact_List = [select Id,Firstname,Lastname,Profile__c,Accountid,Account.website from contact where Accountid in :AccountId_Set];
            for(Contact con : Contact_List)
            {
                if(con.FirstName != Null)
                {
                    con.Profile__c = con.account.website + '/' + con.FirstName.substring(0, 1) + con.lastname;
                }
            }
            update Contact_List;
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
Greetings Ajay,
can you please explain the working of this code
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
ajay i wrote this code and then i checked by updating account but the profile custom field is not visible even after its creation
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
why after update is needed
 
S BishalS Bishal
We are getting the value of the Website field from the SOQL query instead of trigger.new context variable. So, from the Database the updated value should be fetched that's the reason first we need to commit the update then fetch it to use.
S BishalS Bishal
trigger filedupdate on Account (After Update)
{   
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        Set<Id> AccountId_Set = new Set<Id>();
        new list<contact>();
        for(Account acc : trigger.new)
        {
            if((acc.Website != null) && (trigger.oldMap.get(acc.Id).Website != trigger.newMap.get(acc.Id).Website))
            {
                AccountId_Set.add(acc.id);
            }
        }
        
        if(AccountId_Set.size()>0)
        {
            List<Contact> Contact_List = [select Id,Firstname,Lastname,Profile__c,Accountid,Account.website from contact where Accountid in :AccountId_Set];
            for(Contact con : Contact_List)
            {
                if(con.FirstName != Null)
                {
                    con.Profile__c = con.account.website + '/' + con.FirstName.substring(0, 1) + con.lastname;
                }
            }
            update Contact_List;
        }
    }
}

Hi Ajay, 

I think we should add the above underlined logic to increase the usuability the code properly.
Beacuse otherwise everytime we update any other field then also it will override the same value on the Profile field.

Suggestions are most welcome. 

Thanks & Regards,
S Bishal
Sanjana_singh12Sanjana_singh12
How to write the test case for this trigger
Dima MakuhaDima Makuha
I can't help but share a great experience with this platform! Real professionals from Europe are gathered here and my project was done perfectly https://devler.io/. Impressive not only the quality of work, but also the scale of the client base around the world. Join, you won't regret it!