Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

Posted on by 1,579

I have a CRM Form, with 2 fields.

I need to automatically populate the second field with data returned from a different system, based on the value that the user keys into the first field.

I have a web service that accepts a text value, then goes out and runs a query against the external system and returns a value.

What I need to understand "how to do" is when the user keys in a value in new_casenum field on the CRM Form, I need CRM to call that the web service on change and whatever the web service returns needs to somehow go in the new_casenum field on the form, the user will then continue to enter data on the rest of the form before saving it.

I am not worried about error handling yet, I just need to get this concept down and get something very basic working so I can build up from there.

Can anyone who has done something like this offer some guidance as to how something like this can be done?

*This post is locked for comments

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    The MSCRM Shop samples are good to start. I now know the specific thing I have to persue is cross-domain web service -- but I am having difficulty making it work.

    If possible, do you know of a comprehensive tutorial that shows both the cross domain web service and the javascript that goes with it? Details here - community.dynamics.com/.../217492

    - [View:https://community.dynamics.com/crm/f/117/t/217492:750:50]

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    I broke this sub-issue out into a different question here https://community.dynamics.com/crm/f/117/t/217077  since it is its own distinct issue.

    This will allow credit to be assigned for assistance for this separate and distinct issue.

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    Removing that JQuery reference as you suggested, got me past that error.

    And now I am getting an unhandled exception error as shown - 0x800a1391 - JavaScript runtime error: '$' is undefined

    Seems it is having some kind of issue with the $ at the start of the Ajax code.

    Adding in

    return $.ajax({

    doesn't do anything to stop the error, and if I re-add the script source as seems to be recommended via a bunch of stack exchange articles- I go right back to the original error.

    I am learning fast, but just need to get over this set of road blocks.

    First time with ajax.

    Any help is greatly appreciated.

  • Suggested answer
    ansrikanth Profile Picture
    ansrikanth 3,115 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    Can you please remove the jquery reference part (src=•••••••) in the script tag and try?

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    If I do that, I get the same error - 0x800a1391 - JavaScript runtime error: 'GetJSONP' is undefined.

    Here is the modified code to match exactly as you suggested.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ClientSideGeneralTest._Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/.../xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    
    
    <head runat="server">
        <script language="JavaScript" type="text/JavaScript" src="/Scripts/jquery-3.1.1.min.js">
    
    function GetJSONP() {
        debugger;
        $.ajax({
            url: "aloyegeneraltest1/.../GetPriceJSON",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: '{"name":' + JSON.stringify(GetData()) + '}'
        }).done(function (result) {
            alert(result.d);
        }).fail(function (result) {
            alert(result.d);
        });
    }
    
          
    </script>
        
        <title></title>
    </head>
    <body>
       <form id="form1" runat="server"> 
    <div> 
    <input id="Button1" type="button" 
      value="Test" onclick="GetJSONP()" /><br /> 
    
    </div> 
    </form> 
    
        &nbsp;
    </body>
    </html>
    
  • Suggested answer
    ansrikanth Profile Picture
    ansrikanth 3,115 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    Hi

    Can you place the full <script> section inside the <head> and test it ?

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    I was able to modify my web service to serialize the output so that is returned in the format required for the JSONP that these MSCRMSHOP Articles talk about.

    The web service runs at http://aloyegeneraltest1/ReturnJSON.asmx

    and I can cause the results shown in the screen shot below to appear by accessing the web service at http://aloyegeneraltest1/ReturnJSON.asmx/GetPriceJSON 

    WebServiceReturningJSONP.png

    My problem is that I am unable to figure out how to properly write the JavaScript code that will actually get the serialized items shown in the web service above and separate them into the three values. When I run the page below, and click the "test" button, I get an error stating 0x800a1391 - JavaScript runtime error: 'GetJSONP' is undefined.

    I am new to development in general and am hoping for some basic guidance so I can at least see how to get the values returned from the web service into my JavaScript into and make each value appear in an alert. It may be basic stuff but it's beating me to  pulp so far. Any help would  be greatly appreciated. 

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ClientSideGeneralTest._Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script language="JavaScript" type="text/JavaScript" src="/Scripts/jquery-3.1.1.min.js">
    
    function GetJSONP() {
        debugger;
        $.ajax({
            url: "http://aloyegeneraltest1/ReturnJSON.asmx/GetPriceJSON",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: '{"name":' + JSON.stringify(GetData()) + '}'
        }).done(function (result) {
            alert(result.d);
        }).fail(function (result) {
            alert(result.d);
        });
    }
    
          
    </script>
        
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    
    
    <head runat="server">
        <title></title>
    </head>
    <body>
       <form id="form1" runat="server"> 
    <div> 
    <input id="Button1" type="button" 
      value="Test" onclick="GetJSONP()" /><br /> 
    
    </div> 
    </form> 
    
        &nbsp;
    </body>
    </html>
    


  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    Thanks Uriy, I've used JavaScript to do lots of stuff with CRM before, but not accessing web services, so I will check out the last 2 links (part 1 and part 2) and may have a few more questions...

    What I have done so far is this:

    I created the external web service that serves up data from several NON CRM systems, and that web servers sits out on a IIS Server - we will call it webservice.externalweb.local

    As a test, I created a plain vanilla non CRM C#.Net Web Forms application just to make sure I can consume the web service mentioned above, and I am able to get the data back using a regular .Net Web Forms app.... so there are no issues with the web service itself.  That plain vanilla C#.Net web forms app sits on genconsumptionapp.consumptionweb.local

    My CRM App sits at crmapp1.crmweb.local so I am hoping I can see how to use javascript to connect to webservice.externalweb.local and call the webservice to get back data that the web service is "serving up" and place some of that data into specific fields on my crm form using javascript.

    Once I get access to those url's I will check them out and post any questions if I have them.

    Thanks so much for your help.  

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to "Call a Web Service" from a plugin and "Receive the Data" it returns?

    If you want to call external web service on change of field you need to use javascript. But there may be problems with CORS if your external service hosts on different domain.

    Basically you need to create web resource, add it to your form and register function for on change event.

    Check this links for examples:

    www.dynamicscrmpros.com/getting-started-with-javascript-form-scripting-for-microsoft-dynamics-crm-2011

    mscrmshop.blogspot.ru/.../crm2011-and-cross-domain-calls-part-1.html

    mscrmshop.blogspot.ru/.../crm2011-and-cross-domain-calls-part-2.html

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans