CRM FetchXML activitytypecode Result doesn't have enum int value

I have a JS project where I want to pull out the CRM Activity types, let the user select and then do another fetchxml query with the chosen activity types.

I can get distinct list of the activitytypecodes out with simple FetchXML

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="activitypointer" >
<attribute name="activitytypecode" />
</entity>
</fetch>


result.attributes["activitytypecode"].formattedValue; //Returns name - 'Phone Call'

result.attributes["activitytypecode"].value; //Returns string - phonecall

What I want is the Int32 value for the enum (eg: 4210 for Phone Call) for example as when I go to query again with FetchXML I need the int32 value.

I can't seem to get it from the FetchXML results in JavaScript but if I use XrmToolbox - I can see the int32 value.. what am I missing?

The formattedValue seems to = the name And value is a text representation instead of the int32

enter image description here

Here is the complete code for the function:

function GetActivityTypes()
{
    var fetchXML = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">';
    fetchXML += '<entity name="activitypointer" >';
    fetchXML += '<attribute name="activitytypecode" />';
    fetchXML += '</entity>';
    fetchXML += '</fetch>';

    var results = XrmServiceToolkit.Soap.Fetch(fetchXML);

    if (results != null)    
    {
        for (index = 0; index < results.length; index++)
        {           
            var a = results[index].attributes["activitytypecode"].formattedValue; //returns activity type name - Phone Call

            var b = results[index].attributes["activitytypecode"].value;  //returns string - phonecall      
        }
    }       
}

all i want is the numerical value for the activity type - ie: Phone Call = 4120

I am using the latest 2.2.1 version of the XrmServiceToolKit.. i went hunting around inside there too and noted that inside the 'Fetch' method that as the results are built up, they get' deserialize'd.

From inside the doRequest of the Fetch method

var entity = new businessEntity();
entity.deserialize(fetchResult.childNodes[ii]);
fetchResults.push(entity);

I wonder if there is another method to use inside the XrmServiceToolkit... i even tried hacking in my own FetchRAW method to not 'deserialize' the result but i am not strong enough with JS really.. i surely must be doing something wrong.. i've successfully used the Fetch method for other entity data,

Answers:

Answer

you could use the WebAPI (Odata) and include odata.include-annotations=OData.Community.Display.V1.FormattedValue in the header of the call using the name Prefered.

function retrieveEntity(entityName, Id, columnSet) {
    var serverURL = Xrm.Page.context.getClientUrl();
    var Query = entityName + "(" + Id + ")" + columnSet;
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v8.0/" + Query, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
    req.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */ ) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                var data = JSON.parse(this.response);
                if (data != null {
                        alert(data["[email protected]e"]); //for lookup text
                        alert(data["[email protected]"]); //for optionset text
                    }
                } else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        };
        req.send();
    }

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/query-data-web-api#include-formatted-values

http://himbap.com/blog/?p=2077

Hope it helps - M.Acosta.D

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.