Skip to main content

Notifications

Announcements

No record found.

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);
            }
        }
    }
}

  • Bhagyashree Shahade Profile Picture
    170 on at
    RE: An unexpected error occurred from ISV code.

    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.

  • Verified answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: An unexpected error occurred from ISV code.

    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
    RE: An unexpected error occurred from ISV code.

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

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

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: An unexpected error occurred from ISV code.

    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
    RE: An unexpected error occurred from ISV code.

    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
    RE: An unexpected error occurred from ISV code.

    Hello,

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

  • furkank Profile Picture
    535 on at
    RE: An unexpected error occurred from ISV code.

    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.

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

🌸 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…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans