Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Xrm.WebApi how to retrieve attribute

I am trying to retrieve the ID and name of a lookup field in the entity Organization.

var fetchxml = `
                <fetch distinct='false' mapping="logical" output-format="xml-platform" version="1.0">
                <entity name="categorization">
                <attribute name="categorizationid"/>
                <attribute name="name"/>
                <attribute name="category1"/>
                <attribute name="category2"/>
                <attribute name="category3"/>
                <order descending="false" attribute="ava_category1"/>
                <filter type="and">
                <condition attribute="statecode" operator="eq" value="0" />
                </filter>
                </entity>
                </fetch>`;

fetchxml = "?fetchXml=" + encodeURIComponent(fetchxml);

Xrm.WebApi.retrieveMultipleRecords("categorization", fetchxml).then(
  function success(results) {
    var data = [];
    for (var r in results) {
      var category1text = results[r].attributes["category1"].name;
      var category1lookup = results[r]["category1"].id;

      if (results[r].attributes["category2"] != undefined) {
        category2text = results[r].attributes["category2"].name;
        var category2lookup = results[r].attributes["category2"].id;
      }

      var category3text = null;

      if (results[r].attributes["category3"] != undefined) {
        category3text = results[r].attributes["category3"].name;
        var category3lookup = results[r].attributes["category3"].id;
      }
    }
  }
);

Why doesnt the part results[r].attributes["category1"].name run? Am I missing something?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It should be something like this:

webApi.retrieveMultipleRecords("categorization", fetchxml)
    .then(result => {
        var data = [];

        result.entities.forEach(e => {
            var category1text = e.category1.name;
            ... etc.

Some context: for in loops through the properties of an object. It returns string values. You can use for of to loop through an array. I personally prefer the .forEach() function with lambda expressions.

See W3Schools - JavaScript for/in Statement.

about 4 years ago · Juan Pablo Isaza Report

0

the best way is to use odata for building your request like that

` var option = "?$select=categorizationid,name,_category1_value,_category2_value,_category3_value&$filter=statecode eq 0"; SDK.WEBAPI.retrieveMultipleRecords("categorization", option, function (results) {

                for (var i = 0; i < results.value.length; i++) {
                    var Cat1LookupGuid = results.value[i]["_category1_value"];
                    var Cat1LookupName = results.value[i]["_category1_value@OData.Community.Display.V1.FormattedValue"];
                    
                     var Cat2LookupGuid = results.value[i]["_category2_value"];
                    var Cat2LookupName = results.value[i]["_category2_value@OData.Community.Display.V1.FormattedValue"];
                    
                    var Cat3LookupGuid = results.value[i]["_category3_value"];
                    var Cat3LookupName = results.value[i]["_category3_value@OData.Community.Display.V1.FormattedValue"];                  


                }
            
        }, function (error) {
            console.error("your error" + error.message);
        }, function () { }
        );
        
        `

This tool can help you so much to create your odata request https://github.com/jlattimer/CRMRESTBuilder

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!