Notifications
Announcements
No record found.
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.
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}"); } } } }
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Tom_Gioielli 70 Super User 2025 Season 2
Gerardo Rentería Ga... 33 Most Valuable Professional
Daniyal Khaleel 32 Most Valuable Professional