Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Dynamic creation of reference field value

Anirudh Gupta
Mega Expert

I have a scenario where my Table A has a reference field (lets say X) which is referring to Table B. The reference field (X) on Table A is being populated by BR on run-time. So if a matching value of it is found in Table B the same value should get populated in the field else the BR should create the value in table B and populate it in Table A field (X). I have used 

Enable dynamic creation for reference fields

But seems it isn't working.

In Detail : 

find_real_file.png

var MyUserReferenceCreator = Class.create();
MyUserReferenceCreator.prototype = {
	initialize: function() {
	},
	
	create: function(current) {
		gs.log("Inside MyUserReferenceCreator");
		var month_name=new Array(12);
		month_name[0]="January";
		month_name[1]="February";
		month_name[2]="March";
		month_name[3]="April";
		month_name[4]="May";
		month_name[5]="June";
		month_name[6]="July";
		month_name[7]="August";
		month_name[8]="September";
		month_name[9]="October";
		month_name[10]="November";
		month_name[11]="December";
		
		var gdt = new GlideDateTime();
		var value = month_name[gdt.getMonthUTC()-1]+"-"+ gdt.getYearUTC();
		gs.log("Inside MyUserReferenceCreator value : "+ value);
		current.u_cross_charge_month_year = value;
		return current.insert();
	},
	type: 'MyUserReferenceCreator'
};

Even the log statements aren't visible in the logs. which means the script isn't executing.

Any help is highly appreciated.

 

EDIT :  This functionality works when I insert value in u_cross_charge_month_year from form view. But needs to achieve this from BR.

find_real_file.png

 

1 ACCEPTED SOLUTION

This issue is resolved. Had to go with the usual approach of creating a new field and assigning the sys_id in reference field using script include and business rule. Seems the dynamic creation of reference field usually works on front end only.

View solution in original post

3 REPLIES 3

Dubz
Mega Sage

Try adding javascript: before you call the script include. I've not configured this before but it looks the same as how you call an advanced reference qualifier.

javascript: new MyUserReferenceCreator().create(current);

Not working in this case 😞

This issue is resolved. Had to go with the usual approach of creating a new field and assigning the sys_id in reference field using script include and business rule. Seems the dynamic creation of reference field usually works on front end only.