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,...
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
    Amit Katariya007 Profile Picture
    10,409 Moderator on at

    Hello,

    Can you share the error code ?

    Thank you,

    Amit katariya

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

    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

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 111 Super User 2026 Season 2

#2
Syed Aqib Raza Profile Picture

Syed Aqib Raza 88

#3
ParthPatel249 Profile Picture

ParthPatel249 84

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans