Using AJAX to access server-side objects client-side (for Sage CRM v7.1 and earlier)

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Using AJAX to access server-side objects client-side (for Sage CRM v7.1 and earlier)

  • Comments 26
  • Likes

Note:  This article discusses techniques relevant for Sage CRM v7.1 and earlier.  If you are using Sage CRM 7.2 please refer to the articles that discuss the new Client Side API.

*** See my recent blog post for information about an AJAX gotchya *** 


*** See my recent blog post for a different technique for getting server information client side *** 

Wouldn't it be nice to be able to access server-side objects, client-side?  The possibilities of what you could do would be endless. 

Well using a little ASP and some AJAX you can simulate this.  What is AJAX?  AJAX is short hand for Asynchronous Javascript And XML.  It is a web development technique used to retrieve data from the server asynchronously in the background without the need to refresh the entire page.  If you would like to learn more about AJAX there is a good resource here.

Say you added a new Person Search Select Advanced field to the Opportunity entity and also an e-mail address field that you wanted to have automatically populated based on what the user puts into the Person SSA.  Now you could do this with a table level script, populating the field after the record is submitted.  But what if you want the field populated straight away, to give the user an opportunity to edit the field?

You need to do a lookup on the Person table and grab the email address field, this is not something you can do client-side.  So we're going to write an ASP page to do the look up for us:

<!-- #include file ="accpaccrmajax.js"--><%
persid = Request.QueryString("pers_personid")
p = eWare.FindRecord('Person', 'pers_personid=' + persid);
Response.Write(p.pers_emailaddress);  // Write the e-mail address out to the response stream

Note that we are using an include called accpaccrmajax.js (you can find download this file here).  This is the accpaccrmnolang.js with some special modifications specifically for AJAX.  Normally accpaccrmnolang.js outputs <HTML><HEAD> & <BODY> tags.  We do not want these output when we are using AJAX we just want the results of our query so accpaccrmajax.js has these parts omitted.

Now we put the following code into the Custom Content of our screen:

function UpdateEmail() {
    var strQS = location.href.split(/\?/)[1]; 
var strAddr;
    if (window.location.toString().toLowerCase().search('eware.dll')==-1) {
        strAddr = window.location.toString().split('CustomPages')[0];
    } else {
        strAddr = window.location.toString().split('eware.dll')[0];
    persid = document.getElementById('oppo_extraperson').value;
    var strURL = strAddr + 'CustomPages/GetEmail.asp?pers_personid=' + persid + '&' + strQS;
    if (window.XMLHttpRequest) {
        XmlHttp = new XMLHttpRequest();
    } else {
        // IE6 and before
        XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    XmlHttp.setRequestHeader('Content-Type', 'text/xml');
    var strHtml = XmlHttp.responseText;
    XmlHttp=null; // always clear the XmlHttp object when you are done to avoid memory leaks
    document.getElementById('oppo_extraemail').value = strHtml;

Note our Person SSA is referred to as oppo_extraperson and our e-mail address field is oppo_extraemail.

And in the OnChange script of our Person SSA:


So you can see AJAX is quite powerful.  To quote Spidey's Dad, with great power comes great responsibility.  You must be careful not to create security holes in your application by exposing sensitive data in this way, someone could hijack your ASP pages and use them to query information that they shouldn't have access to. 

  • Not to put too fine a point on it but there is one major issue that I have with this article: Spiderman's parents were killed when Peter was very young, it was his Uncle Ben that is attributed with saying "with great power, etc..." in most versions of the story.

  • Touché!

  • There one annoying problem with using this technique that I have just discovered...  The pages you call using AJAX also get added to the recent list which means they come up if you hit the back / forward buttons.  It'd be great if there was a way to tell CRM not to include these pages in the history.  I have logged an enahancement request to have this added: 0-86552-ENH

  • Problem fixed!  Turns out there is a way already in the product to get around this.  Jeff describes it in his blog post here:

    I have updated the ajax include files to resolve this.

  • This is excellent. Thanks very much - worked like a charm

  • Hi There

    Does the same procedure work for custom ASP (COM API) pages?

    I have tested this using the Address entity and it works 100% as you have laid out, however, adding it to a custom entity using a custom ASP page it doesn't work.

    Any ideas?



  • Glad it's of use to you!

    The concept should work with ASP pages as well.  Can you post your code to the forum and send me a link to the forum and I'll take a look at it for you?

  • Something to note about this technique: it works fine on the client side, but Server side it seems to fall over. I have an ASP page that executes a system action using XMLHttpRequest, but I'm getting a response page with "IP Address Mismatch" on it. However, it works fine if the CRM you are calling is on the same machine as the browser calling it - i.e. Server and client are the same machine


  • Hi HarrisWTactec,

    I'm not surprised it failed.  It's a client side technique only.  I would suggest using the .com, .net or webservices api for server side operations.  What is it you are trying to achieve?

  • Hi Jack

    Sorry for the long delay in getting back to you.

    I have posted my code here

    Titled: Using AJAX to access server-side objects client-side - Using Custom ASP Pages

    Sorry about the formatting - not sure what happened. If you need me to re-post, please let me know



  • Hi Jack

    I have updated the post



  • Hi,

    your example works. But in the same maschine i´ve got a problem if i want to do the same with communication.

    I want to see a field from the person in the communication.

    But i have a script error: Access denied.

    The same code wit opportunity works.

    Have someone an idea?



  • @Wolff:

    Best create a new thread for that.  Post a link to the thread here.

    Where are you getting the script error?  Is it server side or client side?  

  • Jack, this was one of the most helpful posts I've come across.  Thanks very much indeed for posting this!

  • I have a problem with date fields. When a date field value come across into another date field it return the full date string e.g. Fri Mar 9 00:00:00 UTC 2012 and not 09/03/2012.

    Any idea how to make it present date fields correctly