Hello,
I am currently trying the following:
I have to fields on the form of my opportunity. One is a Search-Field for an Entity of 'Account', the other is a Search-Field for the Entity 'Companies'.
Whenever a new opportunity on my system is created, I want to check if an opportunity with the same selected account and company already exists. In case that this happens, I want to throw a plugin execution, since this should never be allowed on my system.
My idea/ approach is the following:
I would register my plugin with a Postimage of those two fields. Whenever an opportunity is created or updated, I would execute a plugin that does Query and retrieve all the records that already have the value of those two fields. In case this query contains any data, I would throw an exception. This is what my code currently looks like:
protected override void OnExecute(PluginContext pluginContext) { var log = pluginContext.Logger; var orgsrv = pluginContext.OrganizationService; var salesopportuntiy = pluginContext.GetEntityPostImage("Post_Img"); var accountid = salesopportuntiy.GetAttributeValue("kvs_accountid").Id; var companyid = salesopportuntiy.GetAttributeValue("kvs_companyid").Id; var opportunityProductsQuery = new QueryExpression() { EntityName = "kvs_salesopportunity" }; opportunityProductsQuery.ColumnSet = new ColumnSet(true); var filter1 = new FilterExpression(); filter1.AddCondition(new ConditionExpression("kvs_accountid", ConditionOperator.Equal, accountid)); var filter2 = new FilterExpression(); filter2.AddCondition(new ConditionExpression("kvs_companyid", ConditionOperator.Equal, companyid)); opportunityProductsQuery.Criteria = new FilterExpression(LogicalOperator.And); opportunityProductsQuery.Criteria.AddFilter(filter1); opportunityProductsQuery.Criteria.AddFilter(filter2); DataCollection opportunityProducts = orgsrv.RetrieveMultiple( opportunityProductsQuery).Entities; if (opportunityProducts.Any()) { throw new InvalidPluginExecutionException("This salesopplortunity can not be saved."); } }
Through my debugging, I found that 'firmaid' and 'unternehmenid' are the IDs of the Account and Company data-records, respectively, which I create in the fields of my new record.
However, those data records that are determined by the query are entity references. So I'm afraid that I'm querying the wrong datasets with my current query? I also used the Advanced Search to search for opportunities that filled these two fields and exported the FetchXML. This query shows the data as I would expect. The XML looks like this:
</fet
Can someone help me here, I suspect that my query is faulty...
I am very grateful for any help!