XRM-OSS / Xrm-WebApi-Client

A promise-based JavaScript library for the Microsoft Dynamics CRM WebApi - TypeScript type definitions available
https://xrm-oss.github.io/Xrm-WebApi-Client/
MIT License
100 stars 28 forks source link

Can I run WebApiClient in the synchronous mode #5

Closed phuocle closed 7 years ago

phuocle commented 7 years ago

Some time, need return data from fetchXml in the synchronous mode. Then continue process the business logic. Can WebApiClient have an option do that ?

Thank.

DigitalFlow commented 7 years ago

Hello @phuocle,

This is not yet possible, however I was also thinking about this. I'll add an option for this, so that synchronous requests can be sent as well.

Kind Regards, Florian

DigitalFlow commented 7 years ago

Hey @phuocle,

The latest release includes the possibility to send requests synchronously.

How to do this is described here.

I'd be happy to receive some feedback on this.

Kind Regards, Florian

phuocle commented 7 years ago

My Custom Rule in ribbon command (it worked)

var canSendExtend = false;
function canSendExtendTrial() {
    if (canSendExtend) return true;    
    require(["WebApiClient", "Alert", "Lib", "entity/pl_License.Minify"], function (WebApiClient) {
        var f = new PL.pl_License.Form();
        var o = PL.pl_License.OptionSet;
        var fetchXml = [
            "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>",
            "  <entity name='email'>",
            "    <attribute name='activityid' />",
            "    <filter type='and'>",
            "      <condition attribute='pl_emailtype' operator='eq' value='500000000' />",
            "      <condition attribute='regardingobjectid' operator='eq' uitype='account' value='", f.pl_AccountId.getValue(), "' />",
            "    </filter>",
            "  </entity>",
            "</fetch>"
        ].join("");
        var request = {
            entityName: "email",
            fetchXml: fetchXml,
            async: false
        };
        var response = WebApiClient.Retrieve(request);        
        if (response.value.length === 0) {
            canSendExtend = true;
            Xrm.Page.ui.refreshRibbon();
        }            
    });    
    return false;    
}

WebApiClient now load in the synchronously. But, because it inside the require function, so the function load in the asynchronous . Now I use a template variable canSendExtend with Xrm.Page.ui.refreshRibbon() to control the button enable.

I want to completed remove template variable also the call function refresh ribbon. Now changed the function below, the code throw exception

function canSendExtendTrial() {
    var WebApiClient = require("WebApiClient"); //exception here
    //var WebApiClient = require("full path to WebApiClient");//exception here too
    var f = new PL.pl_License.Form();
    var o = PL.pl_License.OptionSet;
    var fetchXml = [
        "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>",
        "  <entity name='email'>",
        "    <attribute name='activityid' />",
        "    <filter type='and'>",
        "      <condition attribute='pl_emailtype' operator='eq' value='500000000' />",
        "      <condition attribute='regardingobjectid' operator='eq' uitype='account' value='", f.pl_AccountId.getValue(), "' />",
        "    </filter>",
        "  </entity>",
        "</fetch>"
    ].join("");
    var request = {
        entityName: "email",
        fetchXml: fetchXml,
        async: false
    };
    var response = WebApiClient.Retrieve(request);        
    if (response.value.length === 0) return true;    
    return false;
}

Could you tell me how I can archive this ? Thanks

DigitalFlow commented 7 years ago

Hello @phuocle,

Your second approach is still executing asynchronously because of the require call. If it has to be synchronous, you'll have to add the WebApiClient to your form as library. It will then be present automatically once the form is loaded, so you can just leave the require calls away and send your synchronous requests. Hope it helps.

Kind Regards, Florian

DigitalFlow commented 7 years ago

Hey @phuocle,

did it work for you in the end?

Kind Regards, Florian