+ Start a Discussion
iyappan kandasamy 4iyappan kandasamy 4 

trigger - Need combine of 2 triggers into one trigger

Question : Whenever account is created then insert a contact and then when the account field "description" is updated then update the contact description also....

Actually I have got solution seperately...but it would be of great help if it is combined...Thanks

1. Trigger when the account field "description" is updated then update 
    -----------------------------------------------------------------------------------------
     the contact description also..
     --------------------------------------
trigger acc on account(after update)
{
set<id> accids = new set<id>();
for(account a : trigger.new)
{
accids.add(a.id);
}
list<contact> conlist=[select id,account.description, description from contact where accountid=: accids];
for (contact c: conlist)
{
c.description=c.account.description;
}
update conlist;
}

2.  Whenever account is created then insert a contact
      -------------------------------------------------------------------
trigger createcon on account(after insert)
{
list<contact> conlist = new list<contact>();
for (account a:trigger.new)
{
contact c = new contact();
c.lastname= a.name;
c.accountid=a.id;
conlist.add(c);
}
insert conlist;
}

I want to combine the above 2 triggers into one trigger...Any help would be great...Thanks in advance...
Best Answer chosen by iyappan kandasamy 4
Maharajan CMaharajan C
Hi ,

Please try to use the Trigger and Helper class,

Trigger:

trigger Tr_AccTrigger on Account (after insert,after update) {
    
    if(Trigger.isInsert)
    {
        AccTriggerHelper.handleAccount(Trigger.New, 'Insert');
    }
    else if(Trigger.isUpdate)
    {
           AccTriggerHelper.handleAccount(Trigger.New, 'Update');
    }
}

Helper Class:

public class AccTriggerHelper {
    public static void handleAccount(List<Account> accList, String context)
    {
        list<contact> conlist = new list<contact>();
        set<id> accids = new set<id>();

        if(context == 'Insert')
        {
            for (account a : accList)
            {
                contact c = new contact();
                c.lastname= a.name;
                c.accountid=a.id;
                conlist.add(c);
            }
            insert conlist;
        }
        else if(context == 'Update')
        {
            for(account a : accList)
            {
                accids.add(a.id);
            }
            list<contact> contactlist=[select id,account.description, description from contact where accountid=: accids];
            for (contact c: contactlist)
            {
                c.description=c.account.description;
            }
            update contactlist;
        }
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi ,

Please try to use the Trigger and Helper class,

Trigger:

trigger Tr_AccTrigger on Account (after insert,after update) {
    
    if(Trigger.isInsert)
    {
        AccTriggerHelper.handleAccount(Trigger.New, 'Insert');
    }
    else if(Trigger.isUpdate)
    {
           AccTriggerHelper.handleAccount(Trigger.New, 'Update');
    }
}

Helper Class:

public class AccTriggerHelper {
    public static void handleAccount(List<Account> accList, String context)
    {
        list<contact> conlist = new list<contact>();
        set<id> accids = new set<id>();

        if(context == 'Insert')
        {
            for (account a : accList)
            {
                contact c = new contact();
                c.lastname= a.name;
                c.accountid=a.id;
                conlist.add(c);
            }
            insert conlist;
        }
        else if(context == 'Update')
        {
            for(account a : accList)
            {
                accids.add(a.id);
            }
            list<contact> contactlist=[select id,account.description, description from contact where accountid=: accids];
            for (contact c: contactlist)
            {
                c.description=c.account.description;
            }
            update contactlist;
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
iyappan kandasamy 4iyappan kandasamy 4
Thanks Maharajan for your help...It worked....Thanks again