Authored by Zishan Razzaq
Showing posts with label Triggers. Show all posts
Showing posts with label Triggers. Show all posts

Thursday, November 27, 2008

Understanding a Trigger in Salesforce.com

Hi again:
This will be the first of many lessons on Triggers. But before I start writing triggers in salesforce.com, I need to explain triggers and how they work. One needs to understand before anything. So lets start.
  1. What is a Trigger?
"A Trigger is a block of Apex Code that executes in response to a particular type of change in a record's data." from the Force.com Developer Guide Page 336

  • What does the above definition mean in simple English. Basically a trigger is an event that fires off when a record is changed.
  • by changed I mean, you can:
    • Update a Record - Updating an existing record.
    • Insert Record - Inserting a new record like a new Account or Contact.
    • Delete Record - before the record deletes make sure everything is valid.
  • These three Actions we call (Update, Insert, Delete) are the main functions of a trigger. Basically which one will use a trigger for.
  • Also one can use a trigger as a controller combine with Visualforce or calling a web service API.
  • People have asked the question "How can I override the Save button in Force.com" Plain and simple way is to write a trigger. Why or How?
  • Simple, when you create or update a record the only way your new information weather you added information to an existing record or changed it or you added a new record, one would have to click SAVE to place the information into the database.
  • By clicking Save if you have a trigger on that Object then you can fire it off, thus overriding the save button. It will save and do the commands you wrote in the trigger itself.
  • Also, triggers must be written in Sandbox and then deploy to server via eclipse tool.
  • It must pass the standard requirement of 75% in sandbox before deploying into production.


I hope this cleared many questions about understanding a trigger. My next post will be on writing a trigger and implementing it.
Thanks
View my Other Blogs:
VisualForce Made Easy

Salesforce Data Migration Made Easy

Wednesday, July 23, 2008

Too many SOQL queries: 21 Error in Force.com

Hi and Welcome:
Basically this error defines that your code went over the governing level of salesforce. Basically, this happens when you are uploading data through an web service api or data loader.

Just remember you will have to keep your batch size low due to the trigger governor limits (soql statements). A batch of 20 is just fine, or less.

So an example:
Scenario:
  1. Upload through a 3rd party tool to salesforce from sql server into the product and pricebookentry table.
  2. This is to update the existing products into salesforce. (NOT AN INSERT BUT AN UPDATE)
  3. The trigger will update the account making the IsActive(Price) Active on the current Pricebook which is Active not Standard Pricebook.
Bad Trigger:

trigger updateitafter on Product2 (after update) {

List toUpdate = new List();
for(Product2 p:Trigger.new){

Product2 existingProduct= Trigger.oldMap.get(p.Id);
//The SOQL statement below will give you an error
//because of the for loop..
for (PriceBookEntry li:[Select UnitPrice, Product2Id,
Pricebook2Id, Id From PricebookEntry
where Product2Id=:existingProduct.Id
and IsActive=false and UnitPrice!=0.00])
{
li.IsActive=true;
toUpdate.add(li);
}

update toUpdate;
}
}

The right way to do the trigger:
trigger updateitafter on Product2 (after update) {
List allids = new List();

List toUpdate = new List();
for(Product2 p:Trigger.new){

Product2 existingProduct= Trigger.oldMap.get(p.Id);
allids.add(existingProduct);
}

for(PriceBookEntry li:[Select UnitPrice,
Product2Id, Pricebook2Id, Id
From PricebookEntry where
Product2Id In :allids
and IsActive=false
and UnitPrice!=0.00])
{
li.IsActive=true;
toUpdate.add(li);
}
update toUpdate;
}

The Change:

  1. In apex or any database-centric Programming, a query should not be executed inside the loop.
  2. So getting all the ProductId's first and placing it into an array(list) then using that list placing it back into the query just by an IN statement makes it easy and within limits of salesforce.
  3. This would do two things:
    1. Execute a single query and loop through its results.
    2. Also, efficient coding and clean.
Hope this was helpful.
Thanks
View my Other Blogs:
VisualForce Made Easy

Salesforce Data Migration Made Easy