web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

An unexpected error occurred from ISV code.

(0) ShareShare
ReportReport
Posted on by 170

I have registered  a Plugin onCreate of account. My requirement is to Check Duplicates on User Records if Record already exists in Admin Records.

Example : User Create account with name="Mark". If Admin Records already have Account with name "Mark", It must throw an error to User, else Save the Record

Now, I am getting An unexpected error occurred from ISV code. Error on Save Form

Here Is the code I used

using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using System.Collections.Generic;
namespace DuplicationCheck
{
    public class DuplicationCheck : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(null);
                IOrganizationService serviceUser = serviceFactory.CreateOrganizationService(context.UserId);
                //if you want to impersonate System user Pass null in CreateOrganizationService
                if (context.InputParameters.Contains("Target") &&
                   context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters. 
                    Entity entity = (Entity)context.InputParameters["Target"];
                    QueryExpression query = new QueryExpression();
                    query.EntityName = "account";
                    ColumnSet col = new ColumnSet("name");
                    query.ColumnSet = col;
                    //query.Criteria.AddCondition("new_middlename", ConditionOperator.Equal,"R");
                    //collection is of Admin Records
                    EntityCollection collection = service.RetrieveMultiple(query);
                    //collection of User Records
                    EntityCollection collectionUser = serviceUser.RetrieveMultiple(query);
                    string str;
                    string str1;
                    //string booleanField;
                    foreach (Entity e in collectionUser.Entities)
                    {
                        str = e.Attributes["name"].ToString();
                        //booleanField = e.Attributes["new_booleanfield"].ToString();
                        foreach (Entity e1 in collection.Entities)
                        {
                            str1 = e1.Attributes["name"].ToString();
                            if (str1 == str)
                            {
                                Console.WriteLine("Sorry Equal Record Found");
                                throw new InvalidPluginExecutionException("It seems the record of this Name already Exists!!");
                            }
                        }
                    }
                }
            }
            catch (InvalidPluginExecutionException e)
            {
                // catch exception
                throw new InvalidPluginExecutionException("An error has occurred: " + e.Message);
            }
        }
    }
}

I have the same question (0)
  • furkank Profile Picture
    535 on at

    Can you attach to debugger? You can use plugin profiler if you cant attach from visual studio.

    You can check which line throws error. Also try to put traces in your code and enable your plugin trace logs to check these traces.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    This is how platform handles serverside errors on the frontend and you have nothing to do with it.

  • Bhagyashree Shahade Profile Picture
    170 on at

    I guess my Error handling seems wrong. Is it?? If not, can you suggest me how can I save records if User Records matches with Admin Records "name" and throw error if  it does not match

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I believe it is wrong because:

                               if (str1 == str)

                               {

                                   Console.WriteLine("Sorry Equal Record Found");

                                   throw new InvalidPluginExecutionException("It seems the record of this Name already Exists!!");

                               }

    in this code you validate if name already exists and throw an exception if so and... this exception caught and rethrown here:

               catch (InvalidPluginExecutionException e)

               {

                   // catch exception

                   throw new InvalidPluginExecutionException("An error has occurred: " + e.Message);

               }

    Just remove external handling of exception and it should start working.

  • Bhagyashree Shahade Profile Picture
    170 on at

    What can I do to save records if there is no duplication.

    As in if(str!=str1)??. Note:  I am calling it on Preoperation

  • Verified answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    if(str!=str1) - you don't have to do anything. System will save the record if you don't throw any exception.

    If there is no duplicate, you let system save the record so no need to add code for that.

    Make sure you have not activated OOB duplicate detection rules otherwise you will get warning on save of record if rule satisfy the conditions.

    If found helpful, Please mark my answer verified.

  • Bhagyashree Shahade Profile Picture
    170 on at

    Thanks All, Though all of the above suggestions made the error to went off. But I am getting the Duplication check message for every String I enter in UserRecord , Even if Strings are not Equal , It must get Saved, but it isn`t.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans