We want to insert an error log record in the org whenever we receive any error in trigger execution but we want to do it by using platform event. We have tried to insert a record but were not able to do that. The code is executed fine but the record is not created or inserted.
The platform event is getting processed but not published.
Code is attached below :-
CalloutError__e errorEvent = new CalloutError__e(
Description__c = Description,
Error_Stack_Line__c = Error_Stack_Line,
Error_Type__c = Error_Type
);
Error_Log__c errlog1 = new Error_Log__c();
errlog1.Description__c = Description;
errlog1.Error_Stack_Line__c = Error_Stack_Line;
errlog1.Error_Type__c = 'Other';
System.debug('errlog1 '+errlog1);
insert errlog1;
System.debug('errlog1'+errlog1.Id);
//Database.SaveResult result1 = EventBus.publish(errlog1);
Database.SaveResult result = EventBus.publish(errorEvent);
And called this function from the trigger catch block
CalloutError__e errlog = new CalloutError__e();
errlog.Description__c = 'Task trigger exception - '+e.getCause() +' \n'+ e.getMessage();
errlog.Error_Stack_Line__c = e.getStackTraceString();
errlog.Error_Type__c = 'Other';
errlist.add(errlog);
CalloutErrorService errorService = new CalloutErrorService();
errorService.handleCalloutErrorEvent(errlist);
errorService.persistError( errlog.Description__c,errlog.Error_Stack_Line__c,errlog.Error_Type__c);
Please help us to solve this issue.