Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

An unexpected error occurred from ISV code

(0) ShareShare
ReportReport
Posted on by 90

Hello,

I have registered  a Plugin onUpdate of account. My requirement is - when I update the description filed in my Account entity, I want to auto-update then same value in all Contact of that related Accounts.

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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace CFT158SalesPlugins
{
    public class AccountUpdate : IPlugin
    {
        IOrganizationService service; Entity currentEntity;
        IPluginExecutionContext context;
        ITracingService trace;
        /// 
        /// Initializes common parameters to set the IPluginExecutionContext and IOrganizationServiceFactory.
        /// 
        /// Object of IServiceProvider
        
        private void GetOrganizationService(IServiceProvider serviceProvider)
        {
            try
            {
                context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.UserId);
                trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            }
            catch(Exception ex)
            {
                
            }
        }

        private void GetRecordContext()
        {
            try
            {
                currentEntity = (Entity)context.InputParameters["Terget"];
                if (currentEntity.Attributes.Contains("new_description"))
                {
                    string description = currentEntity.GetAttributeValue("new_description");
                    GetAllChildRecords(description);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void GetAllChildRecords(String description)
        {
            try
            {
                String fetchXML = @"
                < entity name = 'contact' >
                    < attribute name = 'fullname' />
                    < attribute name = 'telephone1' />
                    < attribute name = 'contactid' />
                    < order attribute = 'fullname' descending = 'false' />
                    < filter type = 'and' >
                        < condition attribute = 'parentcustomerid' operator= 'eq' value = '{"   currentEntity.Id.ToString()   @"}' />
                    
                
                ";

                EntityCollection retrievedContacts = service.RetrieveMultiple(new FetchExpression(fetchXML));
                if (retrievedContacts.Entities.Count > 0)
                {
                    foreach (Entity e in retrievedContacts.Entities)
                    {
                        Entity contactUpdate = new Entity("contact");
                        contactUpdate["description"] = description;
                        contactUpdate.Id = e.Id;
                        service.Update(contactUpdate);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                GetOrganizationService(serviceProvider);
                GetRecordContext();
            }
            catch(Exception)
            {
                throw;
            }
        }
    }
}

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

    Hello,

    Few recommendations:

    1. Don't use try/catch/rethrow - it makes no sense in plugins. And in code overall. It makes sense when in catch your code does something with Exception. Just a rethrow - makes no sense.

    The following code will make a difference for plugins:

               try

               {

                   //your code here

               }

               catch(Exception e)

               {

                   throw new InvalidPluginExecutionException(e.Message);

               }

    2. There is no "Terget" in InputParameters. Target is:

    currentEntity = (Entity)context.InputParameters["Terget"];

    3. Doublecheck that your FetchXml is valid. I remember I experienced issues when I had an extra space in it like:

    < filter type = 'and' > -- this is non-valid

    <filter type='and'> -- this is valid

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,407 Super User 2025 Season 1 on at
    RE: An unexpected error occurred from ISV code

    Hello,

    Can you share the error code ?

    Thank you,

    Amit katariya

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

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... 113 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

#3
Sahra Profile Picture

Sahra 43

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans