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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Dynamics Plugin - get account GUID

(0) ShareShare
ReportReport
Posted on by 5

Hello, I’m hoping someone might be able to help me, I’m new to creating plugins so apologies if this has been asked before.

 

I am trying to import some external data into Dynamics using a virtual entity. I have successfully created a plugin for this with the Retrieve and RetrieveMultiple event handlers and this shows me all the data in a subgrid using a sample API. However, the API I’m using requires me to filter the results as part of the request by passing a parameter, in this case it’s a company ID. This company ID is saved as a field in the account entity but I’m struggling to get this at run time in the plugin that is making the API call.

 

On the account form I have a tab that will be for Licence Codes that are saved in an external source, I can use an API call to retrieve these, but I need to know the company ID before I can make the call. Is there a way I can get the GUID of the account that I’m using (i.e. the account on the open form) as I can then get the company ID using a simple queryExpression? Using Guid Id = context.PrimaryEntityId only returns 00000000-0000-0000-0000-000000000000 not the GUID of the account record.

Any help would be greatly appreciated

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    problem is that your plugin is running on retrieve and retrievemultiple, this in generally means that your plugin can be triggered by a single account retrieve (so you get a single id) or multiple accounts (so you will get multiple id)

    I don't know if your codebase is inside a single plugin or two plugins (one for retrieve, another for retrievemultiple)

    however normally you can get the id in the following way (beside, the context.PrimaryEntityId should work for the retrieve)

    for retrieve

    Entity currentEntity = null;
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        currentEntity = (Entity)context.InputParameters["Target"];
        Guid currentEntityId = currentEntity.Id;
    	// rest of the code ...

    for retrievemultiple

    if (context.InputParameters.Contains("BusinessEntityCollection") && context.InputParameters["BusinessEntityCollection"] is EntityCollection)
    {
        EntityCollection currentCollection = (EntityCollection)context.InputParameters["BusinessEntityCollection"];
        foreach (Entity entity in currentCollection.Entities)
        {
            Guid currentEntityId = entity.Id;
            // rest of the code ...	

  • Prakash Kalimuthu Profile Picture
    on at

    Hi,

    Can you please elaborate the business requirement.

    As per my understanding, you are displaying external data records as a subgrid in account entity and you have written retrieve for account and retrieve multiple for external records.

    Am I right?

    What is company ID inside account?

    If you want to filter those records based on company id, there should be 1:n relationship needs to be exist between account and the virtual entity.

    Regards,

    Prakash

  • Dave P Profile Picture
    5 on at

    Hello Guido, thank you for coming back to me so quickly. I’m not sure I explained the situation very well, so I will try again.

    I have written a single Plugin which has 2 classes in it, one for Retrieve and another for RetrieveMultiple, then using the Plugin Registration Tool I have registered the dll file as a new assembly as per the guidance notes. I have then Registered a new Data Provider and selected the assembly from the previous step and set the 2 event handlers as the Retrieve and RetrieveMultiple classes and then created the data source again as per the instructions.

    I have added the new Virtual Entity Data Source into Dynamics, created the Virtual Entity and set the data source to be the one we just added. I have then added the appropriate fields to the Virtual Entity and added the tab to the accounts form along with the subgrid.

    When I then open the account form I can go to the new tab and the API call is made and the data is displayed as expected, perfect!

    However, this is just a test to make sure I understand how this all works, what I now need to do is to use the API for the licence codes rather than some other test API. The different is, in the test API, the retrieveMultiple class returns all the records, so I could then filter them based on a relationship or anything else I wanted to, however in the API for the licence codes, retrieveMultiple requires an ID as a parameter so it only returns the Licence codes for that company. So, what I need to do is to read the CompanyID field (This is just an Integer) from the open form and then pass that to the API call.

    But the problem I’m having is that the RetrieveMultiple class seems to be acting upon the Virtual Entity and not the Account form so when I ask for the GUID it doesn’t return the correct value. I have tried adding different classes for getting the Account Info by adding steps into the plugin so it happens on retrieve of an account and whilst this gives me the GUID I then cant pass to the RetrieveMultiple class to pass onto the API call.

    I’m stuck as to how I move forward with this, I seem to be able to get the info I want/need in different plugins but cant seem to join them all together. Please can you advise how I can access the Account GUID from the RetrieveMultiple plugin which is for the Virtual Entity?

  • Prakash Kalimuthu Profile Picture
    on at

    Hi Dave,

    This might help you.

    While configuring subgrid for virtual entity in account select Records as "only related records". In background fetch XML will be added with regardingobjectid with parent account id.

    In retrievemultiple plugin, output collection will get it as fetch XML and from that get regardingobjectid value that is account id. Perform one retrieve for account using above fetched value inside the same plugin to get company id. Add the company ID filter to fetch XML to only get those related records and remove regardingidobject filter from the fetch XML.

    This can be achieved using one single retrieve multiple for virtual entity.

    FYR, refer this link 

    https://jonasr.app/2016/04/all-activities/

    The above link is all about activities but requirement is same.

    Let me know if you need any more details.

    Mark it as answer if this solves your problem.

    Regards,

    Prakash

  • Dave P Profile Picture
    5 on at

    Hi Prakash, Thanks for the reply. I think I understand what this is trying to do, not 100% how I would implement implement this with a virtual entity. Would I use my existing plugin registered as the Data Provider and use the existing RetrieveMultiple class and simply add ReplaceRegardingCondition to it to gain access to GUID of the related record, in this case Account? I presume I would need to add a similar set of conditions to the view used on the subgrid? I have imported the unmanaged solution into my sandbox environment and tried to run it and when I check the trace logs all I get is "Not expected context" and then "Not expected query" any ideas what I'm doing wrong?

  • Prakash Kalimuthu Profile Picture
    on at

    Hi Dave,

    Yes, you can use the existing retrieve multiple plugin.

    From regardingobjectid you will be able to get account id from value property.

    I have few questions,

    1. Is virtual entity added as subgrid in account ? Any relationship has been created between virtual entity and account?

    2. How will you filter virtual entity based on account. Is there any field available, to which it match account and virtual entity?

    I am sure there will be no lookup has been created in that virtual entity.

    Regards,

    Prakash

  • Dave P Profile Picture
    5 on at

    Hi Prakash,

    The virtual entity will be added as a subgrid on a main Account form under its own tab so the data will only need to be loaded if the user opens that tab. At present I have not added any relationship between account and the virtual entity, this will be what the CompanyID will be for, so when you open the tab, it will get the CompanyID and inject that to the API call in the RetrieveMultiple Class to only return the licences that are associated with that company. Any ideas what might be causing the sample code to return those errors?

  • Prakash Kalimuthu Profile Picture
    on at

    Hi Dave,

    Can you please show me the subgrid configuration and plugin code.

    I might take a look into it.

  • Dave P Profile Picture
    5 on at

    Hi Prakash

    I'm simply using the managed solution from the link you sent me, I'm trying to prove that I understand the code and how it works before trying to build the final solution with the correct API.

  • Suggested answer
    Prakash Kalimuthu Profile Picture
    on at

    Hi Dave,

    Try the below steps.

    Please create lookup field in virtual entity that points to account. Do not add inside any form. That will automatically create relationship between account and the virtual entity.

    Open the existing subgrid configuration for virtual entity in account, select Records as "only related records" instead of "all records". I think in your case at present it will be for all records.

    Then follow the link which I shared above.

    Instead of regardingobjectid now it will be schema name of the lookup field which you have created.

    I am not sure how I am going to help without code.

    I hope this might help you.

    Regards,

    Prakash

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans