Setting field values on the basis of parent table

Udit5
Tera Contributor

Hi all,

 

I need to set some field values on the form which shall come from it's parent table when we click on new.

I am thinking to run onload client script but not sure what to write.

getReference will be used here i think.

As soon as new form open some fields should has values same as it's parent table.

Kindly help.

Thanks in advance.

 

2 REPLIES 2

Sagar Pagar
Tera Patron

Hi @Udit5,

You have to use the display business rule on you child table. Take the parent field/column values into g_scratchpad variables.

In onload Client scripts assign/ set Values in your child table if its new record.

 

Take a look at below sample scripts -

 

Display BR: Get Parent Values of Incidents to Incident tasks

Condition: current.isNewRecord() && !current.incident.nil()

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

	var parentInc = current.incident.getRefRecord();

	if (parentInc.isValidRecord()) {
		
		current.cmdb_ci = parentInc.cmdb_ci;
		current.assignment_group = parentInc.assignment_group;
		current.description = parentInc.description;
		current.priority = parentInc.priority;
		
	}

})(current, previous);

 

On-load Client Scripts:

function onLoad() {
	//Type appropriate comment here, and begin script below

	if (g_form.isNewRecord()) {

		g_form.setValue('cmdb_ci', g_scratchpad.cmdb_ci);
		g_form.setValue('assignment_group', g_scratchpad.assignment_group);

		g_form.setValue('description', g_scratchpad.description);
		g_form.setValue('priority', g_scratchpad.priority);

	}
}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Community Alums
Not applicable

Well, busines rule or client script are options, but if you want , having some prepolutated values, better use defaul values on the field configuration. This will prepopulate your fields, but the user still can change that data.

JoroKlifov1_0-1670174077587.png