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
nitinkhunal.ax1786nitinkhunal.ax1786 

Please help me writing trigger for this scenerio

I want to write a trigger:

 

I've one 'Account' object and 'Contact' is its child object. In 'Account Object' there is a custom field Name 'Contact_Name__c'.

 

If i inserts any record in 'Contact' object than its 'First Name'  field value copies into the 'Contact_Name__c' field.

 

I wants that all 'Contact' Object records 'First Name' copies into 'Contact_Name__c' field those are in relationship with that 'Account' Object.

 

For Example:

 

If Account Name-'Acc' and in Child 'Contact' have 3 records.

and First Name of these records are- 'Nitin', 'Rahul', 'Ashwani'

 

Now i want these field values in Contact_Name__c field

Contact Name: Nitin, Rahul, Ashwani

 

So how i can do this. Please help me to write trigger for it.

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

try this

 

trigger UpdateContactName on Contact(after insert){

List<Account> accListToBeUpdated = new List<Account>();
Map<ID,Account> accMap = new Map<ID,Account>([SELECT id,name,Contact_Name__c FROM Account WHERE id in :Trigger.new.AccountId]);
for(Contact con : Trigger.new){
Account acc = new Account();
if(con.AccountId != NULL){
acc = accMap.get(con.AccountId);
if(acc.Contact_Name__c == NULL){
acc.Contact_Name__c = con.name;
}
else{
acc.Contact_Name__c += ',' + con.name;
}
}
accListToBeUpdated.add(acc);
}
if(accListToBeUpdated.size() > 0){
update accListToBeUpdated;
}
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

All Answers

souvik9086souvik9086

try this

 

trigger UpdateContactName on Contact(after insert){

List<Account> accListToBeUpdated = new List<Account>();
Map<ID,Account> accMap = new Map<ID,Account>([SELECT id,name,Contact_Name__c FROM Account WHERE id in :Trigger.new.AccountId]);
for(Contact con : Trigger.new){
Account acc = new Account();
if(con.AccountId != NULL){
acc = accMap.get(con.AccountId);
if(acc.Contact_Name__c == NULL){
acc.Contact_Name__c = con.name;
}
else{
acc.Contact_Name__c += ',' + con.name;
}
}
accListToBeUpdated.add(acc);
}
if(accListToBeUpdated.size() > 0){
update accListToBeUpdated;
}
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

This was selected as the best answer
Dhaval PanchalDhaval Panchal

Try below trigger, It will support insert, update and delete operation. Also if you change account then it will update both accounts.

 

trigger updateAccountContactName on Contact (after insert, after update, after delete) {
    Set<ID> setAccountId = new Set<ID>();
    List<Account> lstAccount = new List<Account>();
    if(trigger.IsInsert || trigger.IsUpdate){
        for(Contact con:Trigger.New){
            if(con.AccountId <> null && con.FirstName <> null){
                if(Trigger.IsInsert || (Trigger.IsUpdate && con.FirstName <> Trigger.oldMap.get(con.Id).FirstName)){
                    setAccountId.add(con.AccountId);
                }
            }
            if(con.AccountId <> Trigger.OldMap.get(con.Id).AccountId){
                setAccountId.add(con.AccountId);
                setAccountId.add(Trigger.oldMap.get(con.Id).AccountId);
            }
        }
    }
    if(trigger.IsDelete){
        for(Contact con:Trigger.Old){
            if(con.AccountId <> null){
                setAccountId.add(con.AccountId);
            }
        }
    }
    if(setAccountId.size()>0){
        lstAccount = [Select Id, Contact_Name__c, (Select FirstName From contacts) From Account Where Id in:setAccountId];
        if(lstAccount.size()>0){
            for(Account acc:lstAccount){
                acc.Contact_Name__c = '';
                if(acc.contacts.size()>0){
                    for(Contact con:acc.contacts){
                        if(acc.Contact_Name__c == '')
                            acc.Contact_Name__c = con.FirstName;
                        else
                            if(con.FirstName <> null)
                                acc.Contact_Name__c += ', ' + con.FirstName;
                    }
                }
            }
            update lstAccount;
        }
    }
}

 I have tested this trigger and it works fine for me.