+ Start a Discussion
Sindhu AmbarkarSindhu Ambarkar 

After update event issue

Hi,

Iam trying to copy the address field of account to contact whenever the contact is inserted or updated.It is working fine when the contact is created but when iam trying to update the contact getting the DML exception.CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
Please help me
* After the contact is created shipping address fields of account should be copied to contacts */
trigger ShippingAddresscopytoContact on Contact (after insert,after update) 
{
if(trigger.isInsert||trigger.isUpdate)
    {
            List<Contact> Listofcontacts=[SELECT id,OtherStreet,OtherCity,OtherCountry,OtherPostalCode,OtherState,Account.shippingStreet,Account.shippingCity,Account.shippingState,Account.shippingCountry,Account.shippingPostalCode FROM Contact WHERE ID IN:trigger.new];
            for(Contact c:Listofcontacts)
               {
                System.debug('c.Account.shippingStreet;'+c.Account.shippingStreet);
                c.OtherStreet=c.Account.shippingStreet;
                c.OtherCity=c.Account.shippingCity;
                c.OtherState=c.Account.shippingState;
                c.OtherCountry=c.Account.shippingCountry;
                c.OtherPostalCode=c.Account.shippingPostalCode;
             } //End of for loop
            Database.update(Listofcontacts);
     } //End of if
srlawr uksrlawr uk
You can't call the "Database.update" on the list of contacts IN a trigger for Contacts.... because - when it fires, it will cause all the triggers on Contact to fire (including this one) - which will, of course, call Database.update as above - which will cause all the triggers on Contact to fire (and you can see where I am going).

If you want to manipulate the fields ON a contact when it is being updated - you need to do it in a before update trigger - and all you need to do then is set the values of "c" in the before update trigger (which runs before the contact is commited to the database) and then those values you add will be sent in with the update as it happens anyway (ie. you don't need to do Database.update(listofcontacts).

you can still keep that all in the same trigger. just drop the update line and change the trigger event to before update. You might larely be there straight away.

 
Sindhu AmbarkarSindhu Ambarkar
Hi,

I need to use update statement because of after insert event.Iam not able to get it.

Thanks,
Sindhu
srlawr uksrlawr uk
you can have a trigger that is before update and after insert..
 
trigger ShippingAddresscopytoContact on Contact (after insert,before update)

they won't ever "clash"