AleksandrRogov / DynamicsWebApi

DynamicsWebApi is a Microsoft Dataverse Web API helper library for JavaScript & TypeScript
MIT License
272 stars 59 forks source link

Use failure (this is not a bug) #159

Closed rickeygong closed 1 year ago

rickeygong commented 1 year ago

DynamicsWebApi version For example: v2.1

Describe the bug this is not a bug

Expected behavior Couldn't get user id.

Actual result 'Hello Dynamics 365! My id is: undefined'

Code Snippet

if (typeof (Dealer) == "undefined") {
    Dealer = { __namespace: true };
}
Dealer = {
    _executionContext: null,
    _formContext: null,
    _lookupsupplierId: null,
    OnLoad: function (executionContext) {
        executionContext = executionContext;
        _formContext = executionContext.getFormContext();

    },
    SayHello: function(){
        //By default DynamicsWebApi makes calls to 
        //Web API v9.2 and Search API v1.0
        const dynamicsWebApi = new DynamicsWebApi();
        const response = dynamicsWebApi.callFunction("WhoAmI");
        Xrm.Navigation.openAlertDialog({ text: `Hello Dynamics 365! My id is: ${response.UserId}` });
    },
}

Steps To Reproduce If there are additional steps that need to be done to reproduce the behavior, describe them here:

  1. I put 'dynamics-web-api.js' in the dist folder onto the Dealer form.
  2. Then I custom added a JS(code above) and registered it in the Dealer form ,
  3. Call 'dealer.sayHello' when onload
  4. After publishing the solution, refresh the form and pop up 'Hello Dynamics 365! My id is: undefined'

Screenshots image image

AleksandrRogov commented 1 year ago

hi @rickeygong . that's strange. going to try to reproduce it.

AleksandrRogov commented 1 year ago

@rickeygong I think the reason why it's not working for you is because you missed the async/await declarations in your function. Here is the working script:

if (typeof Dealer == "undefined") {
  Dealer = { __namespace: true };
}
Dealer = {
  _executionContext: null,
  _formContext: null,
  _lookupsupplierId: null,
  OnLoad: function (executionContext) {
    executionContext = executionContext;
    _formContext = executionContext.getFormContext();
  },
  SayHello: async function () {                                   //<-- add async here
    //By default DynamicsWebApi makes calls to
    //Web API v9.2 and Search API v1.0
    const dynamicsWebApi = new DynamicsWebApi();
    const response = await dynamicsWebApi.callFunction("WhoAmI"); //<-- add await here
    Xrm.Navigation.openAlertDialog({
      text: `Hello Dynamics 365! My id is: ${response.UserId}`,
    });
  },
};