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
The new LearnerThe new Learner 

Need to stop trigger to fire while creating new records

Hi Experts,

Below trigger is firing for new records as well but i dont want to be fired for new records which i am creating i need only for update. Kindly help me out..

Trigger Trigger_Case on Case(before Update){
    CaseHandler.updateObjectField(Trigger.New, Trigger.oldMap);
}

public class CaseHandler{
    static Map<String, Schema.SObjectField> schemaFieldMap = Schema.SObjectType.Case.fields.getMap();
    static Map<String, Case> queriedFieldValues = new Map<String, Case>();
    public static void updateObjectField(List<Case> lstObject, Map<Id, Case> oldMap){
        for(Case obj : lstObject){
            for (String fieldName: schemaFieldMap.keySet()) {
                if(obj.get(fieldName) != oldMap.get(obj.Id).get(fieldName)){
                    obj.put('Status', 'New');
                }
            }
        }
    }
}
Best Answer chosen by The new Learner
Rajesh_ShahRajesh_Shah
Are you sure this trigger is firing on Case insert? As per the trigger event, it should only fire for update. Maybe you have a workflow that updates a field on case that fires this trigger. 

If that is case, you would need to use a static variabe to control the trigger flow. Create a static boolean variable in the CaseHandler. Lets call it 'DontFireTrigger'.
static boolean DontFireTrigger = FALSE;
Update your trigger to also include the Before Insert event. Add logic in trigger to check if its a before insert event. If yes, set the boolean variable to true. 
if(trigger.isInsert) {
     CaseHandler.DontFireTrigger = true;
}
In before update case, call the caseHandler function only if variable = false.
if(trigger.isUpdate && !caseHandler.DontFireTrigger) {
     // Call your handler function.
}

Let me know if this helps you.

All Answers

Rajesh_ShahRajesh_Shah
Are you sure this trigger is firing on Case insert? As per the trigger event, it should only fire for update. Maybe you have a workflow that updates a field on case that fires this trigger. 

If that is case, you would need to use a static variabe to control the trigger flow. Create a static boolean variable in the CaseHandler. Lets call it 'DontFireTrigger'.
static boolean DontFireTrigger = FALSE;
Update your trigger to also include the Before Insert event. Add logic in trigger to check if its a before insert event. If yes, set the boolean variable to true. 
if(trigger.isInsert) {
     CaseHandler.DontFireTrigger = true;
}
In before update case, call the caseHandler function only if variable = false.
if(trigger.isUpdate && !caseHandler.DontFireTrigger) {
     // Call your handler function.
}

Let me know if this helps you.
This was selected as the best answer
The new LearnerThe new Learner
Apart from before update there are before insert, after insert and after update alps there so what I am doing is that, I am using putting if condition and adding conditions called before and is update and calling that method, but I don't why it's firing for new records which I am creating
Rajesh_ShahRajesh_Shah
I guess then there would be some action in after insert trigger (or probably a workflow or process builder) that updates the same case. Can you check what is the value of trigger.isUpdate and trigger.IsBefore in the CaseHandler updateObjectField function?
The new LearnerThe new Learner
This is new logic which is implemented recently
Rajesh_ShahRajesh_Shah
I suggest you try the static variable functionality to control it. I believe it should solve the problem.
The new LearnerThe new Learner
Can you help me once again to amend that code in it pls
Ajay K DubediAjay K Dubedi
Hi,

Below code can fulfill your requirements. Hope this will work for you.
trigger CaseTrigg on Case (before update){
    if(trigger.isbefore)
    {
        CheckCase.withOldValues(trigger.New,trigger.oldMap);
    }
}

public class CheckCase {
    public static void withOldValues(List<Case> caseList, Map<Id,Case> idVsCaseMap)
    {
        for(Case caseObj : caseList)
        {
            // as you have not mention any field name so i have 'Type' as example
            // any change in the phone is detected by the the below if condition
            if(idVsCaseMap.get(caseObj.id).Type != caseObj.Type)
            {
                caseObj.Status = 'New';
            }
        }
    }
}

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi
The new LearnerThe new Learner
Hi Ajay, I need all the field which are there on the case
The new LearnerThe new Learner
Hi Ajay,

Below error is coming while creating new reocrd.
execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object for this line "CheckCase.withOldValues(trigger.New,trigger.oldMap);"
Ajay K DubediAjay K Dubedi
Hi,

Below code can fulfill your requirements. Hope this will work for you.
 
trigger CaseTrigg on Case (before update) {
    
    if(trigger.isbefore)
    {
        CheckCase.withOldValues(trigger.New,trigger.oldMap);
    }
}


public class CheckCase {
    public static void withOldValues(List<Case> caseList, Map<Id,Case> idVsCaseMap)
    {
        for(Case caseObj : caseList)
        {
            // as you have not mention any field name so i have 'Type' as example
            // any change in the phone is detected by the the below if condition
            if(idVsCaseMap.get(caseObj.id).Type != caseObj.Type)
            {
                caseObj.Status = 'New';
            }
        }
    }
}

this trigger only for update the records. this trigger fired on only before update, not before insert.

Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi