UI action to set field value from task table not working

dalewechsler
Mega Expert

 

Hello,

I'm having an argument with a UI action I've created. It's a simple request: click this button to set the value on some fields. It works except for one of the fields returns blank and what it's supposed to do is get a value from a field on the task table (this field doesn't appear on the form) and set that value on the incident form. here's one version of the code I've tried"

 

 

It's that third line of the function. I need the button to set the assignment group to the previous assignment group. We have an invisible field that keeps track of that, but it's on the task table. How do I get that field value into the assignment group with a UI action? I've tried dot-walking the field: 'task.u_previous...' but that didn't work. I tried 'current.u_previous...' and same.

More info: I have a business rule that does this perfectly when a certain field is updated, but the requirements have changed. They want a button. So, I tried this with Client unchecked and just used the code from the BR but it didn't work. Clicking the button returned to the previous screen (the list) without doing anything.

I feel the answer is so simple, but I'm not experienced enough at scripting and learning as I go. Any help would be greatly appreciated.

Here's the business rule that works, but is triggered by field change by member of current group, not button. They want a button (like yesterday).

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

 

if(gs.getUser().isMemberOf(current.assignment_group)){

current.state = 2;

current.u_on_hold_reason = '';

current.assignment_group = current.u_previous_assignment_group;

current.assigned_to = '';

}

 

})(current, previous);

 

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hi,

Could you please check with the below script in your UI action:

(function () {
	if(gs.getUser().isMemberOf(current.assignment_group)){
		current.state = 2;
		current.u_on_hold_reason = '';
		current.assignment_group = current.u_previous_assignment_group;
		current.assigned_to = '';
		current.update();
	}
})();

Also, please provide the code you used in UI action, it will be more clear to understand where is the issue exactly.

Please mark this correct/helpful, if it resolves your issue.

Thanks

View solution in original post

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

The screenshot of your UI Action didn't make it into your post.  But if you have a client type script and you want to reference a value, then that field must be on the form in order for the client code to have access to it.  I mention this because you mention its a hidden field.

Mahendra RC
Mega Sage

Hi,

Could you please check with the below script in your UI action:

(function () {
	if(gs.getUser().isMemberOf(current.assignment_group)){
		current.state = 2;
		current.u_on_hold_reason = '';
		current.assignment_group = current.u_previous_assignment_group;
		current.assigned_to = '';
		current.update();
	}
})();

Also, please provide the code you used in UI action, it will be more clear to understand where is the issue exactly.

Please mark this correct/helpful, if it resolves your issue.

Thanks

Mwatkins
ServiceNow Employee
ServiceNow Employee

Hi Dale,

I downloaded your screenshot and see the problem.

You are doing this:

g_form.setValue('assignment_group', 'u_previous_assignment_group')

That is wrong because it is trying to set the value of the Assignment Group field (which is a reference field) to the String value 'u_previous_assignment_group'. There's a couple things you can do.

  1. Mahendra's recommendation to make the UI Action server-side code will work
    OR
  2. You can keep the UI Action a client-side script and create a g_scratchpad object to pass the u_previous_assignment_group value to the client-side with a before/display business rule. Once you do that, you can access the value from the client-side. I like Mahendra's solution better. Much simpler.

Minimize Server Lookups

Scripting with Display Business Rules

GlideFormScratchpad Scoped API

dalewechsler
Mega Expert

Thank you for the replies, and sorry I'm so late with a follow-up, but I had a deadline. Now that I'm back to this, I was able to get the server side code to work. The first time I tried it it didn't - not sure why, but it seems to be working now. Thank you Mahendra and Mwatkins! Mwatkins, since having an initial problem when I tried the server side code, I was about to create a scratchpad object and pass the value, and your details would have been very helpful.