function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WEIdataWEIdata 

New Rollup Summary Creates Trigger Errors

Hello- As soon as I create a simple SUM rollup summary for a custom object, the trigger I created to insert records for said custom object stops working. Also, DemandTools stopped being able to merge duplicate records on the same custom object.

Here is the trigger:

Trigger createPages on Contact (after insert, after update) {
List<Pages__c> p = new List <Pages__c> ();
For (Contact newContact: Trigger.New)
If (newContact.Pages__c  > 0) {
p.add (new Pages__c (
Pages_Read__c = newContact.Pages__c,
Reading_Detail__c = newContact.Pages_Detail__c,
Reader_Type__c = newContact.Reader_ID__c,
Contact_Name__c = newContact.Id ) ) ;
Insert p;
}
}
Try {
Insert pToInsert;
} catch (system.Dmlexception e) {
System.debug (e);
}
}
Best Answer chosen by WEIdata
bob_buzzardbob_buzzard
Can you explain what "stop working" means - did you get errors?   I'd expect the error to be around recursion - each time you add a page tot he contact, that will change the rollup summary field and update the contact, causing your trigger to fire again, which adds a new page etc.

There are a number of techniques to avoid this - if you can determine that you have already got a pages__c record with the same details you could drop out of the trigger, or you could set a static variable to indicate that you have already executed this trigger, as detailed at:

http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

All Answers

bob_buzzardbob_buzzard
Can you explain what "stop working" means - did you get errors?   I'd expect the error to be around recursion - each time you add a page tot he contact, that will change the rollup summary field and update the contact, causing your trigger to fire again, which adds a new page etc.

There are a number of techniques to avoid this - if you can determine that you have already got a pages__c record with the same details you could drop out of the trigger, or you could set a static variable to indicate that you have already executed this trigger, as detailed at:

http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
This was selected as the best answer
WEIdataWEIdata
Thank you so much, Bob. You hit the nail on the head. I just got around to trying this today and it was magnificently simple and effective.