You need to sign in to do that
Don't have an account?
The 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');
}
}
}
}
}
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');
}
}
}
}
}
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'. 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. In before update case, call the caseHandler function only if variable = false.
Let me know if this helps you.
All Answers
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'. 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. In before update case, call the caseHandler function only if variable = false.
Let me know if this helps you.
Below code can fulfill your requirements. Hope this will work for you.
Please mark this as best answer if this solves your problem.
Thank you,
Ajay Dubedi
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);"
Below code can fulfill your requirements. Hope this will work for you.
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