Creating Ajax calls using the Client Side API.

Hints, Tips and Tricks

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

Creating Ajax calls using the Client Side API.

  • Comments 5
  • Likes

Below is a screen shot of an Opportunity Summary page. You can see that as the mouse hovered over the Company Name in the 'For' panel of the screen a tool-tip appeared that displayed the company's Status and Type. This is information that is not normally available within the browser when looking at an Opportunity record. The comp_status and comp_type fields are not present anywhere within the HTML of the page. This is data that has been retrieved from the server and added to the screen using an Ajax call.

The Sage CRM Client Side API has methods which make the retrieval and parsing of data very easy.

The code used for the example above is shown below. This can be added to the OpportunityDetailBox screen definition.

code language="javascript"]
<script>
//Custom Content - SData (Ajax) Request Example
crm.ready(function()
{
//
var compField = crm.fields("oppo_primarycompanyid");
var companyID = compField.value();
var successCompany = function(crmRecord)
{
//crm.infoMessage(crmRecord.comp_name);
compField.title("Status: "+crmRecord.comp_status +"\n Type: "+crmRecord.comp_type);
}
crm.sdata({
entity: "company",
id: companyID,
success: successCompany
});
})
</script>
[/code]

The crm.sdata() method is described in the documentation.

http://help.sagecrm.com/js-api/classes/sdata.html#method_sdata

The documentation explains the parameters that you can use which are passed to the crm.sdata() method as a JSON object. The properties of the parameter object allow you to either specify that a single record or a set of records should be returned.

The code above will grab the current value of the oppo_primarycompanyid field. It uses that value in the parameter object of the sdata() method. The function successCompany is executed if the sdata request returns without error. The crmRecord object is passed automatically to the success function.

Comments
  • Jeff,

    Using the above method, I am having trouble setting a selection field value from the company record into an opportunity field with the same dropdown selections (and name) upon creating the record which is a new opportunity from the company in context.  It displays the value correctly using the crm.infoMessage but the correct value is not selected in the dropdown.  It appears to work if the value is displayed in the translations without a space eg: EQUITYHOLDER but, when a space is inserted eg: EQUITY HOLDER, the value is no longer selected.  Is there a better method for selecting the actual selected "code" to copy across?

    Here is what I have - posted in custom content (while testing), oppo detail box:

    Appreciate some pointers here as I am baffled :/

    Kind Regards,

    Penny

  • The code wasn't shown below.  Could you reenter?

  • Hi Jeff,

    Sorry.  I reposted the question in a new post and Rob Hurson answered my question.  It can be found here: community.sagecrm.com/.../32225.aspx

    Thanks for responding though Jeff!

    Regards,

    Penny

  • I need to create a multi-line address string, pulling address fields via SData in an OnChange script.

    SData returns [object Object] for addr2 using the following code where addr_address2 is NULL. Trying to figure out how to test for this since I don't want to concatenate null values into the multi-line text field. The script doesn't work with (addr2 !=NULL) and (addr2 !=''). The below code returns the string:

    3001 Spruce St.

    [object Object]

    Code:

    var addrID = crm.fields("oppo_address").value();

    var address = "test";

    var addr1 = "";

    var addr2 = "";

    var successFunction = function (crmRecord) {

     addr1 = crmRecord.addr_address1;

     addr2 = crmRecord.addr_address2;

     if (addr2 != 'undefined') {

     address = addr1 + "\r" + addr2;

     }

      crm.fields("oppo_addresstext").value(address);

    }

    crm.sdata(

    {

      entity: "address",

      id: addrID,

      success: successFunction

    });

  • Paul

    See the example code below.  The default conversion from an object to string is "[object Object]".  This is annoying when the object returned by SData call is empty.  This is easy enough to fix with a function.  I am sure someone will come up with a more elegant solution.

           var checkObjectString = function (fieldValue)

           {

               //The default conversion from an object to string is "[object Object]"

               //Where the string is used in the interface this needs to be changed to an empty string

               //usage checkObjectString(crmRecord.comp_type);

               if (fieldValue == "[object Object]")

               {

                   fieldValue = "";

               }

               return fieldValue;

           }

           var successCompany = function (crmRecord) {

               compField.title("Status: " + checkObjectString(crmRecord.comp_status) + "\n Type: " + checkObjectString(crmRecord.comp_type));

           }