Using UpdateRecord in a table-level script

This example uses the UpdateRecord() function in a table-level script. The script sets all opportunities associated with a company to Stage Sale Closed, when the user changes the status to Archive.

  1. Go to <My Profile> | Administration | Customization | Company | TableScripts.
  2. Click New, and then create a new table-level script.
    For more information about the options you need to complete, see Creating a script.
    When creating your script, do the following:
    • From Script Type, select Table Level.
    • In Table Script, within the function UpdateRecord section, enter the following script:
  3. function UpdateRecord() 
    { 
    	// if company Status is changed to Archive then set all its companies 
    	// Opportunities to be Sale Closed 
    	if (Comp_Status == 'Archive') 
    	{ 
    	// sql string that will do the update for this company 
    	sql = "UPDATE Opportunity SET Oppo_Stage='Sale Closed' 
    	WHERE Oppo_PrimaryCompanyId=";
    	sql+=CRM.GetContextInfo('Company','Comp_CompanyId'); 
    	UpdateQuery = CRM.CreateQueryObj(sql); 
    	UpdateQuery.ExecSql();
    	}
    }
  4. Click Save.

Open a Company Summary, change the Status to Archive, and click the Opportunities tab.
The stage of every Opportunity related to the Company is set to Sale Closed.