You need to sign in to do that
Don't have an account?
Calvin Moodley 11
Trigger Creates Multiple Records
Hi All,
I have created a trigger that when an order is in the Status[Order Submitted] and the create case field is true i want a case to be created.
However there are multiple cases being created against the record
Please see below triger and classes. :
My Trigger
triggerHandler
triggerFunction
trigger caseOrder on ccrz__E_Order__c (after insert, after update) {
if(trigger.isInsert){
caseOrderTriggerHandler.isAfterInsert(Trigger.new);
}
if(trigger.isUpdate){
caseOrderTriggerHandler.isAfterInsert(Trigger.new);
}
}
public class caseOrderTriggerHandler {
public static void isAfterInsert (List<ccrz__E_Order__c>ords) {
caseOrderTriggerFunction.createCase(ords);
}
}
public class caseOrderTriggerFunction {
public static void createCase(List<ccrz__E_Order__c>ords){
Date ordCreatedDate = date.today();
List<ccrz__E_Order__c> todaysOrds = [SELECT id, ccrz__OrderStatus__c, Create_Case__c,ccrz__OrderDate__c,ccrz__Contact__c,ccrz__Account__c
FROM ccrz__E_Order__c
WHERE id IN :ords
AND ccrz__OrderDate__c = :ordCreatedDate];
List<Case> myCase = new List <Case>();
for(ccrz__E_Order__c myOrds : todaysOrds) {
if(myOrds.ccrz__OrderStatus__c == 'Order Submitted' && myOrds.Create_Case__c == true ) {
case c = new case();
c.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('General Enquiry').getRecordTypeId();
c.Reason = 'General Enquiry';
c.Case_Reason__c ='orders';
c.Status = 'New';
c.Subject = 'order Submitted status';
c.Case_Sub_Reason__c ='Incomplete Order Creation';
c.Description ='Order Id: '+myOrds.Id+'related to account Id: '+myOrds.ccrz__Account__c+' has failed in SAP';
c.ContactId = myOrds.ccrz__Contact__c;
c.AccountId = myOrds.ccrz__Account__c;
myCase.add(c);
}
}
if(mycase.size() == 1 ) {
insert myCase;
}
}
}
I have created a trigger that when an order is in the Status[Order Submitted] and the create case field is true i want a case to be created.
However there are multiple cases being created against the record
Please see below triger and classes. :
My Trigger
triggerHandler
triggerFunction
trigger caseOrder on ccrz__E_Order__c (after insert, after update) {
if(trigger.isInsert){
caseOrderTriggerHandler.isAfterInsert(Trigger.new);
}
if(trigger.isUpdate){
caseOrderTriggerHandler.isAfterInsert(Trigger.new);
}
}
public class caseOrderTriggerHandler {
public static void isAfterInsert (List<ccrz__E_Order__c>ords) {
caseOrderTriggerFunction.createCase(ords);
}
}
public class caseOrderTriggerFunction {
public static void createCase(List<ccrz__E_Order__c>ords){
Date ordCreatedDate = date.today();
List<ccrz__E_Order__c> todaysOrds = [SELECT id, ccrz__OrderStatus__c, Create_Case__c,ccrz__OrderDate__c,ccrz__Contact__c,ccrz__Account__c
FROM ccrz__E_Order__c
WHERE id IN :ords
AND ccrz__OrderDate__c = :ordCreatedDate];
List<Case> myCase = new List <Case>();
for(ccrz__E_Order__c myOrds : todaysOrds) {
if(myOrds.ccrz__OrderStatus__c == 'Order Submitted' && myOrds.Create_Case__c == true ) {
case c = new case();
c.RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('General Enquiry').getRecordTypeId();
c.Reason = 'General Enquiry';
c.Case_Reason__c ='orders';
c.Status = 'New';
c.Subject = 'order Submitted status';
c.Case_Sub_Reason__c ='Incomplete Order Creation';
c.Description ='Order Id: '+myOrds.Id+'related to account Id: '+myOrds.ccrz__Account__c+' has failed in SAP';
c.ContactId = myOrds.ccrz__Contact__c;
c.AccountId = myOrds.ccrz__Account__c;
myCase.add(c);
}
}
if(mycase.size() == 1 ) {
insert myCase;
}
}
}
Your code is fine, just a few mistakes that you have made and I've corrected those. Please, use the below code and try if this works:
If you find the above solution helpful, then mark it as the best answer so as to help others too.
Regards & Thanks,
Suraj
I have tested , The result is creating 6 cases for one order
Yes, Actually that's happening because the for loop will run based on the value it gets from the list variable (List<ccrz__E_Order__c> todaysOrds).
So it's obvious that It's going to create that many cases. Now a simple possible way to prevent this from happening would be to LIMIT the query to return just 1 record. But again, this would not be a good solution.
Also please do a cross-check like what exactly you are getting inside this variable (List<ccrz__E_Order__c> todaysOrds), coz if your creating one order then the loop shouldn't run more than once.
Also, kinda request if you could explain the whole scenario like what's exactly you are trying to achieve, and what all possibilities you are assuming that we need to consider.
trigger caseOrder on ccrz__E_Order__c (after insert, after update) {
if(trigger.isInsert && trigger.isAfter){
caseOrderTriggerHandler.isAfterInsert(Trigger.new);
}
if(trigger.isUpdate && trigger.isAfter){
caseOrderTriggerHandler.isAfterInsert(Trigger.new);
}
}
Main Reason , 6 order has been created in your org for same date that's why
Please debug this code in Anonymous window
Date ordCreatedDate = date.today();
List<ccrz__E_Order__c> todaysOrds = [SELECT id, ccrz__OrderStatus__c, Create_Case__c,ccrz__OrderDate__c,ccrz__Contact__c,ccrz__Account__c
FROM ccrz__E_Order__c
WHERE id IN :ords
AND ccrz__OrderDate__c = :ordCreatedDate]
system.debug('todaysOrds -->>>>>> '+todaysOrds.size());
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Thank you for responding. The Scenario is as follows : When an order is placed and the order status is set to Order Submitted Then the workflow rule sets the Create Case field to true.
when the Create case field is True , this should create a case for the specific order..
Thank you for your help, i will try this out now and revert.
I have debugged the logic and i see there is 5 transactions, which is the reason for so many cases being created.