Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

An unexpected error occurred from ISV code.

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

  • 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
    Bipin D365 28,964 Super User 2024 Season 1 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.

  • 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
    a33ik 84,323 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.

  • 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
    a33ik 84,323 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
    furkank 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

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans