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
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
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"); } } } } } }
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.
André Arnaud de Cal...
292,516
Super User 2025 Season 1
Martin Dráb
231,409
Most Valuable Professional
nmaenpaa
101,156