title: Microsoft Dynamics CRM 365 calling an unbounded action via JavaScript asynchronously
body: <p>Is there a way to call an action via javascript without the use of third party scripts?</p>
  
  <p>I found this <a href="https://github.com/PaulNieuwelaar/processjs" rel="nofollow noreferrer">https://github.com/PaulNieuwelaar/processjs</a></p>
  
  <p>However, I cannot use third party libraries.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>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 <strong>true</strong>.</p>
  
  <pre><code>req.open(consts.method.post, oDataEndPoint, true);
  </code></pre>
  
  <p>// plugin</p>
  
  <pre><code>   public class RunAsync : CodeActivity
      {
          [Input("input")]
          public InArgument&lt;string&gt; Input { get; set; }
  
          [Output("output")]
          public OutArgument&lt;string&gt; 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);
              }
          }
      }
  </code></pre>
  
  <p>// javascript</p>
  
  <pre><code>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...');    
  }
  </code></pre>
  
  <p>// Action</p>
  
  <p><a href="https://i.stack.imgur.com/6miqJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6miqJ.png" alt="enter image description here"></a></p>