Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

retrieve parent

Posted on by Microsoft Employee

314660.Capture.PNG

I am trying since one month. Could not retrieve and populate the Parent account of the account name in the picture. For each account name, as son as it is clicked, the parent account should auto populate, as well as Top level pal name and pal of the parent account must populate in their respective fields. I can understand that this following may have to be done - 

1 while loop to check if ParentAccountID is set and if set, add that account to list
2. Get new account from ParentAccountID field and repeat process in loop.
3. IF account does not have parrentAccountID(means it's a top), update all accounts in list from 1st step by values from top account.

But I can do nothing. Please be kind to help with complete code. Is there anyone out there to help me please.

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: retrieve parent

    As per your requirement you should go with JS. If you still want to do with C# then On Update of "AccountNumber" you can write a plugin or you choose Console Application to update your all existing record, As Ravi already provided the C# code please try it once.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: retrieve parent

    I appreciate your time spent on the issue, Pankaj. However, I dont know JSON or ODATA so these line of codes were hard for me to understand. If you could simply write in C# would have been better.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: retrieve parent

    Hi Suchi,

    Write a function on onchange on your field "AccountNumber".

    //This will retreive the account record id which having details of your feilds : ParentAccount,Top Level PAL ....

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$filter=accountnumber eq ' **Pass Your accountnumber** '", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var results = JSON.parse(this.response);

               for (var i = 0; i < results.value.length; i++) {

                   var accountid = results.value[i]["accountid"];

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    // Retreive the values of the fields

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(accountid)?$select=description,_parentaccountid_value", true); // select your fields

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var result = JSON.parse(this.response);

               var desc = result["description"]; // get the values of selected fields..

               var _parentaccountid_value = result["_parentaccountid_value"]; // get the values of selected fields..

               var _parentaccountid_value_formatted = result["_parentaccountid_value@OData.Community.Display.V1.FormattedValue"];

               var _parentaccountid_value_lookuplogicalname = result["_parentaccountid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

    // Set your current attributes values

    Xrm.Page.getAttribute("description").setValue(desc);

    Xrm.Page.getAttribute("").setValue();

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    Please mark the answer as verified, if found useful.

    Thanks,

    Pankaj

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: retrieve parent

    Ok, Thanks Ravi. Thanks for investing time for me. I will try and keep you informed.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: retrieve parent

    Hi Suchisuchi,

    Did you try the below code to get the root account-

    ==============

    public void GetRootAccount(IOrganizationService service, EntityReference accountRef, out EntityReference rootAccount)

           {

               rootAccount = null;

               // Check if the account has parent

               var accoutnRecord = service.Retrieve(accountRef.LogicalName, accountRef.Id, new ColumnSet(true));

               if (accoutnRecord.Contains("parentaccountid"))

               {

                   var parentAccountRef = (EntityReference)accoutnRecord["parentaccountid"];

                   GetRootAccount(service, parentAccountRef, out rootAccount);

               }

               else

               {

                   rootAccount = accountRef;

               }

           }

    ============

    You cna use this method as follows-

    ============

    // Get the root accoutn reference

                   EntityReference rootAccountRef = null;

                   GetRootAccount(service, mainAccount.ToEntityReference(), out rootAccountRef);

                   // If root account ref is null that means there is no parent account, displaying the name from the main account

                   if (rootAccountRef != null)

                   {

                       var rootAccount = service.Retrieve(rootAccountRef.LogicalName, rootAccountRef.Id, new ColumnSet(true));

                       Console.WriteLine(rootAccount["name"]);

                   }

                   else

                   {

                       Console.WriteLine(mainAccount["name"]);

                   }

    ============

    In the above code snippet, the root account will be retrived if available toerh wise it will return null. Once the rootAccountRef is present, it retrives all the details of the root account. You cna then what ever values you want and update it to the main account.

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: retrieve parent

    Heelo Mohd Saad, I dont have skype , so sent you invite in linkedin my requirement is : as soon as any name for account name is clicked the top level parent account should sit on parent account field and the top levle pal name and top level pal will sit at thier respective fields. So it is like for each account name the  parent account will show the top level account name for that account name field and parents account also has pal name and top levl pal that also sits in the fields or auto populates with the change of account name

    Hope this helps to help me

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: retrieve parent

    Your requirement is not clear. Please elaborate more on what you are trying to achieve.

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans