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
Teju MeshramTeju Meshram 

Whenever a user updates the account record with rating as Warm then check for its related contact, if there is any related contact then update the account description field with the contact's firstname.

Can you please provide me code for this
Here is my code, but I am getting an error

trigger AccountTrigger on Account (before update) {
    if(Trigger.isBefore && Trigger.isUpdate){
        AccountTriggerHandler.updateAcct(Trigger.new, Trigger.oldMap);
    }

}

public class AccountTriggerHandler {
    public static void updateAcct(List<Account> acctList, Map<Id, Account> actMap){
        
        Set<Id> actId = new Set<Id>();
        
        for(Account a : acctList){
            if((a.rating != actMap.get(a.id).rating && a.rating == 'Warm')&& (a.Type != actMap.get(a.id).Type && a.Type == 'Prospect')){
                 actId.add(a.Id);             
            }
        }
           
        Map<Id, Contact> conMap = new Map<Id, Contact>();
        for(Contact con :[SELECT id, firstName from Contact where accountId IN : actId]){
            
                conMap.put(con.id, con);
        }

        for(Account act : [SELECT id, description from Account where Id IN : actId]){
            act.Description = conMap.get(act.id).firstname;
            
        }        
        
    }

}
SwethaSwetha (Salesforce Developers) 
HI Teju,
What is the error you are seeing? Thanks
Teju MeshramTeju Meshram
I just updated my code.
But this works only when there is a single related contact and it fails when there is more than one contact
public class PracticeActTrigger {
    public static void updtAcct(List<Account> acctList, Map<Id, Account> actMap){
        
        Set<Id> setOfAccctId = new Set<Id>();
        
        for(Account a : acctList){
            if((a.rating != actMap.get(a.id).rating && a.rating == 'Warm')&& (a.Type != actMap.get(a.id).Type && a.Type == 'Prospect')){
                 setOfAccctId.add(a.Id);             
            }
        }
           
        Map<Id,Contact> mapToUpdate = new Map<Id, Contact>();
        if(!setOfAccctId.isEmpty()){
            for(Account acc : [Select id, description, (SELECT id, firstname, lastName from Contacts) from Account where id IN : setOfAccctId]){
                mapToUpdate.put(acc.Id, acc.contacts);
            }
        }
        if(!mapToUpdate.isEmpty())
        for(Account a : acctList){
            if(mapToUpdate.containsKey(a.id)){
                    a.Description = mapToUpdate.get(a.id).firstName;        
                
            }
        }
        
    }

}
 
Arun Kumar 1141Arun Kumar 1141

Hi Teju,
Try below code to update the Account's description field with the related contact's firstName

public class PracticeActTrigger {
    public static void updtAcct(List<Account> acctList, Map<Id, Account> actMap) {
        Set<Id> setOfAccctId = new Set<Id>();
        
        for (Account a : acctList) {
            if ((a.Rating != actMap.get(a.Id).Rating && a.Rating == 'Warm') && (a.Type != actMap.get(a.Id).Type && a.Type == 'Prospect')) {
                setOfAccctId.add(a.Id);             
            }
        }
        
        Map<Id, String> accountToContactNamesMap = new Map<Id, String>();
        
        if (!setOfAccctId.isEmpty()) {
            for (Contact c : [SELECT Id, FirstName, AccountId FROM Contact WHERE AccountId IN :setOfAccctId]) {
                if (!accountToContactNamesMap.containsKey(c.AccountId)) {
                    accountToContactNamesMap.put(c.AccountId, c.FirstName);
                } else {
                    String contactNames = accountToContactNamesMap.get(c.AccountId);
                    contactNames += ', ' + c.FirstName;
                    accountToContactNamesMap.put(c.AccountId, contactNames);
                }
            }
        }
        
        for (Account a : acctList) {
            if (accountToContactNamesMap.containsKey(a.Id)) {
                a.Description = accountToContactNamesMap.get(a.Id);
            }
        }
    }
}

Mark this as best answer if this helps.
Thanks