+ Start a Discussion
Davinder Kaur 8Davinder Kaur 8 

Trigger working on Sandbox but not working on production

I wrote a trigger to create a new case when a closed case receives a mail. It works absolutely fine on sandbox but not working on production.

trigger EmailMessageAfterUpdate on EmailMessage (after insert) {
   
// limit 1001 used, else it was giving error "System.LimitException: Too many query rows: 50001".

   List<case> caseList = [Select id, Status from Case where Status = 'Closed' limit 1001];
            for (EmailMessage newEmail : Trigger.new) 
            {     for(case cc : caseList){
                       if(cc.Status == 'Closed' && cc.id == newEmail.ParentId){ 
                            Case newCase = new Case();
                            newCase.Subject = newEmail.Subject;
                            newCase.Parent_Case__c=newEmail.ParentId;
                            newCase.Category__c = newEmail.Parent.Category__c;
                            newCase.BusinessHours = newEmail.Parent.BusinessHours;
                            newCase.Status = 'Re-Opened';
                            newCase.Sub_Status__c = newEmail.Parent.Sub_Status__c;
                            insert newCase;
                         }
                   }
             }
}
Best Answer chosen by Davinder Kaur 8
BalajiRanganathanBalajiRanganathan
what is the error you are getting?

you can try below code which is more optimized
trigger EmailMessageAfterUpdate on EmailMessage (after insert) {
   
  Set<Id> caseIds = new Set<Id>();
  for (EmailMessage newEmail : Trigger.new) {
    caseIds.add(newEmail.parentId);
  }

   Map<id, case> caseMap = new Map<id, case>([Select id, Status from Case where Status = 'Closed' and id in :caseIds ]);

  List<Case> newCases = new List<Case>();

  for (EmailMessage newEmail : Trigger.new) {
    Case oldCase = caseMap.get(newEmail.parentId);
    if (oldCase != null) {
      Case newCase = new Case(); 
      newCase.Subject = newEmail.Subject;
      newCase.Parent_Case__c = newEmail.ParentId;
      newCase.Category__c = newEmail.Parent.Category__c; 
      newCase.BusinessHours = newEmail.Parent.BusinessHours;
      newCase.Status = 'Re-Opened';
      newCase.Sub_Status__c =   newEmail.Parent.Sub_Status__c;
      newCases.add(newCase);
    }
  }
  insert newCases;
}

 

All Answers

BalajiRanganathanBalajiRanganathan
what is the error you are getting?

you can try below code which is more optimized
trigger EmailMessageAfterUpdate on EmailMessage (after insert) {
   
  Set<Id> caseIds = new Set<Id>();
  for (EmailMessage newEmail : Trigger.new) {
    caseIds.add(newEmail.parentId);
  }

   Map<id, case> caseMap = new Map<id, case>([Select id, Status from Case where Status = 'Closed' and id in :caseIds ]);

  List<Case> newCases = new List<Case>();

  for (EmailMessage newEmail : Trigger.new) {
    Case oldCase = caseMap.get(newEmail.parentId);
    if (oldCase != null) {
      Case newCase = new Case(); 
      newCase.Subject = newEmail.Subject;
      newCase.Parent_Case__c = newEmail.ParentId;
      newCase.Category__c = newEmail.Parent.Category__c; 
      newCase.BusinessHours = newEmail.Parent.BusinessHours;
      newCase.Status = 'Re-Opened';
      newCase.Sub_Status__c =   newEmail.Parent.Sub_Status__c;
      newCases.add(newCase);
    }
  }
  insert newCases;
}

 
This was selected as the best answer
kiranmutturukiranmutturu
try this in sandbox and it works you can deploy this to production
 
trigger EmailMessageAfterUpdate on EmailMessage (after insert) {
   
	for (EmailMessage newEmail : Trigger.new)
	{
		if (Schema.Case.SObjectType == newEmail.ParentId.getSobjectType())
		{
			emailToCaseMap.put(newEmail.ParentId, newEmail);
		}
		
	}

	List<case> newcaseList = new List<case>();

	for(case cc : [Select id from Case where id in :emailToCaseMap.keySet() and Status = 'Closed'])
	{
			EmailMessage newEmail = emailToCaseMap.get(cc.Id);
			Case newCase = new Case();
			newCase.Subject = newEmail.Subject;
			newCase.Parent_Case__c=newEmail.ParentId;
			newCase.Category__c = newEmail.Parent.Category__c;
			newCase.BusinessHours = newEmail.Parent.BusinessHours;
			newCase.Status = 'Re-Opened';
			newCase.Sub_Status__c = newEmail.Parent.Sub_Status__c;
			newcaseList.add(newCase);
	}

	insert newcaseList;
}

 
Davinder Kaur 8Davinder Kaur 8
Thanks for the reply.. I will try  these codes on my Org.. However can anyone tell me the reason why it is not working on production.
It does not give any error but it is not creating any new case now... Earlier it was working fine on production but now it have stopped working.
kiranmutturukiranmutturu

Couple of things you can take in to count..

1. there may be no closed cases at all in production or
2. There may be no closed case realted to the incoming email.

And yes code given by @BalajiRanganathan is much optimized than what I written.....  

I think I am too much thinking while writing the code for you...


 

Davinder Kaur 8Davinder Kaur 8
Thanks BalajiRanganathan.. your answer has solved my problem..