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.

UI Action that updates record

Daniel142
Tera Expert

Hi everyone!

 

I need some help, I'm developing a UI Action that should insert a new record into the cost_plan_breakdown table.

For a Cost Plan, when I'm in one of those records,

Daniel142_0-1689708439518.png

I should be able to be inside the record, clic the UI Action and then create a new Cost Plan Break Down. Here is my code. Those are the list results. What i want is to insert the indicated record, the record that matches with the next one in the cost plan.

Daniel142_1-1689708710975.png

Daniel142_2-1689708979484.png

 

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Hi Daniel142,

 

I took a stab at creating a UI action based on your description. This is what I have:

 

Screenshot 2023-07-18 181834.png

 

The script follows:

 

 

(function(current, previous, gs, action) {

//	gs.addInfoMessage('Cost Plan = ' + current.name);

	// Get desired fiscal period
	var cpbFP = '';
	var curDateTime = new GlideDateTime();
	var fp = new GlideRecord('fiscal_period');
	fp.addQuery('fiscal_type', '=', 'month');
	fp.addQuery('fiscal_start_date_time', '>', curDateTime);
	fp.orderByDesc('name');
	fp.query();
	var fpSysid = '';
	// loop through results and capture the last value
	while (fp.next()) {
		fpSysid = fp.sys_id;
	}
//	gs.addInfoMessage("fiscal_period sys_id =  " + fpSysid + ".");

	// Now create the new record
	var cpb = new GlideRecord('cost_plan_breakdown');
	cpb.initialize();
	cpb.cost_plan = current.sys_id;
	cpb.fiscal_period = fpSysid;  // from logic from posted script
	// add additional field values as desired
	var sysId = cpb.insert();
	if (sysId)
		gs.addInfoMessage("Created new cost plan breakdown");
	else
		gs.addInfoMessage("Failed creating new cost plan breakdown.");

//	action.setReturnURL(current);
//	action.setRedirectURL(cpb.getGlideRecord());
})(current, previous, gs, action);

 

 

I took from another OOB UI Action as an example, and script logic from your image (modified as I couldn't copy that).  This seems to work, but can use improvement.

 

BTW, there is a New button on the cost_plan_breakdown related list on the cost_plan form.

View solution in original post

1 REPLY 1

Bert_c1
Kilo Patron

Hi Daniel142,

 

I took a stab at creating a UI action based on your description. This is what I have:

 

Screenshot 2023-07-18 181834.png

 

The script follows:

 

 

(function(current, previous, gs, action) {

//	gs.addInfoMessage('Cost Plan = ' + current.name);

	// Get desired fiscal period
	var cpbFP = '';
	var curDateTime = new GlideDateTime();
	var fp = new GlideRecord('fiscal_period');
	fp.addQuery('fiscal_type', '=', 'month');
	fp.addQuery('fiscal_start_date_time', '>', curDateTime);
	fp.orderByDesc('name');
	fp.query();
	var fpSysid = '';
	// loop through results and capture the last value
	while (fp.next()) {
		fpSysid = fp.sys_id;
	}
//	gs.addInfoMessage("fiscal_period sys_id =  " + fpSysid + ".");

	// Now create the new record
	var cpb = new GlideRecord('cost_plan_breakdown');
	cpb.initialize();
	cpb.cost_plan = current.sys_id;
	cpb.fiscal_period = fpSysid;  // from logic from posted script
	// add additional field values as desired
	var sysId = cpb.insert();
	if (sysId)
		gs.addInfoMessage("Created new cost plan breakdown");
	else
		gs.addInfoMessage("Failed creating new cost plan breakdown.");

//	action.setReturnURL(current);
//	action.setRedirectURL(cpb.getGlideRecord());
})(current, previous, gs, action);

 

 

I took from another OOB UI Action as an example, and script logic from your image (modified as I couldn't copy that).  This seems to work, but can use improvement.

 

BTW, there is a New button on the cost_plan_breakdown related list on the cost_plan form.