Table Scripting: Difference between revisions

From Sage CRM Knowledge Base
No edit summary
No edit summary
Line 17: Line 17:


----
----


function InsertRecord()
function InsertRecord()
Line 32: Line 31:


var personTable="person,vPersonPE";
var personTable="person,vPersonPE";
var casep_sql="select * from CaseProgress (nolock) where "+WhereClause;
var casep_rec=CRM.CreateQueryObj(casep_sql);
casep_rec.SelectSQL();


   var case_sql="select * from vsummarycase (nolock) where "+WhereClause;
if (casep_rec.eof==false)
{
   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);
   var case_rec=CRM.CreateQueryObj(case_sql);
   case_rec.SelectSQL();
   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");
    
    
   m_subject="New Case Created: #Cases:"+case_rec("case_referenceid")+"#";
   if (case_rec("Comp_PrimaryPersonId")==case_rec("Case_PrimaryPersonId"))
  m_body=case_rec("case_description");
 
  if (case_rec("comp_customertypeit")=="Y")
   {
   {
     var user_where="";
    if(case_rec("Comp_PrimaryPersonId")+""!="undefined")
     if (case_rec("comp_itemployee1")+""!="undefined")
      {
    {
    //send one email
        user_where+="user_userid=" + case_rec("comp_itemployee1");
     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("comp_itsecondary")+""!="undefined")
     if(case_rec("Case_PrimaryPersonId")+""!="undefined")
    {
    {
        if (user_where!="")
     var pers_rec2=CRM.FindRecord(personTable,"pers_personid="+case_rec("Case_PrimaryPersonId")+" and Pers_sendEmailNotif='Y'");
          user_where+=" or ";
     if (pers_rec2.eof==false)
        user_where+="user_userid=" + case_rec("comp_itsecondary");
          {
    }
            sendEmail(pers_rec2("pers_emailaddress"),pers_rec2("pers_firstname")+ " "+pers_rec2("pers_lastname"));
    if (user_where=="")
          }
      user_where="1=2";
     var userRecord = CRM.FindRecord("users",user_where);
    while (userRecord.eof==false)
    {
          sendEmail(userRecord("user_emailaddress"),userRecord("user_firstname")+ " "+userRecord("user_lastname"));
          userRecord.NextRecord();
    }
  }else
  if (case_rec("comp_customertypesage")=="Y")
  {
    var user_where="";
    if (case_rec("comp_sageprimary")+""!="undefined")
    {
        user_where+="user_userid=" + case_rec("comp_sageprimary");
    }           
    if (case_rec("comp_sagesecondary")+""!="undefined")
    {
        if (user_where!="")
          user_where+=" or ";
        user_where+="user_userid=" + case_rec("comp_sagesecondary");
    } 
     if (user_where=="")
      user_where="1=2";   
    var userRecord = CRM.FindRecord("users",user_where);
    while (userRecord.eof==false)
    {
          sendEmail(userRecord("user_emailaddress"),userRecord("user_firstname")+ " "+userRecord("user_lastname"));
          userRecord.NextRecord();
     }
     }
   }
   }
     
}
}
}



Revision as of 10:59, 15 November 2012


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");
   }
 }



function InsertRecord() {

 // Handle insert record actions here

}

var m_subject=""; var m_message=""; function PostInsertRecord() { //pre- 7.0 //var personTable="person";

var personTable="person,vPersonPE"; var casep_sql="select * from CaseProgress (nolock) where "+WhereClause; var casep_rec=CRM.CreateQueryObj(casep_sql); casep_rec.SelectSQL();

if (casep_rec.eof==false) {

 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 sendEmail(email1, name1) {

 ErrorStr+=email1+":"+name1+":" +m_subject+m_body+"=====";
 return;
 var myMailObject = CRM.GetBlock("messageblock");
 with (myMailObject)
 {
   DisplayForm = false;
   mSubject = m_subject;
   mBody = m_message;
   mShowCC = true;
   mShowBCC = true;
   AddRecipient(email1,name1,"TO");
   Mode=2;  
   Execute();
   
   if (mSentOK)
   {
     ErrorStr = "Email Sent";
   }  
   else  
   {
     ErrorStr = "There has been a problem:  "+mErrorMessage;
   }
 }

}

function UpdateRecord()

{

 // Handle update record actions here

}


function DeleteRecord()

{

 // Handle delete record actions here

}