Populating the value from a field on one table to a field on another table

Community Alums
Not applicable

I need to copy the value from one field on one table to a field on another table. What is the best way to configure this?

9 REPLIES 9

Community Alums
Not applicable

Measure name is common between the two tables. Field value is 'name'.

Hi, 

Create a new BR , and select the table name [ sn_gf_program_measure] 

 

AshishKMishra_0-1699391194898.png

 

AshishKMishra_1-1699391261649.png

 

Code as written in Advance tab:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var grFisccalYear = new GlideRecord("sn_gf_fiscal_year");
	 	grFisccalYear.addQuery("name", current.name);  //find matched record in fiscal year table 
		grFisccalYear.query(); 
		//iterate the result set and copy the data to fiscal year table
		if( grFisccalYear.next()){
			grFisccalYear.u_target_modifier = current.u_target_modifier;
			grFisccalYear.u_measure_reporting_type= current.u_measure_reporting_type;
			// update the record in fiscal year 
			grFisccalYear.update();
		}

})(current, previous);

 

Try with above code in BR, after this , any new insert or update in program measure record will update those two fields in fiscal year table for matched name value.  

 

Let us know the result , if case, BR is not updating fiscal year record. 

 

Note: considering there is only 1 name value record present in fiscal year table.

 

-Thanks,

AshishKMishra

Please accept solution and mark helpful, if it helps you.


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Community Alums
Not applicable

I created the business as directed and created a new program measure record. It did not update the field values in the fiscal year table for the same measure record.

The business rule will trigger only when there is any new record insert and existing record update in program measure table,  

 

If you have program measure records and trying the update those two column in fiscal year table then write the fix script with code shared by @Bert_c1 , this fix script code will update existing data in one go. 

 

Add the logs in code to track the execution flow.

How many such record in program measure table? 

 

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

var gfProgram = new GlideRecord('sn_gf_program_measure');
gfProgram.addEncodedQuery('[encoded_query]');	// Get that value from a list view of sn_gf_program_measure, filtered by whatever you like
gfProgram.query();
gs.info("Script: Found " + gfProgram.getRowCount() + " records to process.");
while (gfProgram.next()) {
	var fiscalYear = new GlideRecord('sn_gf_fiscal_year');
	fiscalYear.addQuery('name', gfProgram.name);
	fiscalYear.query();
	if (fiscalyear.next()) {
		gfProgram.u_target_modifier = fiscalyear.u_target_modifier;
		gfProgram.u_measure_reporting_type = fiscalYear.u_measure_reporting_type;
		gfProgram.update();
	}
}

assuming you want to copy from 'sn_gf_fiscal_year' to record in 'sn_gf_program_measure'.

 

If you want to copy fields in a Business rule, defined on the 'sn_gf_program_measure' table to copy to 'sn_gf_fiscal_year' table, use:

 

	
	var fiscalYear = new GlideRecord('sn_gf_fiscal_year');
	fiscalYear.addQuery('name', current.name);
	fiscalYear.query();
	if (fiscalYear.next()) {
		fiscalYear .u_target_modifier = current.u_target_modifier;
		fiscalYear .u_measure_reporting_type = current.u_measure_reporting_type;
		fiscalYear .update();
	}

if the function body.