web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Using external REST api in CRM

(0) ShareShare
ReportReport
Posted on by

Hi guys,

Please Please help me out!!!!  Help

1. I have Rest Api and its hosted on our Server
2. I want to Integrate my Rest Api with Dynamic CRM 365.

Scenario:-
Suppose when user create any Accounts/Contacts in Dynamic CRM 365 and fill some value in Postal-Code/Zip-Code and click on Save Button.
On Click of save button , validate this Postal-code through Rest Api which is hosted on other server and API send respose to the Dynamic CRM 365 which is valid Postal code or not

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at
    RE: Using external REST api in CRM

    Hi Pankaj,

    You can create a JavaScript function which gets executed when OnSave event of the form triggers.

    The function may look similar to this.

    function UserAction() {
        var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "Your Rest URL Here", false);
        xhttp.setRequestHeader("Content-type", "application/json");
        xhttp.send();
        var response = JSON.parse(xhttp.responseText);
    }

    You can find many examples, if you google.

    Once you receive the response, if validated you can let CRM save the record, otherwise you can prevent the save action and display a warning.

    https://neilparkhurst.com/2015/11/11/javascript-prevent-save/

    saveEvent.preventDefault();

    Hope this helps.

    Cheers,

    Nadeeja

    My Blog: http://dyn365apps.com/ - Follow me on Twitter: https://twitter.com/dyn365apps

    LinkedIn: https://www.linkedin.com/in/nadeeja

  • Suggested answer
    Alagunellaikumar Profile Picture
    6,212 on at
    RE: Using external REST api in CRM

    Hi Pankaj,

    Is your Dynamics 365 is on premise or online.?

    Why did you need custom REST API because for address validation any way you are using third party webservice. You can even use plugin

    If you still need REST API, Please follow below step

    Step:1 If it is online then hosting your custom webservice it must be public

    Step:2 In onsave event check if the state and zipcode is get dirty then call your public REST api server using ajax call.

    If your CRM is onprimesis

    Step:1 Host your site in the CRM server.

    Step:2 In onsave event check if the state and zipcode is get dirty then call your REST api server using ajax call.

  • Suggested answer
    Alagunellaikumar Profile Picture
    6,212 on at
    RE: Using external REST api in CRM

    Hi

    You can also refer this

    arpitmscrmhunt.blogspot.in/.../call-external-web-service-using-custom.html

  • Community Member Profile Picture
    on at
    RE: Using external REST api in CRM

    hi Nadeeja Bomiriya,

    function onSave(context) {

       var saveEvent = context.getEventArgs();

       if (Xrm.Page.getAttribute("address1_postalcode").getValue() == null) {

              alert("Put in a zip/postalcode!");

           saveEvent.preventDefault();

       }

       else

       {

           UserAction();

       }

    }

    function UserAction()

    {

      var req = new XMLHttpRequest();

       req.open("POST", "localhost/.../GetNoteTypes;, true);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.onreadystatechange = function ()

       {

           if (this.readyState == 4 /* complete */)

           {

               req.onreadystatechange = null;

               if (this.status == 201)

               {

                   successCallback(JSON.parse(this.responseTex,_dateReviver).d);

               }

               else

               {

                   errorCallback(_errorHandler(this));

               }

           }

       };

       req.send();  

    }

    I am getting error "Unexpected end of JSON input at JSON.parse (<anonymous>)" .

    I have paste code above. "I want consume web API from external URL from dynamic crm 2016"

  • Community Member Profile Picture
    on at
    RE: Using external REST api in CRM

    Hi,

    I am not getting any response from external web api. I am calling external web API on click of onSave from Dynamic crm 2016. Please help me out.

    function onSave(context) {

       var saveEvent = context.getEventArgs();

       if (Xrm.Page.getAttribute("address1_postalcode").getValue() == null) {

           // *** Note: I am using an alert for testing a notification maybe better!

           alert("Put in a zip/postalcode!");

           saveEvent.preventDefault();

       }

       else

       {

           UserAction();

       }

    }

    function UserAction() {

       alert("DayTuesday");

      // var PCode = Xrm.Page.getAttribute("address1_postalcode").getValue();

      // var xhttp = new XMLHttpRequest();

      // xhttp.open("POST", "localhost/.../ValidatePostalCode&quot;, false);

      // xhttp.setRequestHeader("Content-type", "application/json");

       //xhttp.setRequestHeader("Accept", "application/json");

       //xhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");

     //  xhttp.setRequestHeader("Access-Control-Allow-Origin", "https://demotransportcrm.crm8.dynamics.com&quot;);

      // xhttp.send();

      // var response = JSON.parse(xhttp.responseText);

       var req = new XMLHttpRequest();

       req.open("POST", "localhost/.../GetNoteTypes;, true);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.onreadystatechange = function ()

       {

           alert("onreadystatechange");

           if (this.readyState == 4 /* complete */)

           {

               alert("readyState");

               var response = JSON.parse(req.responseText);

               alert(response);

               alert("response1");

               //req.onreadystatechange = null;          

               //if (this.status == 201)

               //{

               //    alert("201")

               //    successCallback(JSON.parse(this.responseText,_dateReviver).d);

               //}

               //else

               //{

               //    errorCallback(_errorHandler(this));

               //}

           }

       };

       req.send();  

    }

  • Verified answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at
    RE: Using external REST api in CRM

    Hi Pankaj,

    You need to have "http://" in front of localhost. Try that.

    Cheers,

    Nadeeja

  • Suggested answer
    sathis kumar Profile Picture
    on at
    RE: Using external REST api in CRM

    Hi Pankaj,

    As mentioned by Nadeeja, it is possible to call directly using the javascript but to reuse the same functionality across several and better usability, instead of calling directly in the javascript.

    I would recommend using Javascript to Call the CRM Actions for validating the result onSave Event. Please check this link ,how to call CRM Actions using javascript.

    In CRM Actions, using C# you can use the following code  to call the REST API:

    string URL = "REST API URL";
    WebRequest request = WebRequest.Create(URL);
    request.Method = "GET"; // GET (or) POST
    request.ContentType = @"application/json; charset=utf-8";
     
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string jsonResponse = string.Empty;
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
     jsonResponse = sr.ReadToEnd();
    }     
  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Using external REST api in CRM

    Hi,

    Thanks Every one !! I appreciate your response. :) :)

    I need another help, I have JavaScript Plugin which is call Web API on click of Onsave () function of Dynamic CRM 365.

    I want to register this Plugin in Microsoft App-source.

  • Verified answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at
    RE: Using external REST api in CRM

    Hi Pankaj,

    If any of the suggested answers help you resolve this problem, please mark them as verified.  This is help another person in the future.

    To answer your second question;

    Microsoft AppSource is similar to Apple Store, where Microsoft makes you go through a strict app validation process.  If you not planning to sell your App, there's no need to Publish to AppSource.  SImply use solutions to package up and deploy to other systems.

    More information can be found in below links.

    https://smp-cdn-prod.azureedge.net/documents/AppsourceGuidelines/Microsoft%20AppSource%20app%20review%20guidelines.pdf

    https://appsource.microsoft.com/en-us/partners#getStarted

    Cheers,

    Nadeeja

    If the answer solves your problem, please mark as Verified. Thanks.

    My Blog: http://dyn365apps.com/ - Follow me on Twitter: https://twitter.com/dyn365apps

    LinkedIn: https://www.linkedin.com/in/nadeeja

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans