Hi All,
I need to create a plugin to prevent the duplication but with a different Criteria The scenario :
I have Maintenance department and service department so fro example :
Contract_number Department
1002 Maintenance
1002 Service
This is right
Contract_number Department
1002 Maintenance
1002 Maintenance
This is wrong
So the following code will prevent the duplication for all contract numbers how can i convert it to be for the departments level The Code:
if (contract.Contains("new_name")) contractNumber = (string)contract.Attributes["new_name"]; else if (contractImg.Contains("new_name")) contractNumber = (string)contractImg.Attributes["new_name"]; QueryExpression qExp = new QueryExpression(); EntityCollection eCol = new EntityCollection(); qExp.EntityName = "new_contract"; qExp.ColumnSet = new ColumnSet("new_name"); qExp.Criteria.AddCondition("new_name", Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,(contractNumber)); eCol = service.RetrieveMultiple(qExp); if (eCol.Entities.Count > 1) { throw new InvalidPluginExecutionException("Contract Number already exists "); }
*This post is locked for comments
The default filter is AND, If you need to change it then you can use Filter Expression :)
Thanks, but you don't use filter1 the and operator ?
int DepartmentValue;
if (contract.Contains("new_name"))
contractNumber = (string)contract.Attributes["new_name"];
else if (contractImg.Contains("new_name"))
contractNumber = (string)contractImg.Attributes["new_name"];
if (contract.Contains("new_department")) //Get the option set value for the department field
DepartmentValue = contract.GetAttributeValue<OptionSetValue>("new_department").Value;
else if (contractImg.Contains("new_department"))
DepartmentValue = contractImg.GetAttributeValue<OptionSetValue>("new_department").Value;
QueryExpression qExp = new QueryExpression();
EntityCollection eCol = new EntityCollection();
qExp.EntityName = "new_contract";
qExp.ColumnSet = new ColumnSet("new_name");
FilterExpression filter1 = new FilterExpression(LogicalOperator.And);
qExp.Criteria.AddCondition("new_name", Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,(contractNumber));
qExp.Criteria.AddCondition("new_department", Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,(DepartmentValue)); // add condition to check the department
eCol = service.RetrieveMultiple(qExp);
if (eCol.Entities.Count > 0)
{
throw new InvalidPluginExecutionException("Contract Number already exists ");
}
Yes the Department field is an optionset
And the Department Field is new_Type
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156