Saturday 14 May 2011

Writing Apex Trigger : Trigger vs Workflow

Hi All,

Triggers are very essential in any business logic. Triggers are fired when any event gets executed in database. These events can be insertion, update or deletion of rows in data tables in the data tables. In Apex triggers are fired when any DML statement gets executed that means a new record added, existing record updated or deleted. Triggers in salesforce triggers are extended form of workflows. They provide more complex logic to be added to any DML action. So before learning how to write an Apex trigger it is important to make a decision when to go for a trigger not for workflow.

Trigger vs Workflow

S. No. Trigger Workflow
1. Triggers can not be updated on production org after being created via a managed package. Actions in workflows can be edited,removed and new actions can be added
2. More complex logic can be added in the triggers like DML actions on other object than the object on which trigger is written Less Complex logic is supported
3. Trigger get fired on more actions like on insert , update , delete and undelete Workflow get fired on insert and update
4. You can decide excution of trigger whether it should be before or after the initiation action like ( on before insert trigger or on after update trigger) Workflow only gets executed after any insert or update

So by above comparison you may decide when to opt for a trigger over a workflow.


Regards

7 comments:

  1. very nice comparison is given by you and also very helpful for me.

    ReplyDelete
  2. Thank u sharma,

    I have learned triggers from u r blog,Now i got confidence.

    Haribabu
    flexandsalesforce.blogspot.com

    ReplyDelete
  3. I was look at your update trigger (the fax number example) and was wondering how you would add a new record (Contact) based on a field updated on the Account page?

    ReplyDelete
  4. I guess this is your answer

    trigger Trigger1 on Account (after update)
    {

    for(Account acc : Trigger.New)
    {
    //Trigger Action Logic
    List listCon = [Select Fax from Contact where AccountId =: acc.id];
    if(listCon.size() == 0)
    listCon.add(new Contact(LastName = 'New Contact', Fax = acc.Fax)
    for(Contact con : listCon)
    {
    con.Fax = acc.Fax;
    }
    upsert listCon;

    }
    }

    ReplyDelete
  5. Hi Shahsikant,

    I have written a query where I want to update a field on opportunity if Opportunity Owner of any opportunity is changed. I have a field Previous_Owner__c on opportunity which stores the name of previous owner if owner is changed.

    I am getting the below error:

    "Apex trigger OpptyPreOwnAssn caused an unexpected exception, contact your administrator: OpptyPreOwnAssn: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006P0000003XNGsIAO; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 006P0000003XNGs) is currently in trigger OpptyPreOwnAssn, therefore it cannot recursively update itself".





    trigger OpptyPreOwnAssn on Opportunity ( before update) {

    List listOppty= [Select Id,OwnerID, Previous_Owner__c from Opportunity where Id In:Trigger.New];

    Set Owner1id = new Set();



    List opp2= new List();



    for(Opportunity opp1 : listOppty){

    Owner1id.add(opp1.OwnerId); }



    List< User > listusr = [Select Id,Name from User where ID in : Owner1id];


    for (ID id1:Owner1ID){

    for (Opportunity opp:listOppty){

    for (User usr:listusr){

    if(opp.ownerid==usr.id){

    opp.Previous_Owner__c=usr.Name;

    opp2.add(opp);

    }} } }

    update opp2;

    }


    I am not able to find the solution. Can you please help and reply me at alokalokaryan@gmail.com

    Hope to hear from u very soon.

    Thanks
    Alok

    ReplyDelete

Tweet