web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

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

(1) ShareShare
ReportReport
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!

I have the same question (0)
  • Suggested answer
    Mohsin Ali Profile Picture
    3,634 on at

    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.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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/

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans