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
ReneRendReneRend 

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;
}
Deepa.B.AnkaliDeepa.B.Ankali

@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.

PremanathPremanath

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

ReneRendReneRend
Thanks @deepaAnika can not find any information that it would be running twice. and gain it is doing this only for the first reason of contact. not after that.
ReneRendReneRend
Hi Premanath, this is not helping with the problem.
Deepa.B.AnkaliDeepa.B.Ankali

@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.

PremanathPremanath

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

ReneRendReneRend
Hi @Premanath, when trying this code I get the follwoing error :

Error: Compile Error: unexpected token: public at line 1 column 0
Deepa.B.AnkaliDeepa.B.Ankali
@ReneRend,

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;
}
}