Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Query on the ID of an Entity Reference (content of a search-field) using a plugin

Posted on by 45

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!

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Query on the ID of an Entity Reference (content of a search-field) using a plugin

    Hi,

    Your code looks good.

    I hope you have registered your plugin step on Post operation Create and Update.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    Mohsin Ali Profile Picture
    Mohsin Ali 3,610 on at
    RE: Query on the ID of an Entity Reference (content of a search-field) using a plugin

    Add tracing in your plugin to check accountid and companyid values that are coming as part of post image. Also please check if you have added correct name for the post image.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans