Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to create a log when throw InvalidPluginExecutionException in a sync plugin

(0) ShareShare
ReportReport
Posted on by 91

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

  • Suggested answer
    talk2.gauravb Profile Picture
    120 on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    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!!!!

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    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.

  • Song Profile Picture
    91 on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    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

  • RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    Then you can try with custom action.

  • Song Profile Picture
    91 on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    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

  • Song Profile Picture
    91 on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    Hi Pranav,

    We have already tried the Finally block but it will be rollbacked...Thanks

  • Daniel Wikell Profile Picture
    2,360 on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    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.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    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.

  • gdas Profile Picture
    50,091 Moderator on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    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

  • Suggested answer
    MS CRM DYNAMICS Profile Picture
    592 on at
    RE: How to create a log when throw InvalidPluginExecutionException in a sync plugin

    Please check below link.

    https://www.inogic.com/blog/2017/03/plugin-pre-validation-operation-to-show-an-error-message-as-well-as-log-the-error/

    If found useful, please mark answer as verified.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans