Get LocalTimeFromUtcTime in JavaScript using Xrm.WebApi.online.execute function call.

sometime we need to get the local time of use in local timezone set in personal setting of dynamics using client side script.

I needed to make sure that i am passing each and everything correctly in Xrm.WebApi.online.execute as this function throws very generic errors and doesn’t give any clue of real missing parameter or syntax error.

Code sample with some comments :

////Microsoft Documentation can be found on this url “https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/online/execute”

function LocalTimeFromUtcTimeRequest(timeZoneCode, utcTime) {
this.TimeZoneCode = timeZoneCode;
this.UtcTime = utcTime;
}

LocalTimeFromUtcTimeRequest.prototype.getMetadata = function () { //// Make sure to write the cunstructor in this way so that the metadata goes to proto
return {
boundParameter: null,
parameterTypes: { ////This property needs two parameters TimeZoneCode, UtcTime
“TimeZoneCode”: {
“typeName”: “Edm.Int32”,
“structuralProperty”: 1 ////1 is for PrimitiveType data structures
},
“UtcTime”: {
“typeName”: “Edm.DateTimeOffset”, ////For Dates we need to use DateTimeOffset and not the DateTime
“structuralProperty”: 1 ////1 is for PrimitiveType data structures
}
},
operationType: 1, //// We need to make sure what is the operation type in this example it is “Function” Hence passed 1
operationName: “LocalTimeFromUtcTime” ////Notice the operationName and don’t be confused with C# request name LocalTimeFromUtcTimeRequest
};
};

var localTimeFromUtcTimeRequest = new LocalTimeFromUtcTimeRequest(20, “2020-05-28T12:39:10Z”); //Date Format is strickly should be in this format

Xrm.WebApi.online.execute(localTimeFromUtcTimeRequest).then(
async function success(result) { //// make the function sync otherwise below await for result.json() is not possible.
debugger;
if (result.ok) {
debugger;
var data = await result.json(); ////Simple return statement won’t work as there is always a Promise return so we need to await for that result
return data;
}
},
function (error) {
debugger;
Xrm.Utility.alertDialog(error.message);
}
);

2 thoughts on “Get LocalTimeFromUtcTime in JavaScript using Xrm.WebApi.online.execute function call.

Leave a comment