Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

how to get account id on the behalf of opportunityid?

Posted on by Microsoft Employee

Hi ,

I am creating a entity ,name of new entity"ProductListing" and i have some fields on this  entity like product name,product amount and account name which is lookup field and when i am open the opportunity,in which product line section, i am adding a product.

After that product name,product amount and account name fill in my new entity form on "ProductListing", i copy the product name and product amoount in ProductListing but i can't get accountID. How to fetch accountID?

But i have opporunityId and using plugins

My code is:

protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new InvalidPluginExecutionException("serviceProvider");
            }

            // TODO: Implement your custom Plug-in business logic.

            IPluginExecutionContext context = serviceProvider.PluginExecutionContext;

            //Check weather the "Traget" exists in the inpu parameter
            if (context.InputParameters.Contains("Target") == false) return;

            //Check weather the Input Parameter is a entity or not
            if (context.InputParameters["Target"] is Entity == false) return;

            //Get service and service factory from service provider            
            IOrganizationService service = serviceProvider.OrganizationService;

            //Extract the tracing service for use in debugging and to do tracing            
            ITracingService tracingService = serviceProvider.TracingService;

            //Check weather the Target entity have the originating lead filed or not
            Entity selectedEntity = (Entity)context.InputParameters["Target"];

            //For insert record in product entity
            Entity entProdList = new Entity("new_productlisting");
            entProdList["new_name"] = selectedEntity.GetAttributeValue<string>("productdescription");            
            entProdList["new_description"] = "LG";
            entProdList["new_accountName"]=?
            service.Create(entProdList);
        }

Thanks in advance!

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to get account id on the behalf of opportunityid?

    Hi Shahbaaz,

    Its working .

    Thanks

  • Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: how to get account id on the behalf of opportunityid?

    Hi,

    Did it resolved your issue??

    if you still facing problem, please let me know.

    Best Regards,

    Shahbaaz

  • Verified answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: how to get account id on the behalf of opportunityid?

    Please try below code.

    protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)

           {

               if (serviceProvider == null)

               {

                   throw new InvalidPluginExecutionException("serviceProvider");

               }

               // TODO: Implement your custom Plug-in business logic.

               IPluginExecutionContext context = serviceProvider.PluginExecutionContext;

               //Check weather the "Traget" exists in the inpu parameter

               if (context.InputParameters.Contains("Target") == false) return;

               //Check weather the Input Parameter is a entity or not

               if (context.InputParameters["Target"] is Entity == false) return;

               //Get service and service factory from service provider            

               IOrganizationService service = serviceProvider.OrganizationService;

               //Extract the tracing service for use in debugging and to do tracing            

               ITracingService tracingService = serviceProvider.TracingService;

               //Check weather the Target entity have the originating lead filed or not

               Entity selectedEntity = (Entity)context.InputParameters["Target"];

    Guid opportunityid = new Guid()//pass guid of opportunity which you have and get the accountid

    //QueryExpression to get Accountid

    //get accountid

                                       QueryExpression getAccountid = new QueryExpression()

                                       {

                                           EntityName = "opportunity",

                                           ColumnSet = new ColumnSet("parentaccountid"),//confirm the account schema name on opportunity entity

                                           Criteria =

                                               {

                                                Filters ={

                                                           new FilterExpression

                                                           {                                                          

                                                                   Conditions =

                                                                   {

                                                                       new ConditionExpression("opportunityid",ConditionOperator.Equal,opportunityid),

                                                                   }

                                                           },

                                                        }

                                               }

                                       };

                                       EntityCollection getAccount = service.RetrieveMultiple(getAccountid);

                                       if (getAccount.Entities.Count > 0)

    {

    Guid accoutid = ((EntityReference)getAccount.Entities[0].Attributes["parentaccountid"]).Id;

    //For insert record in product entity

    Entity entProdList = new Entity("new_productlisting");

    entProdList["new_name"] = selectedEntity.GetAttributeValue<string>("productdescription");            

    entProdList["new_description"] = "LG";

    entProdList["new_accountName"]=new EntityReference("account", accoutid);

    service.Create(entProdList);

    }

           }

    If it helps, Mark my answer as verified.

    Best Regards,

    Shahbaaz

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to get account id on the behalf of opportunityid?

    Hi Shahbaaaz,

    But how to get "guidofaccount" ?

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: how to get account id on the behalf of opportunityid?

    Hi ,

    You need to set like

    entProdList["new_accountName"]=new EntityReference("account", guidofaccount);

    Please follow below code :

    protected override void ExecuteCrmPlugin(LocalPluginContext serviceProvider)

           {

               if (serviceProvider == null)

               {

                   throw new InvalidPluginExecutionException("serviceProvider");

               }

               // TODO: Implement your custom Plug-in business logic.

               IPluginExecutionContext context = serviceProvider.PluginExecutionContext;

               //Check weather the "Traget" exists in the inpu parameter

               if (context.InputParameters.Contains("Target") == false) return;

               //Check weather the Input Parameter is a entity or not

               if (context.InputParameters["Target"] is Entity == false) return;

               //Get service and service factory from service provider            

               IOrganizationService service = serviceProvider.OrganizationService;

               //Extract the tracing service for use in debugging and to do tracing            

               ITracingService tracingService = serviceProvider.TracingService;

               //Check weather the Target entity have the originating lead filed or not

               Entity selectedEntity = (Entity)context.InputParameters["Target"];

               //For insert record in product entity

               Entity entProdList = new Entity("new_productlisting");

               entProdList["new_name"] = selectedEntity.GetAttributeValue<string>("productdescription");            

               entProdList["new_description"] = "LG";

               entProdList["new_accountName"]=new EntityReference("account", guidofaccount);

               service.Create(entProdList);

           }

    Mark my answer as verified if it helps

    Best regards,

    Shahbaaz

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans