Hello,
I have an sync plugin for sending date to another system, and I create an new entity new_log to log the data sent or exception.
the main logical is that
try { logCenter.Init(crmservice)//to init logcenter by passing crm org service to it SendData(); logCenter.Trace("OK","blablabal"); }catch(ex) { logCenter.Trace("Failed","blablabal"); throw new InvalidPluginExecutionException(); }
the problem is that as the plugin is registered on sync mode, when there is an exception, the log can not be created by logCenter, I think it was created but then was rollback by the plugin? Do you have any idea if I want to write the log by create a new crm record?
will it be OK if I make a new ACTION dedicated for the log?
Thanks
*This post is locked for comments
Hi,
I also have plugin registered on Post Create and I am able to create exception records. I have created default functions which I call them whenever an error occurs and it write details into custom entity.
Here is snap of my default function.
/// <summary>
/// Function to handle exceptions
/// </summary>
/// <param name="ex"></param>
/// <param name="target"></param>
/// <param name="organizationService"></param>
public static void HandleFaultException(FaultException<OrganizationServiceFault> ex, string externalReferenceNumber, IOrganizationService organizationService, string traceText = "")
{
ErrorInfo eI = new Common.ErrorInfo();
eI.ExternalReferenceNumber = externalReferenceNumber;
eI.InnerException = ex.InnerException != null ? ex.InnerException.Message : String.Empty;
if (String.IsNullOrWhiteSpace(eI.InnerException))
{
eI.InnerException = ex.Detail != null && ex.Detail.InnerFault != null ? ex.Detail.InnerFault.Message : String.Empty;
}
eI.StackTrace = ex.StackTrace;
eI.ErrorMessage = ex.Message;
eI.DetailMessage = ex.Detail != null ? ex.Detail.Message : String.Empty;
eI.Trace = ex.Detail != null ? ex.Detail.TraceText : String.Empty;
if (String.IsNullOrWhiteSpace(eI.Trace))
{
eI.Trace = traceText;
}
Exception(eI, organizationService);
}
/// <summary>
/// Function to create exception records
/// </summary>
/// <param name="eI"></param>
/// <param name="organizationService"></param>
public static void Exception(ErrorInfo eI, IOrganizationService organizationService)
{
Entity formSubmissionException = new Entity(FormsSubmissionException.ENTITYNAME);
formSubmissionException[FormsSubmissionException.PCS_DETAILMESSAGE] = eI.DetailMessage.Length > 4000 ? eI.DetailMessage.Substring(0, 3999) : eI.DetailMessage;
formSubmissionException[FormsSubmissionException.PCS_INNEREXCEPTION] = eI.InnerException.Length > 4000 ? eI.InnerException.Substring(0, 3999) : eI.InnerException; ;
formSubmissionException[FormsSubmissionException.PCS_MESSAGE] = eI.ErrorMessage.Length > 4000 ? eI.ErrorMessage.Substring(0, 3999) : eI.ErrorMessage;
formSubmissionException[FormsSubmissionException.PCS_NAME] = eI.ExternalReferenceNumber;
formSubmissionException[FormsSubmissionException.PCS_STACKTRACE] = eI.StackTrace.Length > 4000 ? eI.StackTrace.Substring(0, 3999) : eI.StackTrace;
formSubmissionException[FormsSubmissionException.PCS_TRACE] = eI.Trace.Length > 4000 ? eI.Trace.Substring(0, 3999) : eI.Trace;
ExecuteMultipleRequest multipleRequest = new ExecuteMultipleRequest();
multipleRequest.Requests = new OrganizationRequestCollection();
multipleRequest.Requests.Add(new CreateRequest() { Target = formSubmissionException });
multipleRequest.Settings = new ExecuteMultipleSettings()
{
ContinueOnError = true,
ReturnResponses = false
};
organizationService.Execute(multipleRequest);
throw new InvalidPluginExecutionException(eI.ErrorMessage);
}
Hope it will help you.
Happy CRM ING!!!!
Then I think you can't do this in plugin. You will have to move your code from plugin to custom workflow activity with the some string output parameter say "Returned Message" and then use that within the workflow to identify if the execution is sucessfull or not by returning the exception in the Returned message parameter. Within the workflow, check if returned paramter has any value, if yes, then cancel the workflow execution and display the error message returned by the custom workflow.
Hope this helps.
Hi, I have tried it yesterday and it was rollbacked, I make a custom action and call it just before throwing the exception. it was rollbacked for post and pre-operation but OK for pre-validation...
thanks
Then you can try with custom action.
Hi, we have to register the plugin in post-operation, I forgot to point it. so change it to pre-validation state is not OK for me.
Thanks
Hi Pranav,
We have already tried the Finally block but it will be rollbacked...Thanks
And since no one answered your last question: Yes making a new Action for log registration and calling that from your plugin would work since the action is not executed in your pipeline and thus not part of the rollback.
Hi,
Yes you are correct. If you throw the exception from plugin, it will rollback the database transaction. However you can register your plugin on PreValidation stage which is outside of the database transaction so any operation performed here will persists.
You can read more about stages here: docs.microsoft.com/.../gg327941(v=crm.8)
Hope this helps.
Hi ,
Please find below reference how to enable plugin trace in dynamics CRM.
www.powerobjects.com/.../debugging-your-plug-ins-with-the-plug-in-trace-log
Please check below link.
If found useful, please mark answer as verified.
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6