Table Scripting: Difference between revisions

From Sage CRM Knowledge Base
No edit summary
No edit summary
 
(12 intermediate revisions by the same user not shown)
Line 18: Line 18:
----
----


function InsertRecord()
Altering CRM dates in Table level scripts
{
 
   // Handle insert record actions here
  var mydate = new Date();
}
  mydate.setDate(mydate.getDate() +7);
   Values("comp_customerstartdate") = mydate.getVarDate();
 
Ref: https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2011/03/07/setting-or-updating-date-fields-using-a-table-level-script.aspx
 
 
----
 
Comparing previous values (in cases where you might want to run some code only when a field value has changed)
 
Within the UpdateRecord method stub you would first query the current record using
 
  var crec=CRM.FindRecord("agreement",WhereClause);
 
You would then compare values as in
 
  if (crec("comp_name")!=Values("comp_name"))...
 
For date fields you have to create a Date object
 
  var c_comp_renewaldate = new Date(crec("comp_renewaldate"));
  var v_comp_renewaldate = new Date(Values("comp_renewaldate"));
 
and then call the getDate() method of the Date object to do the comparison.
You cannot compare the date objects as it will never match
 
  if (c_comp_renewaldate.getDate() != v_comp_renewaldate.getDate())...
 
 
----
 
Useful learning tool if your trying to work out when each of the TLS functions are called. 


var m_subject="";
Table level script will log all events to the SQL logs
var m_message="";
function PostInsertRecord()
{
//pre- 7.0
//var personTable="person";


var personTable="person,vPersonPE";
  function log(message, error) {try{CRM.CreateQueryObj('/* '+((error)?message:message+' */')).SelectSql();}catch(e){}}
var casep_sql="select * from CaseProgress (nolock) where "+WhereClause;
  function InsertRecord() { log("InsertRecord called",1); }
var casep_rec=CRM.CreateQueryObj(casep_sql);
  function PostInsertRecord() { log("PostInsertRecord called",1); }
casep_rec.SelectSQL();
  function UpdateRecord() { log("UpdateRecord called.  WHERECLAUSE=" + WHERECLAUSE,1); }
  function DeleteRecord() { log("DeleteRecord called. WHERECLAUSE=" + WHERECLAUSE,1); }


if (casep_rec.eof==false)
Ref: https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2011/08/19/how-do-you-know-if-your-table-level-scripts-are-being-executed.aspx
{
  var case_sql="select case_referenceid, case_description, Comp_PrimaryPersonId, Case_PrimaryPersonId from vsummarycase (nolock) where Case_CaseId="+casep_rec("case_caseid");


  var case_rec=CRM.CreateQueryObj(case_sql);
  case_rec.SelectSQL();


  m_subject="New Tracking Note on Case: "  + case_rec("case_referenceid") +"#Cases:"+case_rec("case_referenceid")+"#";
----
  m_body=casep_rec("case_progressnote");
 
  if (case_rec("Comp_PrimaryPersonId")==case_rec("Case_PrimaryPersonId"))
  {
    if(case_rec("Comp_PrimaryPersonId")+""!="undefined")
      {
    //send one email
    var pers_rec=CRM.FindRecord(personTable,"pers_personid="+case_rec("Comp_PrimaryPersonId")+" and Pers_sendEmailNotif='Y'");
    if (pers_rec.eof==false)
        {
            sendEmail(pers_rec("pers_emailaddress"),pers_rec("pers_firstname")+ " "+pers_rec("pers_lastname"));
        }
      }
  }else{
    //send two emails
    if(case_rec("Comp_PrimaryPersonId")+""!="undefined")
    {
    var pers_rec=CRM.FindRecord(personTable,"pers_personid="+case_rec("Comp_PrimaryPersonId")+" and Pers_sendEmailNotif='Y'");
    if (pers_rec.eof==false)
          {
            sendEmail(pers_rec("pers_emailaddress"),pers_rec("pers_firstname")+ " "+pers_rec("pers_lastname"));
          }
    }
    if(case_rec("Case_PrimaryPersonId")+""!="undefined")
    {
    var pers_rec2=CRM.FindRecord(personTable,"pers_personid="+case_rec("Case_PrimaryPersonId")+" and Pers_sendEmailNotif='Y'");
    if (pers_rec2.eof==false)
          {
            sendEmail(pers_rec2("pers_emailaddress"),pers_rec2("pers_firstname")+ " "+pers_rec2("pers_lastname"));
          }
    }
  }
}
}


Function to get the SID from within a table level script - you may want to do this to call an asp page via ajax for example


function sendEmail(email1, name1)
Based off of code at: https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2008/03/25/how-can-i-get-the-sid-in-serverside-scripting.aspx
{
  ErrorStr+=email1+":"+name1+":" +m_subject+m_body+"=====";
  return;


   var myMailObject = CRM.GetBlock("messageblock");
   function getsid()
  with (myMailObject)
   {
   {
     DisplayForm = false;
     var strPath = new String(CRM.URL(200));
    mSubject = m_subject;
     var arrayFullKeys = strPath.split("?");
     mBody = m_message;
     var arrayKeys = arrayFullKeys[1].split("&");
    mShowCC = true;
     for (var i=0;i<arrayKeys.length;i++)
    mShowBCC = true;
    AddRecipient(email1,name1,"TO");
     Mode=2; 
    Execute();
      
    if (mSentOK)
    {
      ErrorStr = "Email Sent";
    } 
    else 
     {
     {
      ErrorStr = "There has been a problem:  "+mErrorMessage;
      var arrayValue = arrayKeys[i].split("=");
    }
      if (arrayValue[0].toLowerCase()== "sid")
  }
      {
            return arrayValue[1];
      }
  }
  return "";
}
}


function UpdateRecord()


{
----


  // Handle update record actions here
To Make an Ajax GET request call from within a table script


}
  function MakeAjaxPostRequest(RequestPath)
  {
    var XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    XmlHttp.open('GET',RequestPath,false);
    XmlHttp.send();
    XmlHttp=null; // always clear the XmlHttp object when you are done to avoid memory leaks
  }




function DeleteRecord()
----


{
PostInsertRecord


  // Handle delete record actions here
https://community.sagecrm.com/user_community/f/84/t/10447.aspx
 
}

Latest revision as of 20:21, 18 July 2016


This sample shows you how to check if the field has been changed from one value to another

 function UpdateRecord()
 {
   if (Values("case_AssignedUserId")+""=="undefined")
     return;  //bad update from crm..or field not changed
   var CaseRec=CRM.CreateQueryObj("SELECT * FROM Cases WITH (NOLOCK) WHERE " + WhereClause);
   CaseRec.SelectSQL();
   if (CaseRec("case_assigneduserid")!=Values("Case_AssignedUserId"))
   {
     ErrorStr="field value has changed from "+CaseRec("case_assigneduserid") +" to "+Values("Case_AssignedUserId");
   }
 }



Altering CRM dates in Table level scripts

 var mydate = new Date();
 mydate.setDate(mydate.getDate() +7);
 Values("comp_customerstartdate") = mydate.getVarDate();

Ref: https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2011/03/07/setting-or-updating-date-fields-using-a-table-level-script.aspx



Comparing previous values (in cases where you might want to run some code only when a field value has changed)

Within the UpdateRecord method stub you would first query the current record using

 var crec=CRM.FindRecord("agreement",WhereClause);

You would then compare values as in

 if (crec("comp_name")!=Values("comp_name"))...

For date fields you have to create a Date object

 var c_comp_renewaldate = new Date(crec("comp_renewaldate"));
 var v_comp_renewaldate = new Date(Values("comp_renewaldate"));

and then call the getDate() method of the Date object to do the comparison. You cannot compare the date objects as it will never match

 if (c_comp_renewaldate.getDate() != v_comp_renewaldate.getDate())...



Useful learning tool if your trying to work out when each of the TLS functions are called.

Table level script will log all events to the SQL logs

 function log(message, error) {try{CRM.CreateQueryObj('/* '+((error)?message:message+' */')).SelectSql();}catch(e){}}
 function InsertRecord() { log("InsertRecord called",1); }
 function PostInsertRecord() { log("PostInsertRecord called",1); }
 function UpdateRecord() { log("UpdateRecord called.  WHERECLAUSE=" + WHERECLAUSE,1); }
 function DeleteRecord() { log("DeleteRecord called.  WHERECLAUSE=" + WHERECLAUSE,1); }

Ref: https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2011/08/19/how-do-you-know-if-your-table-level-scripts-are-being-executed.aspx



Function to get the SID from within a table level script - you may want to do this to call an asp page via ajax for example

Based off of code at: https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2008/03/25/how-can-i-get-the-sid-in-serverside-scripting.aspx

 function getsid()
 {
   var strPath = new String(CRM.URL(200));
   var arrayFullKeys = strPath.split("?");
   var arrayKeys = arrayFullKeys[1].split("&");
   for (var i=0;i<arrayKeys.length;i++)
   {
      var arrayValue = arrayKeys[i].split("=");
      if (arrayValue[0].toLowerCase()== "sid")
      {
           return arrayValue[1];
      }
  }
  return "";

}



To Make an Ajax GET request call from within a table script

 function MakeAjaxPostRequest(RequestPath)
 {
   var XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   XmlHttp.open('GET',RequestPath,false);
   XmlHttp.send();
   XmlHttp=null; // always clear the XmlHttp object when you are done to avoid memory leaks
 }



PostInsertRecord

https://community.sagecrm.com/user_community/f/84/t/10447.aspx