Ads

Showing posts with label Triggers. Show all posts
Showing posts with label Triggers. Show all posts

Monday 15 April 2013

Record is read-only error in Apex Trigger?

Field update cannot be done after the record has been Updated/Saved.

So, use after insert or after update as the trigger events.

Cheers!!!

Trigger to update the Date field one day less than to next year?

trigger dateUpdate on Pincode__c (before insert, before update)
{
    for(Pincode__c p : trigger.New)
    {
        p.X1st_Anniversary__c = p.Evaluation_Date__c.addDays(364);
        p.X6th_Anniversary__c = p.Evaluation_Date__c.addDays(2184);
    }
}

When to use before and after trigger in Salesforce?

Before Trigger:
In case of validation check in the same object.
Insert or update the same object.

After Trigger: 
Insert/Update related object, not the same object.
Notification email.
We cannot use After trigger, if we want to update a record because it causes read only error. This is because after inserting or updating, we cannot update a record.

How to call Apex class in trigger in Salesforce?


trigger sample on Account (before insert)
{
    for(Account a : trigger.New)
    {
        sampleRest s = new sampleRest();
    }
}



Sample Apex Class:

public class sampleRest
{
     ......................
      ......................
      ......................
      ......................
}

Difference between triggers and workflow rules in Salesforce


Triggers
Work flow rules
Trigger can work across objects.
Workflow Rules will be helpful to update the same object or master object in custom master-detail relationships.
Coding is required.
Coding is not required.
Trigger works before and after some actions.
Workflows work only after some actions.