Trying to update value of field in table using scripts or business rules

mikestockman
Giga Contributor

I have a table u_enhancement_task that contains:

u_task_type, u_state, u_assigned_to, u_requirements_state, u_requirements_assigned_to

I'm trying to update the value in the u_requirements_state with u_state and u_requirements_assigned_to with u_assigned_to

where u_task_type = "Requirements".

Here's what I have set up using a business rule (not sure this is the best way):

In the Condition field, I have:

current.u_task_type == "Requirements"

In thescript window, I have:

updateRequirementsStateandAssign();

function updateRequirementsStateandAssign() {

var req = new GlideRecord("u_enhancement_task");

req.addActiveQuery();

req.addQuery("u_task_type", "Requirements");

req.query();

while (req.next()) {

    req.setValue(req.requirements_state, current.state);

    req.setValue(req.requirements_assigned_to, current.assigned_to);

  }

}

If I have the When the run set to :before or display,in a FORM it shows correctly, but it doesn't reflect the updates when I list table.

What did I miss?

1 ACCEPTED SOLUTION

Ok, I wouldn't have done this way but it does work


If you want, you can mark the answer as a solution (so people would see the issue has been answered)


View solution in original post

9 REPLIES 9

david_legrand
Kilo Sage

Hi Mike, I'll try to write what I would say to a colleague



1) I think you created your "u_enhancement_task" without extending the "task" table, according the rest of the code, I would probably extend the task table to inherit the nice features (state, SLA, metrics, list of work to do....) without any effort


==> So you might consider deleting this table and create a new table extending task



2) Why do you need to duplicate the fields like the u_state and the u_requirement_state specially if you copy the information?



3) If you want to copy the data like this, yes business rule is probably the best option



4) On the script, if you have the time, you should consider the scripting course from ServiceNow because as everyone (me the first when I started to work on ServiceNow), you're mixing "client side code" and "server side code"



5) Could you make screenshots (with fake data) of:


==> the business rule (with the table where the code is applying)


==> the form of the record you're modifying (where you click on save)


==> the form of the record you want to modify   automatically (is it the same "u_enhancement_task" or a different one?)



I need the information on the point 5 to continue to correct the small issues, we can probably simplify a lot your code



Regards,


I think taking the scripting course from SN is a GREAT idea. I will do that.


Thank you!!


Here are jpgs of the business rules' when to run tab, action tab, advanced tab, form where task_type=requirements, and the table list.


I modified script in advanced tab, but still am experiencing same issue. It shows properly on the screen when I view a form, but it doesn't update the table field.


task types table.jpgimageFile.jpgadvanced tab.jpgactions tab.jpgwhen to run tab.jpg


Perfect, you're very close to succeed but I'll be honest with you, you'll might want to redo your work on a later stage because I'm not sure the approach you took is the best one on the "functional point of view" (for reporting, daily work...).



So here I'll just help you to fix the technical issues:


"When to run" tab


1) You want to modify the "When" from "display" to "before" because you want ServiceNow to modify the value "requirement state" before it save the record in the database.


==> Business Rules - ServiceNow Wiki : 2.1 When Business Rules Run



2) When you do that, check the "insert" and "update" chveckboxes


==> You want ServiceNow to modify the value when you insert or when you update



Action tab:


1) Perfect



Advanced tab:


1) You don't need the condition because the "when to run" tab is doing the job


2) You don't need the script because the action tab is doing the job


3) This tab was the only way to use the business rules before Eureka release and is today used mainly for the "complex" scripts, here you don't need it


4) For the information, the script can't work because you have a small error "current.requirement_state" but the real name of your field is "u_requirement_state" (cf the message field in the Action tab) so you need to write "current.u_requirement_state"



So if you do these little modification, it'll work perfectly but again, you'll probably want to rethink the application at some point.



If you have more questions, feel free to tell



Regards,