sdata crud (update example using SID)

Hints, Tips and Tricks

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

sdata crud (update example using SID)

  • Comments 3
  • Likes

The following is an example of how to update a field, in this case an Oppo_OpportunityUId, via client side 'Custom Content' using an sdata2/sagecrm2 (CRUD) call. In this example the aim is to copy the hidden Opportunity Id to a new field so that it can be exposed to the end user as a unique reference identifier. We make use of the SID in order to bypass the need for authentication.

<script>
crm.ready(function(){
  // Manage Opportunity UId field
  if ($("#oppo_opportunityuid").length > 0)
  {
    // Edit Mode
    crm.fields("oppo_opportunityuid").collapse();
    var OppoId = parseInt(crm.getArg("Key7"));
    var OppoUId = parseInt($("#oppo_opportunityuid").val());
    if (!isNaN(OppoId) && isNaN(OppoUId))
    {
      crm.fields("oppo_opportunityuid").val(OppoId);
    }
  }
  else
  {
    // View Mode
    var OppoId = parseInt(crm.getArg("Key7"));
    var OppoUId = parseInt($("#_HIDDENoppo_opportunityuid").val());
    if (!isNaN(OppoId) && (isNaN(OppoUId) || OppoUId == 0))
    {
      // Build the sdata2 url
      var strUrl = crm.url(location.href, {parts:"sap"});
      strUrl = strUrl.split("eware.dll")[0];
      strUrl += "sagecrm2/-/opportunity('" + OppoId + "')";
      strUrl += "?SID=" + crm.getArg("SID", crm.url());
      // Build the data Object to update the UId
      var objOppo = {
        "Oppo_OpportunityUId": OppoId
      };
      // Ajax Update Call
      $.ajax({
        type: "POST",
        url: strUrl,
        data: JSON.stringify(objOppo),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) { $("#_Dataoppo_opportunityuid").text(data.oppo_OpportunityUID); },
        error: function(jqXHR, textStatus, errorThrown) { alert("Failed to update UId: " + errorThrown); }
      });
    }
  }
});
</script>

Comments
  • this is an amazing article Jonathan :) , do you have a sample of how this script could be used from an external application where we authenticate via the rest connection

  • You can do it in c# using a WebRequest:

         WebRequest aWebRequest = WebRequest.Create("http://localhost/sdata/crmj/sagecrm/-/company('43')");

         aWebRequest.Credentials = new NetworkCredential("Admin", "");

         WebResponse aWebResponse = aWebRequest.GetResponse();

         StreamReader sr = new StreamReader(aWebResponse.GetResponseStream());

         XmlDocument aXmlDoc = new XmlDocument();

         aXmlDoc.LoadXml(sr.ReadToEnd());

         this.richTextBox1.Text = aXmlDoc.OuterXml;

         sr.Close();

  • is sagecrm2 supported? havent seen it mentioned in any of the documentation yet..