Friday, April 2, 2010

Triggers

Writing a basic Trigger:

The possible events of trigger are
  • before insert, before update, before delete, after insert, after update, after delete, after undelete
Example:

trigger MyTrigger on Account(after insert)
{
//Update Account Name by appending some dummy text
Account a = new Account(Id= trigger.new[0].Id);
a.Name = 'Test ' + Trigger.new[0].Name;
update a;
}

Please note, the above is a very basic trigger. I will discuss in other article how to write a Trigger which is bulk enabled

8 comments:

  1. Hi Srinivas,

    How can i write a trigger to create the opportunities to a particular campaign member who has registered automatically based on the member status?

    Thanks in advance.

    Naidu

    ReplyDelete
  2. trigger MyTrigger on CampaignMember(after insert)
    {
    list lstOp = new list();
    for(Integer i = 0; i < Trigger.new.size(); i++)
    {

    if(Trigger.new[i].Status == 'test status')//provide appropriate staus field here
    {
    opportunity op = new Opportunity();
    op.Name = 'test';
    //Add the remaining necessary fields here like the above
    lstOp.add(op)
    }
    }

    if(lstOp.size() > 0)
    {
    insert lstOp;
    }
    }

    ReplyDelete
  3. Hi Srinivas,



    I have 2 custom objects application and recommender , in the application I have a field name as recommender email address .So, I need to come up with an trigger such that when an recommender email address is filled , the trigger should be fired which in turn creates a recommender object which is associated with an application and application ID should be populated.



    I would really appreciate any help in solving this...any ideas, solutions are most welcome....

    ReplyDelete
  4. trigger mytrigger on Application__c(after insert)
    {
    if(trigger.new[0].recom_Email__c != null)
    {
    Recom__c obj = Recom__c();
    obj.ApplicationId__c = trigger.new[0].Id;
    obj.Email__c = trigger.new[0].recom_Email__c;
    //rest of the recom fields here
    insert obj;
    }
    //You might want to bulk enable this trigger. check the best practices for that
    }

    ReplyDelete
  5. Hi Srinivas,

    I have custom object named "Dongle" that related to Account Object. There are not Master-Detail relationship between these two object. However i would like to create a Roll-Up Summary field on Account object to summarize data from "Dongle" field named "Number of seats".

    I would appreciate very much any kind of help in this. Anyway thank you very much!

    ReplyDelete
  6. Hi Eddie,
    Rollup Summary is not supported for Lookups. If you could not make it as master detail relationship, then you can do this with triggers.
    You have to write a trigger on Dongle object (after insert, after update) and you need to add/edit the Number of Seats on Account each time a Dongle record is inserted/updated.
    If you are unable to achieve this let me know. I will post some code.
    Thanks
    Srinivas
    Trekbin.
    www.trekbin.com

    ReplyDelete
  7. i need help creating a trigger that auto populates the state field when a zip code is sentered.

    This is what i have so far:
    trigger zipCode on Lead (After insert, After update) {

    Lead c = new lead(zip
    = trigger.new[0].zip);
    For (Zip/Postal_Code c : trigger.new)
    }

    ReplyDelete
  8. How to get an uploaded file as a link via feedpost?

    ReplyDelete