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

Hints, Tips and Tricks

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

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

  • Comments 2
  • Likes

 

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.

 

Comments
  • Hi Jeff, can you suggest how I might tweak this to do the following: In the Lead screen, when I create a new company on the screen (ie select the "Find or add this company" link), I need to copy the values of some of the lead fields into the company table. (e.g. comp_currentsupplier from lead_currentsupplier). Should I use a post insert script?

  • Caili

    You might not need to do any programming.  Please can you look at the lessons 1750 on the page: www.sagecrm.com/.../videos.htm