I am trying to create a EventLog (Custom object) record when ever a exception is caught. It works fine only when the system exception occurs (Like any DML operation. In my case If I call Update instead of Insert in try block). How can I catch the custom exception and store it as a record in Eventlog. Below is the code that I am using. Here I am checking if all the fields received from External system are correct and not Null. And if the value of the fields like Ids have 18 characters. If so, then I am showing a custom error as response.
I want to catch all the errors like, If the AccountId has less than 18 characters, catch that as exception with the field name and create a record in EventLog object. Any help would be really appreciated.
@RestResource(urlMapping='/cases/V1/caseservice/')
global with sharing class RESTWebservice
{
@HttpPost
global static String createNewCase() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
createNewCase response= new createNewCase();
Boolean MissingData=False;
Boolean Dataexception=False;
// Convert the request body to string.
string request = req.requestbody.tostring();
// Deserialize the JSON request that is received using the inner class.
IncomingRequest R = (IncomingRequest)JSON.deserialize(request, IncomingRequest.Class);
if(R.sfclientid==null || R.sfaccountid==null)
{
MissingData=True;
}
if(R.sfclientid.length()<18 || R.sfaccountid.length()<18)
{
Dataexception=True;
}
Case c = new Case();
Map<string, DFM_Js__c> cs = DFM_Js__c.getAll();
List<String> Formnames = new List<String>();
Formnames.addAll(cs.keySet());
for(string dm:Formnames)
{
DFM_Js__c fn=cs.get(dm);
system.debug('** MissingData before assigning values to case fields' + MissingData);
system.debug('**Dataexception before assigning values to case fields ' + Dataexception);
if(Dataexception==False || MissingData==False)
{
// COmpare the documenttype field value received with Form Name field in custom settings.
if(R.documenttype==fn.FormName__c)
{
c.Status=fn.Case_Status__c;
c.Subject = R.Subject;
c.AccountId__c=R.sfaccountid;
}
}
}
try
{
if(MissingData && Dataexception)
{
res.statusCode=400;
response.message='Required Fields are missings and Wrong Ids Received';
}
else if(MissingData)
{
res.statusCode=400;
response.message='Missing Data - Required Fields are missings';
}
else if(Dataexception)
{
res.statusCode=400;
response.message='Data Exception - Received wrong Ids';
}
else
{
Insert c;
res.statusCode=201;
response.status = 'Success';
response.message = 'Case created successfully with case ID'+' '+ c.Id;
}
}
catch(Exception ex)
{
// create a record in Event Log object if there is any error occuring when creation of case.
CreateErrorMesage(c,'Case','RESTWebservice','RESTWebservice.createNewCase()',ex);
}
return response.message;
}
// Create a record in Error Log object if there is any error occured when creating of case record.
public static void CreateErrorMesage(SObject acc, String ObjName, string EventType, string ProcessName, Exception ex){
try{
string errormessage;
Event_Log__c eventlog = new Event_Log__c();
eventlog.ObjectName__c= ObjName;
eventlog.ObjectId__c = acc.Id;
eventlog.EventType__c = EventType;
eventlog.ProcessName__c=ProcessName;
errormessage = ex.getTypeName() + ': ' + ex.getCause() +' At Line'+ ex.getLineNumber() + 'Stack Trace:-'+ex.getStackTraceString();
if(errormessage.length() < 32000) eventlog.StackTrace__c = errormessage;
else eventlog.StackTrace__c = errormessage.SubString(0,32000);
eventlog.ErrorMessage__c = ex.getMessage();
eventlog.UserInfo__c = Userinfo.getUserId()+ '|' +Userinfo.getFirstName();
insert eventlog;
} catch(Exception e){}
}
public class incomingrequest{
public string sfclientid;
public string sfaccountid;
incomingrequest(){}
}
global class createNewCase {
public String status;
public String message;
public createNewCase(){
}
}
}