4

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,

Paul O'Brien
  • 171
  • 1
  • 11
  • What is the value and type of `result.attributes["activitytypecode"]`? – James Wood Mar 01 '18 at 11:16
  • it appears to be a string. i am using XrmServiceToolkit to retrieve results inside a JS function. i've just looked at XrmToolbox (where i design the fetch queries). if i go and set the result to be 'raw fetch results' i can see the real value - 4210 but if i leave it default (Serialize - Explicit XML), i get a string result instead - phonecall - i guess i need to look how to get XrmServiceToolkit to return raw results some how? – Paul O'Brien Mar 01 '18 at 20:40
  • I'm confused, I don't get how it can be a `string` if it has properties like `formattedValue` and `value`. Might help if you post the code you are using - https://stackoverflow.com/help/mcve – James Wood Mar 02 '18 at 09:20
  • Cheers James, i have edited original solution and pasted the function and an example of the response. – Paul O'Brien Mar 07 '18 at 21:07

1 Answers1

0

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["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"]); //for lookup text
                        alert(data["paymenttermscode@OData.Community.Display.V1.FormattedValue"]); //for optionset text
                    }
                } else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        };
        req.send();
    }

https://learn.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