0
votes

How do I get the text for an optionset value from a CRM Query? Here is how I have written my query:

function nearByCases(addr){
    var nearByCasesFetchXML = '<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">' +
        '<entity name="incident">' +
          '<attribute name="title"/>' +
          '<attribute name="ticketnumber"/>' +
          '<attribute name="createdon"/>' +
          '<attribute name="new_department"/>' +
          '<attribute name="new_casetype"/>' +
          '<attribute name="incidentid"/>' +
          '<attribute name="caseorigincode"/>' +
          '<order descending="false" attribute="title"/>' +
          '<filter type="and"><condition attribute="new_address" value="'+addr+'" operator="eq"/>' +
          '</filter>' +
        '</entity>' +
      '</fetch>';

    nearByCasesFetchXML = "?fetchXml=" + encodeURIComponent(nearByCasesFetchXML);
    var html = "";
    //var outputText = "Case\t\t\tCreated\n---------------------------------------------------\n";
    window.parent.Xrm.WebApi.retrieveMultipleRecords("incident", nearByCasesFetchXML).then(function success(result) {
      console.log(result.entities);
      if(result.entities.length <=0) {
        window.parent.Xrm.Utility.alertDialog("No results found.", null);
        return false;
      }
      else{
        for (var casecount = 0; casecount < result.entities.length; casecount++) {
         // outputText += result.entities[casecount].ticketnumber+ "\t\t" + result.entities[casecount].title + "\n";
          html += "<tr>";
          html += "<td>"+result.entities[casecount].title+"</td>";
          html += "<td>"+result.entities[casecount].ticketnumber+"</td>";
          html += "<td>"+result.entities[casecount].FormattedValues["new_department"].ToString()+"</td>";
          html += "<td>"+result.entities[casecount].new_casetype+"</td>";
          html += "</tr>";
          $("#nearbycases tbody").html(html);
        }
        return true;
      }
      //window.parent.Xrm.Utility.alertDialog(outputText, null);
    },function (error) {
      // Handle error conditions
      window.parent.Xrm.Utility.alertDialog(error.message, null);
    });

}

This is fetchXml to fetch the data from incident and I am able to get all incident values but there are some custom optionsets (new_department,new_casetype) which only return the value, not the text.

So how do I get the text of these option set.

This is the response I get

{
  "@odata.etag": "W/"1999118"" ,
  "createdon": "2018-12-13T08:30:34Z" ,
  "[email protected]": "12/13/2018 2:30 AM" ,
  "incidentid": "dedfb05b-b1fe-e811-a977-000d3a33eb4e" ,
  "new_casetype": 1 ,
  "[email protected]": "Abandoned Vehicles" ,
  "new_department": 1 ,
  "[email protected]": "Parking Control" ,
  "ticketnumber": "CAS-01001-H2S7L9" ,
  "title": "case for sla 1",
}
1
i am getting the response like this: @odata.etag: "W/"1999118"" createdon: "2018-12-13T08:30:34Z" [email protected]: "12/13/2018 2:30 AM" incidentid: "dedfb05b-b1fe-e811-a977-000d3a33eb4e" new_casetype: 1 [email protected]: "Abandoned Vehicles" new_department: 1 [email protected]: "Parking Control" ticketnumber: "CAS-01001-H2S7L9" title: "case for sla 1"Bibhuti Bhusan Satpathy

1 Answers

0
votes

The FormattedValue entries are the text of the option set value.

The response you're getting contains the text so it's just a matter of referencing it.

You can reference them like this:

var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
    var departmentCode = results.value[i]["new_department"];
    var departmentCodeText = results.value[i]["[email protected]"];
}

departmentCode will be the integer and departmentCodeText will be the text.

Rather than what's shown in the question:

html +="<td>"+result.entities[casecount].FormattedValues["new_department"].ToString()+"</td>";

Try:

html += "<td>"+result.entities[casecount]["[email protected]"]+"</td>";