public with sharing class CaseTriggerHandler implements ITrigger{
List<Case_Call__c> ccList = new List<Case_Call__c>();
List<Case> newCaseToEmail = new List<Case>();
List<Case> escalatedCasesToEmail = new List<Case>();
List<Case> closedCasesToEmail = new List<Case>();
RecordType custReqRecType = new RecordType();
RecordType retradeReqRecType = new RecordType();
// Used to store the string that will need to get added
// Into the text field 'Snippet_Information__c'
Map<Id,String> snippetIdMap = new Map<Id,String>();
//Used to store resolution emails as Kinnser Email Messages in SF
List<Kinnser_Email_Message__c> kemResolutions = new List<Kinnser_Email_Message__c>();
public void bulkBefore(){
if(Trigger.isInsert || Trigger.isUpdate){
Set<Id> parentSnippetIds = new Set<Id>();
for(Sobject sob : Trigger.new){
Case c = (Case)sob;
If(c.Add_Snippet__c != NULL){
parentSnippetIds.add(c.Add_Snippet__c);
}
}
If(parentSnippetIds.size() > 0){
For(Snippet__c snip : [SELECT Id, Description__c FROM Snippet__c WHERE Id IN : parentSnippetIds]){
snippetIdMap.Put(snip.Id , snip.Description__c);
}
}
}
}
/**
* bulkAfter
*
* This method is called prior to execution of an AFTER trigger. Use this to cache
* any data required into maps prior execution of the trigger.
*/
public void bulkAfter(){
if(Trigger.isInsert || Trigger.isUpdate){
for(RecordType r : [SELECT Name, Id FROM RecordType WHERE Name LIKE 'Customer Requests' OR Name LIKE 'Retrade Request']){
If(r.Name == 'Retrade Request'){
retradeReqRecType = r;
} else if(r.Name == 'Customer Requests'){
custReqRecType = r;
}
}
}
}
/**
* beforeInsert
*
* This method is called iteratively for each record to be inserted during a BEFORE
* trigger. Never execute any SOQL/SOSL etc in this and other iterative methods.
*/
public void beforeInsert(SObject so){
Case currentCase = (Case)so;
if(currentCase.Subject != NULL){
currentCase.Subject_Encrypted__c = currentCase.Subject;
currentCase.Subject = NULL;
}
if(currentCase.Description != NULL){
currentCase.Encrypted_Description__c = currentCase.Description;
currentCase.Description = NULL;
}
// If the snippet lookup is not null, append the snippet text
// to the snippet information textbox
if(currentCase.Add_Snippet__c != NULL){
String snipText = ' ';
if(currentCase.Snippet_Information__c != NULL){
snipText += currentCase.Snippet_Information__c + '\n';
}
snipText += snippetIdMap.get(currentCase.Add_Snippet__c);
currentCase.Snippet_Information__c = snipText;
}
}
/**
* beforeUpdate
*
* This method is called iteratively for each record to be updated during a BEFORE
* trigger.
*/
public void beforeUpdate(SObject oldSo, SObject so){
Case currentCase = (Case)so;
Case oldCase = (Case)oldSo;
// If the snippet lookup has changed and is not null, append the snippet text
// to the snippet information textbox
if(currentCase.Add_Snippet__c != NULL && currentCase.Add_Snippet__c != oldCase.Add_Snippet__c){
String snipText = ' ';
if(currentCase.Snippet_Information__c != NULL){
snipText += currentCase.Snippet_Information__c + '\n';
}
snipText += snippetIdMap.get(currentCase.Add_Snippet__c);
currentCase.Snippet_Information__c = snipText;
}
}
/**
* beforeDelete
*
* This method is called iteratively for each record to be deleted during a BEFORE
* trigger.
*/
public void beforeDelete(SObject so){}
/**
* afterInsert
*
* This method is called iteratively for each record inserted during an AFTER
* trigger. Always put field validation in the 'After' methods in case another trigger
* has modified any values. The record is 'read only' by this point.
*/
public void afterInsert(SObject so){
Case currentCase = (Case)so;
if(currentCase.Origin == 'Phone'){
Case_Call__c casecall = new Case_Call__c();
casecall.Call_Subject__c = currentCase.Subject_Encrypted__c;
casecall.Case_Reference__c = currentCase.Id;
ccList.add(casecall);
}
if(currentCase.RecordTypeId == custReqRecType.Id
&& currentCase.Status == '01-New'
&& currentCase.Kinnser_closed_on_create__c == FALSE
&& currentCase.Email__c != NULL
&& currentCase.Do_Not_Send_Case_Email__c == FALSE){
newCaseToEmail.add(currentCase);
}
if(currentCase.RecordTypeId == retradeReqRecType.Id
//&& currentCase.Status == '01-New'
&& currentCase.Origin == 'Email'
&& currentCase.Kinnser_closed_on_create__c == FALSE
&& currentCase.Email__c != NULL
&& currentCase.Do_Not_Send_Case_Email__c == FALSE){
newCaseToEmail.add(currentCase);
}
}
public void afterUpdate(SObject oldSo, SObject so){
Case currentCase = (Case)so;
Case oldCase = (Case)oldSo;
if(currentCase.RecordTypeId == custReqRecType.Id
&& currentCase.Status == '90-Closed'
&& oldCase.Status != '90-Closed'
&& oldCase.Status != '40-Reopened'
//&& currentCase.Kinnser_closed_on_create__c == FALSE
&& currentCase.Do_Not_Send_Case_Email__c == FALSE
&& currentCase.Email__c != NULL){
closedCasesToEmail.add(currentCase);
Kinnser_Email_Message__c kem = new Kinnser_Email_Message__c();
kem.ToAddress__c = currentCase.Email__c;
kem.Case__c = currentCase.Id;
kem.Do_Not_Send__c = TRUE;
kem.Incoming__c = FALSE;
kem.ToName__c = currentCase.Calculated_Contact_Name__c;
kem.Subject__c = 'Kinnser case resolved: ' + currentCase.CaseNumber;
kem.TextBody__c = currentCase.Encrypted_Description__c;
kem.TextBody__c += 'Dear ' + currentCase.Calculated_Contact_Name__c + '\n';
kem.TextBody__c += 'Please note that your case ' + currentCase.CaseNumber +' has been resolved.\n';
kem.TextBody__c += 'Resolution:'+ currentCase.Resolution_Summary__c +'\n';
kem.TextBody__c += 'Please contact Kinnser Support if you continue to experience any difficulties or have any questions.';
kemResolutions.add(kem);
}
}
public void afterDelete(SObject so){}
public void andFinally(){
if(ccList.size()>0){
insertCaseCalls(JSON.serialize(ccList));
}
if(closedCasesToEmail.Size() > 0){
emailClosedCase(JSON.serialize(closedCasesToEmail));
}
if(newCaseToEmail.Size() > 0){
emailNewCase(JSON.serialize(newCaseToEmail));
}
if(kemResolutions.Size() > 0){
Insert kemResolutions;
}
}
@future(callout=TRUE)
public static void emailNewCase(String jsonNewCases){
SendGridEmail seg = new SendGridEmail();
seg.GenNewCase(jsonNewCases);
seg.SendEmail();
}
@future(callout=TRUE)
public static void emailClosedCase(String jsonClosedCases){
SendGridEmail seg = new SendGridEmail();
seg.GenCloseCase(jsonClosedCases);
seg.SendEmail();
}
@future
public static void insertCaseCalls(String jsonCaseCallList){
List<Case_Call__c> caseCalls = (List<Case_Call__c>)JSON.deserialize(jsonCaseCallList, List<Case_Call__c>.class);
if(!Test.isRunningTest()){
Insert caseCalls;
}
}
}
-
Hm. Can you explain more about querying the field? Not sure if I am doing that or how to do it. I have permission to view, yes.– HeatherSep 19, 2017 at 16:59
-
I posted the whole class above. Thanks for your help!– HeatherSep 20, 2017 at 15:16
-
Since it is coming from a trigger context, it should be there. So I'm leaning towards permission to the field?– JRiffeSep 21, 2017 at 15:08
Add a comment
|