0

Is there a way to call an action via javascript without the use of third party scripts?

I found this https://github.com/PaulNieuwelaar/processjs

However, I cannot use third party libraries.

UPDATE:

Here is some sample code that demonstrates an asynchronous call to an action via JavaScript. A important point to remember is to make the last parameter of the open method of the request to true.

req.open(consts.method.post, oDataEndPoint, true);

// plugin

   public class RunAsync : CodeActivity
    {
        [Input("input")]
        public InArgument<string> Input { get; set; }

        [Output("output")]
        public OutArgument<string> Output { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                Thread.Sleep(20000);
                Output.Set(executionContext, $"Result:{Input.Get(executionContext)}");                
            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
    }

// javascript

function callAction(actionName, actionParams, callback) {

    var result = null;
    var oDataEndPoint = encodeURI(window.Xrm.Page.context.getClientUrl() + consts.queryStandard + actionName);

    var req = new XMLHttpRequest();
    req.open(consts.method.post, oDataEndPoint, true);
    req.setRequestHeader(consts.odataHeader.accept, consts.odataHeader.applicationJson);
    req.setRequestHeader(consts.odataHeader.contentType, consts.odataHeader.applicationJson + ";" + consts.odataHeader.charset_utf8);
    req.setRequestHeader(consts.odataHeader.odataMaxVersion, consts.odataHeader.version);
    req.setRequestHeader(consts.odataHeader.odataVersion, consts.odataHeader.version);
    req.onreadystatechange = function () {
        if (req.readyState === 4) {
            req.onreadystatechange = null;
            if (req.status === 200) {
                if (callback) {
                    result = JSON.parse(this.response);
                    callback(result);
                }
            } else {
                console.log(JSON.parse(this.response).error);
            }
        }
    };
    req.send(JSON.stringify(actionParams));
}

function onLoad() {

    console.log('call action...');

    var actionParams = {
        Input: 'test1234'                            
    };

    callAction('TestAsyncAction',actionParams, function(data){              
        console.log('action callback triggered...');
        console.log(JSON.stringify(data));
    });

    console.log('action called...');    
}

// Action

enter image description here

J.S. Orris
  • 4,653
  • 12
  • 49
  • 89
noobie
  • 2,427
  • 5
  • 41
  • 66

1 Answers1

4

You can use webapi to execute custom Action. This is wrapped in XMLHttpRequest & can be called asynchronous.

/api/data/v8.2/Action_Name

For asynchronous run:

req.open(....., true);

The same using soap call (not recommended).

Processjs uses Organization.svc/web which is going to be deprecated.

  • I have noticed it is actually executing synchronously and verified this with a dummy plugin that waits 20 sec and console messages that print before and after the action was called and on the callback of the action. will upload the code. Here is an article that also demostrates the call is synchronous - http://blogs.microsoft.co.il/rdt/2016/01/13/executing-custom-action-via-javascript/ Still checking for additional info online. Any ideas? – noobie Aug 23 '17 at 23:46
  • 1
    brilliant! it worked. thanks for your help. I will update my question with the code for others. – noobie Aug 23 '17 at 23:56