Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

need a plugin code

(0) ShareShare
ReportReport
Posted on by 150

i need a plugin code to check if  same acc name & same country name is repeatd . throw a expection that acc name alrdy exits.

*This post is locked for comments

  • Verified answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: need a plugin code

    Hi ,

    I am bit late but some changes I did on top of Ravi's plugin - 

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TestConsole
    {
        public class ValidateAccountNameCountry : IPlugin
        {
    
            public void Execute(IServiceProvider serviceprovider)
    
            {
    
                if (serviceprovider == null)
    
                    throw new ArgumentNullException("serviceProvider");
    
                ITracingService tracingService = (ITracingService)serviceprovider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                if (context.InputParameters != null)
                {
    
                    Entity targetEntity = (Entity)context.InputParameters["Target"];
                    if (targetEntity.Contains("name") && targetEntity.Contains("address1_country"))
                    {
                        string accountName = string.Empty;
                        if (targetEntity.Attributes.Contains("name"))
                        {
                            if (targetEntity.GetAttributeValue<string>("name") != "" && targetEntity.GetAttributeValue<string>("name") != null)
                            {
                                accountName = targetEntity.GetAttributeValue<string>("name");
                            }
                        }
    
                        Guid accountCountry = new Guid();
                        if (targetEntity.Attributes.Contains("address1_country"))
                        {
                            if (targetEntity.Attributes["address1_country"] != null)
                            {
                                accountCountry = (targetEntity.Attributes["address1_country"] as EntityReference).Id;
                            }
                        }
    
                        ConditionExpression condition1 = new ConditionExpression();
                        ConditionExpression condition2 = new ConditionExpression();
                        FilterExpression filter1 = new FilterExpression();
                        filter1.FilterOperator = LogicalOperator.And;
    
                        condition1.AttributeName = "name";
                        condition1.Operator = ConditionOperator.Equal;
                        condition1.Values.Add(accountCountry);
                        filter1.Conditions.Add(condition1);
    
                        condition2.AttributeName = "address1_country";
                        condition2.Operator = ConditionOperator.Equal;
                        condition2.Values.Add(accountCountry);
                        filter1.Conditions.Add(condition2);
    
                        QueryExpression qe = new QueryExpression();
                        qe.Criteria.AddFilter(filter1);
                        qe.EntityName = "account";
                        qe.ColumnSet = new ColumnSet();
                        qe.ColumnSet.AddColumns("name", "accountid", "accountnumber", "statuscode");
                        EntityCollection results = service.RetrieveMultiple(qe);
    
                        if (results != null && results.Entities.Count > 0)
                        {
    
                            throw new InvalidPluginExecutionException("Account with the same name and country already exists");
    
                        }
    
                    }
    
                }
    
            }
    
        }
    
    }
    


  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: need a plugin code

    Hi,

    I think the below should work for you. You can register this on pre-operation and synchronous

    ================

    public class CheckAccountExists : IPlugin

       {

           public void Execute(IServiceProvider serviceprovider)

           {

               if (serviceprovider == null)

                   throw new ArgumentNullException("serviceProvider");

               ITracingService tracingService = (ITracingService)serviceprovider.GetService(typeof(ITracingService));                        

               IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));            

               IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));

               IOrganizationService service = factory.CreateOrganizationService(context.UserId);

               if (context.InputParameters != null)

               {

                   Entity targetEntity = (Entity)context.InputParameters["Target"];

                   if (targetEntity.Contains("name") && targetEntity.Contains("address1_country"))

                   {

                       var accountName = targetEntity["name"];

                       var accountCountry = targetEntity["address1_country"];

                       QueryExpression accountQE = new QueryExpression("account");

                       accountQE.ColumnSet = new ColumnSet(true);

                       accountQE.Criteria.AddCondition("name", ConditionOperator.Equal, accountName);

                       accountQE.Criteria.AddCondition("address1_country", ConditionOperator.Equal, accountCountry);

                       var results = service.RetrieveMultiple(accountQE);

                       if (results != null && results.Entities.Count > 0)

                       {

                           throw new InvalidPluginExecutionException("Account with the same name and country already exists");

                       }

                   }

               }

           }

       }

    ==========================

    Hope this helps.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans