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
closenetclosenet 

workflow field update and causing insert twice in the trigger

Hi Guys,

 

I have a dazzling issue when inserting a record triggered with After update trigger

bascially I want to create a child record when the user tick the complete box in the parent form.

 

I managed to do create the trigger, however the record gets created twice, after investigating the issue realised that I have a workflow update on the complete tick box Completed__c which set the complete date Completed_Date__c if the complete ticked Completed__c

 

The question is how can I keep the workflow and the trigger works in the same time without causing the trigger to create the same record twice

 

to understand my point more,  read point 11 in the below link

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

 

Thanks for your help in advance

 

 

//Below is the code for the trigger...
 
 
trigger setNewPoint_Penalty on Activity__c (before update) {

List <User_Points__c> newPoint = new List <User_Points__c> ();

isMinusValue MyNegClass = new isMinusValue ();

for (Activity__c a: trigger.new)
{
List <Activity__c> actLst = [SELECT Id, Name, CreatedDate, CreatedById, Assigned_To__c, Completed_Date__c, ActivityTypeId__c,
Completed__c, Cancelled__c, Required_Date__c, Cancelled_Date__c, Status__c, Details__c 
FROM Activity__c where id =: a.id];
List <User_Points__c> pointsExist = [SELECT Id, OwnerId, Points__c, Member__c, ProgressRel__c, Activity__c, Notes__c 
FROM User_Points__c
where Activity__c = : a.id ] ;


integer NegTotal = MyNegClass.penaltiy(actLst);

for (User_Points__c p: pointsExist)
{
if (p.Points__c > 0 && actLst.get(0).Completed__c && NegTotal <> 0)
{


newPoint.add (new User_Points__c (
 Activity__c = actLst.get(0).id
,Points__c = NegTotal // this is to minus 2 point as a penalty
, Member__c = actLst.get(0).Assigned_To__c
, ProgressRel__c = null 
) ); 

}
else
{
continue;
}

}
insert newPoint;
}

}
 

 

Best Answer chosen by Admin (Salesforce Developers) 
Satish_SFDCSatish_SFDC

Hi,

In this case you would have to use a static boolean variable to check if the code has already been executed.

Basically, you would set the boolean variable to false initially and upon the code execution you set that to true. The next time around, it will check and condition and not execute the code.

 

The following link may help you.

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

Regards,

Satish Kumar


Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

All Answers

Satish_SFDCSatish_SFDC

Hi,

In this case you would have to use a static boolean variable to check if the code has already been executed.

Basically, you would set the boolean variable to false initially and upon the code execution you set that to true. The next time around, it will check and condition and not execute the code.

 

The following link may help you.

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

Regards,

Satish Kumar


Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

This was selected as the best answer
closenetclosenet

That's exactly what I was looking for , this is now sorted, 

Thank you very much

Eric KintzerEric Kintzer
The static variable solution will not work if the trigger is invoked when AllOrNone header = false (Data Loader for example) and one or more records in the batch fail. This is because SFDC will rollback any updates from the successes, and retry the records in the batch that succeeded. But the static variable is not reset in the retry and your trigger will then end up doing nothing and your database will be inconsistent.