Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Javascript code to retrieve account name field on related table

Posted on by 2

Hi,

Javascript code to retrieve account name from related table case

.Can we use Xrm.WebApi.retrieveRecord or fetchxml

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript code to retrieve account name field on related table

    Hello,

    Replace the line

    "<condition attribute='associatedcase' operator='eq' uiname='' uitype='incident' value='caseid' />"+

    with line

    "<condition attribute='associatedcase' operator='eq' uiname='' uitype='incident' value='" + caseid + "' />"+

    and try it again.

  • Suggested answer
    ashokkmb Profile Picture
    ashokkmb 25 on at
    RE: Javascript code to retrieve account name field on related table

    Hello EKL,

    You can use both of them. But Xrm.WebApi.retrieveRecord will  provide the result little bit faster.

    Thanks!

    Ashok.

  • EKL Profile Picture
    EKL 2 on at
    RE: Javascript code to retrieve account name field on related table

    Hi,

    The below code is not giving the name value.

    var caseid = formContext.getAttribute("associatedcase").getValue()[0].id;  

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> latematerialize='true'"+

     "<entity name='service'>"+

       "<attribute name='serviceid' />"+

       "<filter type='and'>"+

         "<condition attribute='associatedcase' operator='eq' uiname='' uitype='incident' value='caseid' />"+

       "</filter>"+

       "<link-entity name='incident' from='incidentid' to='associatedcase' link-type='inner' alias='aa'>"+

         "<attribute name='customerid' />"+

         "<filter type='and'>"+

           "<condition attribute='customerid' operator='not-null' />"+

         "</filter>"+

       "</link-entity>"+

     "</entity>"+

    "</fetch>"

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/service?fetchXml=" + encodeURIComponent(fetchXml),true);

    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);

         var name = results[0];

       } else {

         alert(this.statusText);

       }

     }

    };

    req.send();

  • Suggested answer
    Manoj Mane Profile Picture
    Manoj Mane 1,012 on at
    RE: Javascript code to retrieve account name field on related table

    Hi EKL,

    Please find the  below code snippet with the retrieveMultipleRecords:- 

    // Get the related record's ID
    var relatedRecordId = Xrm.Page.getAttribute("new_relatedrecordid").getValue()[0].id;

    // Set the related entity name (e.g. "account" or "contact")
    var relatedEntityName = "account";

    // Set the query string to retrieve the related record's account name
    var queryString = "?$select=name&$expand=accountid($select=name)&$filter=" + relatedEntityName + "id eq " + relatedRecordId;

    // Retrieve the related record's account name using the Xrm.WebApi
    Xrm.WebApi.retrieveMultipleRecords("new_relatedentityname", queryString).then(
    function success(results) {
    // Get the account name from the first result's accountid attribute
    var accountName = results.entities[0]["accountid"]["name"];

    // Use the account name as needed (e.g. set a field value)
    formContext.getAttribute("new_accountname").setValue(accountName)

    },
    function error(error) {
    console.log(error.message);
    }
    );



    Thanks,

    Manoj Mane.

    Please mark this as VERIFIED if it helps. image


  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript code to retrieve account name field on related table

    Hello,

    You can use both depending on what your scenario is. I would recommend using Dataverse Rest Builder for that purpose - github.com/.../DRB

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans