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

Thursday, December 2, 2010

Update Namespace Prefix in Your Apex Code

As a developer, it is sometimes very tedious when we have to make all of our code as a Managed Package. Have to go to each custom field/Object in the code and change it manually with the Namespace Prefix.

I have developed a tool which takes input as your Class/VF Page/Trigger etc.. for that matter any of the file type mxml, aspx, php etc... and gives the updated output like in the screen shots below.




 'Hope this helps

You can use the tool from here.

Thanks,
Srinivas.

Saturday, April 24, 2010

Bulk Triggers in Salesforce


Bulk Enabled Triggers in Salesforce

When you have a need of writing a trigger, it is strongly recommended that you make it bulk enabled, so that the trigger can handle number of records whcih can be inserted/updated/deleted through data loader or any other data migration tool.
I have explained writing a basic trigger here.


I will explain how to bulk enable a trigger now in this article. Consider a scenario where you might want to update the opportunity status to "Closed-Won" depending on a boolean field CloseOpportunities__c on each of the account that has been updated (That is, if you want to close the opportunities related to accounts that are being updated). And you will be uploading hundreds of accounts using a data uploader tool.

Here is the trigger.

 


Trigger myTrigger on Account(after update)
{
   
    Set <Id> setAccId = new Set<Id>();

    for(Account a: Trigger.new)
    {
        if(a.CloseOpportunities__c)
            setAccId.add(a.Id);//set always contains distinct Ids
    }
   
    list <Opportunity> lstOpp = [select Id, StageName from Opportunity where AccountId in : setAccId];
    for(Integer i = 0; i < lstOpp.size(); i++)
    {
        lstOpp[i].StageName = "Closed-Won";
    }
    if(lstOpp.size() > 0)
        update lstOpp;

}




And a more genaralised way is something like this.


tirgger MyTrigger on SObject([your events goes here comma seperated])
{
       Set<Id> SetOfIds = new Set<Id>();

       for(SObject s : trigger.new)
       {
             SetOfIds.add(s.idField);
       }
  

       Map<Id,SObject> objMap = new Map<Id,SObject>([select Id, [Other Fields] from SObject where Id in : SetOfIds]);

       for(SObject s : trigger.new)
       {
              //do your processing with map
       }
}



 
Hope this is useful.


Thanks
Srinivas,
www.trekbin.com

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