web
You’re offline. This is a read only version of the page.
close
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

Write a plugin for restricting the Contacts in the Associated View while adding contacts. We wanted to do this when the contact does not have the status reason ‘Approved’ by showing an error message and sending an email to the owner of the contact

(0) ShareShare
ReportReport
Posted on by 154

Hi community, Write a plugin for restricting the Contacts in the Associated View while adding contacts. We wanted to do this when the contact does not have the status reason ‘Approved’ by showing an error message and sending an email to the owner of the contact can anyone please help me for write a plugin for this.

I have the same question (0)
  • Verified answer
    Linn Zaw Win Profile Picture
    3,407 on at

    You will have to write a Pre Operation plugin for the Associate message. In the plugin, check the relationship name, get the Contact GUID from the target to check the Status Reason and throw an error message.

    Here's the sample code generated from ChatGPT.

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    
    namespace YourNamespace
    {
        public class ContactCaseAssociationPlugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                try
                {
                    // Check if the plugin is triggered on the Associate message for the Contact and Case relationship
                    if (context.MessageName.ToLower() == "associate" && context.InputParameters.Contains("Relationship") && context.InputParameters["Relationship"] is Relationship)
                    {
                        Relationship relationship = (Relationship)context.InputParameters["Relationship"];
    
                        // Check if the relationship is between Contact and Case entities
                        if (relationship.SchemaName.ToLower() == "new_contact_new_case")
                        {
                            // Get the ID of the associated Contact
                            Guid contactId = ((EntityReference)context.InputParameters["Target"]).Id;
    
                            // Retrieve the Contact record to check its status
                            ColumnSet columnSet = new ColumnSet("statuscode");
                            Entity contact = service.Retrieve("contact", contactId, columnSet);
    
                            // Check if the Contact's status is not approved
                            if (contact.Contains("statuscode") && ((OptionSetValue)contact["statuscode"]).Value != 1)
                            {
                                throw new InvalidPluginExecutionException("Cannot associate a Contact with a Case when the Contact's status is not approved.");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException($"An error occurred in the ContactCaseAssociationPlugin: {ex.Message}");
                }
            }
        }
    }
    

  • Tushar Jadhav Profile Picture
    154 on at

    I have written the code in post operation also i got desire output also but email not be send but it tracer it will print that email send but i didn't got any email can you please help me for this.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans