A Table Level Script to update Opportunities when a Company is Reassigned

Less than one minute read time.

A customer had the requirement that when a company was reassigned to a different account manager, the opportunities that belonged to that company that were still in progress should automatically be reassigned to the new company account manager.

This was accomplished using an UpdateRecord event function in a table level script on the company.


function UpdateRecord()
{
if (CRM.GetContextInfo('Company','comp_primaryuserid') != Values('comp_primaryuserid'))
{
var myCompRecordId = CRM.GetContextInfo('company','comp_companyid');
var strSQL = "oppo_status = 'In Progress' and oppo_primarycompanyid="+myCompRecordId;
var myOppoRecord = CRM.FindRecord('opportunity', strSQL);
while (!myOppoRecord.EOF)
{
myOppoRecord.oppo_assigneduserid = Values('comp_primaryuserid');
myOppoRecord.NextRecord();
}
myOppoRecord.SaveChanges();
}
}

Note:

The line that detects the change in the company account manager is:


if (CRM.GetContextInfo('Company','comp_primaryuserid') != Values('comp_primaryuserid'))

I can use the fact that GetContextInfo in Table Level Scripts detects the original database value of the field, whereas the Values() collection grabs the new value being submitted from the interface. If the two differ then a change has taken place.

The rest of the code then loops through the opportunities that belong to the company and reassign them to the new user.