+ Start a Discussion
indra reddy 19indra reddy 19 

how to write the trigger program ?

whever a contact is created or updated then it should update the related account name with the exit account name appended with contact last name and contact mailing address should be update to the account shipping address and then the update account's related opportunities name should update with the account name appended with the value "Opp" and the stage should update to prospecting and close date should be one month from today's date.
Raghu NaniRaghu Nani
Hi Reddy,

If your project is simple and if you have only few process builders used on contact and account, you can opt for process builder option. In this senario you have to use 2 process builders, one on contact to update the account values, & another on Account to update the opportunity values.
If you need to address the much more senarios, trigger & handler also be the best option.

indra reddy 19indra reddy 19
Hi Raghu,
thanks for your information. but how to write the trigger program
Deepali KulshresthaDeepali Kulshrestha
Hi Indra,

I have tried this trigger before but I only made it add the last name of Contact to Account Name. I have modified the program according to your requirements. 

Try this code for trigger:
Trigger - 

trigger UpdateContactAccountTrigger on Contact (after insert, after update) {
    if(Trigger.isAfter) {
        if(Trigger.isInsert) {

Handler Class - 

public class UpdateContactAccountHandler {
    public static void updateContactAccountOpp(List<Contact> conListNew) {
            Set<Account> accList = new Set<Account>();
            Set<Id> accSet = new Set<Id>();
            List<Contact> conList = new List<Contact>([Select LastName, AccountId, Account.Name,MailingCity,MailingState,Account.ShippingCity,Account.ShippingState From Contact Where Id In :conListNew]);
            List<Contact> contactToUpdate = new List<Contact>();
            for(Contact con: conList) {
                if(con.AccountId != null) {
                    con.Account.Name = con.Account.Name + con.LastName;
                    con.MailingCity = con.Account.ShippingCity;
                    con.MailingState = con.Account.ShippingState;
            List<Opportunity> oppToUpdate = new List<Opportunity>();
            List<Opportunity> opportunityList = new List<Opportunity>();
            opportunityList = [SELECT Id, Name, AccountId,Account.Name FROM Opportunity WHERE AccountId IN :accSet];
            for(Opportunity opp : opportunityList ){
                opp.Name = opp.Account.Name+'opp';
                opp.StageName = 'Prospecting';
                opp.CloseDate = date.today().addDays(30);
            if(accList.size()>0) {
                update accList;
            update contactToUpdate;
            update oppToUpdate;
        } catch(Exception e) {
            System.debug('Error - '+e.getmessage()+' in line number - '+e.getLineNumber());

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

Thanks and Regards,
Deepali Kulshrestha