You need to sign in to do that
Don't have an account?
after update trigger creates duplicate value to related list if related list is empty
I am trying to create a trigger that would add a new value to a related list if a value is adeded to a field.
The field is called Reason of Contact (ROC) and when ever a new reason of contact is added or the field is populated it brings the new value to the related list in the Case. This is working correctly but when ever the first reason of contact is added to the case this value is entered twice. But after that everything works fine. So can someone see what is wrong with the code so that it would not create a duplicate value if the list for Reason Of Contacts (ROCs) is empty.
// trigger createROCchildonCaseUpdate on Case (after update) { // Create an empty list of String List<Reasons_of_Contact__c> ROCs = new List<Reasons_of_Contact__c>(); //For each case processed by the trigger, add a new //ROC record for the specified case. //Note that Trigger.New is a list of all the new positions //that are being created. for (Case newCase : Trigger.new) { Case oldCase = Trigger.oldMap.get(newCase.Id); if (oldCase.Request_3__c != newCase.Request_3__c | oldCase.Request_3__c != NULL) { //Create the new ROC in the related list with the following values ROCs.add(new Reasons_of_Contact__c( ProgramCaseLink__c = newCase.Id, Type__c = newCase.Request_3__c, ProgramCommentLookup__c = newCase.Program__c)); } } insert ROCs; }
@ReneRend,
Please check in debug if the trigger is being called twice. May be the trigger is going in recursion which is why it is created twice.
Here
if (oldCase.Request_3__c != newCase.Request_3__c | oldCase.Request_3__c != NULL) {
}
instead of above try below code
if (oldCase.Request_3__c != newCase.Request_3__c && oldCase.Request_3__c != NULL) {
}
Your code in not wrong may be trigger recursion happening there.... Tye with using and operator... you may avoid duplicates.
prem
@ReneRend,
You can see in Debug, if the DML is being performed twice by searching for 'Op:' in debug statements. If there is recursion, we can make use of recursion helper and solve the issue. Please check and let me know if it is due to recursion.
Hi,
Yes ,As per Deepa Said you can check with the Debug logs...
If it is firing twice Use StaticFlag Class
public class staticFlag {
public static boolean varble= true;
}
---------------------------
trigger createROCchildonCaseUpdate on Case (after update) {
if(staticFlag.varble){
staticFlag.varble= false;
-------Here your code----
}
}
prem
Error: Compile Error: unexpected token: public at line 1 column 0
Try this for a recursion helper class :
public with sharing class SL_RecursionHelper
{
///This boolean will be set to true by default
public static boolean isRecursive = true;
public static boolean getisRecursive()
{
///return the isRecursive variable to the calling method.
return isRecursive;
}
}