Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin Exception - create record

Posted on by 1,540

CRM Online, latest version.

I have a scenario where I need to record the fact that an exception has been thrown. In this instance it's to record that a dodgy email has attempted to be entered against the contact. I have the plugin running sync on both create and update of contact.

In my body I'm checking whether the email address matches:

try{
throw new Exception("<h2>Invalid Email Domain</h2>");
}
catch (Exception ex)
{
    Entity eRecord = new Entity("test_emailexclusionlog");
    eRecord["test_name"] = (string)targetEntity["emailaddress1"];
    eRecord["ownerid"] = new EntityReference("systemuser", context.UserId);
    tracer.Trace("create log record #2");
    service.Create(eRecord);
    throw new InvalidPluginExecutionException(ex.Message);
}


but the log record isn't being created.

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin Exception - create record

    Try to follow this 

    http://crmtidbits.blogspot.com/2014/02/bypass-plug-in-transaction-to-log.html

    Regards

    Raffaele

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Plugin Exception - create record

    You can try moving that code to the pre-validation stage. Pre-validation normally runs outside of the database transaction, so it won't be rolled back.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin Exception - create record

    In case of a synchronous plugin you can directly show the message to the user so why do you want to record it? And if you really want to record how many times the wrong entry was made, I would suggest using JS and creating new record using web API calls. This way you can stop the data from being saved, you can show error to the user and also create logs.

  • ChrisJC Profile Picture
    ChrisJC 1,540 on at
    RE: Plugin Exception - create record

    Thanks both.

    So stopping throwing the second error let's the bad data persist to the record. How do I stop the data being written to the record via notification to the user as well as recording the fact in a separate record?

    Thanks,

    Chris

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: Plugin Exception - create record

    Hi,

    Once you write the line "throw new InvalidPluginExecutionException" , Then everthing will be rollback. That's why you are unable to see the new record in your instanse.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin Exception - create record

    Everything will be rolled back as you are throwing another exception after creating the record. You should not throw another exception.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans