Table Level Script - Basics

I have a need to update a custom field: comp_datelastnote (date/time) when a user inserts or updates a Note. I've been reading through posts for a few days to try to piece the solution together. Currently I'm here:

var myCompanyID = CRM.GetContextInfo('Notes','note_foreignid');
var myUpdatedDate = CRM.GetContextInfo('Notes','note_updateddate');
var myCompanyRecord = CRM.FindRecord('Company', 'comp_companyid='+myCompanyID);

myCompanyRecord.comp_LastNoteDate = myUpdatedDate;
myCompanyRecord.SageChanges();

The code above is returning an error message on the 5th line: "myCompanyRecord.comp_LastNoteDate = myUpdatedDate;"

Error: "Object doesn't support this property or method..."

Any guidance would be appreciated.

  • 0

    Your line

    myCompanyRecord.SageChanges();

    Should be

    myCompanyRecord.SaveChanges();

  • 0

    The insert and the update are going to be different.

    In the case of an update then the Note record will exist and will be in context.

    In the case of an insert the record will not yet exist and so can not be available to a GetContextInfo() call.

    An insert event function is primarily used for entity scripts where you may wish to carry out a pre-commit rollback. For other entities like Notes, you are better off using the PostInsert event function.

    But here you can not use the GetContextInfo because a new record has not yet be returned in context. You can however use the system variable 'whereclause'.

    function PostInsertRecord()

    {

    // Handle post insert record actions here

    var intRecordId = CRM.GetContextInfo("company","comp_companyid");

    var myRecord = CRM.FindRecord("company","comp_companyid="+intRecordId);

    var noteRecord = CRM.FindRecord("notes",WhereClause);

    myRecord.comp_note = noteRecord.note_note;

    myRecord.SaveChanges()

    }

    or

    function UpdateRecord()

    {

    //Get Context Info

    var intRecordId = CRM.GetContextInfo("company","comp_companyid");

    var myRecord = CRM.FindRecord("company","comp_companyid="+intRecordId);

    myRecord.comp_note = Values("note_note");

    myRecord.SaveChanges()

    }

  • Thanks Jeff - an unfortunate miss on my part, but not my issue:

    Updated Code - same error - when inserting a Note:

    function InsertRecord()

    {

    var myCompanyID = CRM.GetContextInfo('Notes','note_foreignid');javascript:void(0);

    var myUpdatedDate = CRM.GetContextInfo('Notes','note_updateddate');

    var myCompanyRecord = CRM.FindRecord('Company', 'comp_companyid='+myCompanyID);

    myCompanyRecord.comp_LastNoteDate =myUpdatedDate ;

    myCompanyRecord.SaveChanges();

    }

  • Thank you Jeff - an amazingly powerful tool once you grasp the flow and conventions - much appreciated! Final code for reference:

    function PostInsertRecord()

    {

    // Handle post insert record actions here

    var intRecordId = CRM.GetContextInfo("company","comp_companyid");

    var myRecord = CRM.FindRecord("company","comp_companyid="+intRecordId);

    var noteRecord = CRM.FindRecord("notes",WhereClause);

    myRecord.comp_LastNoteDate = noteRecord.note_createddate;

    myRecord.SaveChanges()

    }