- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2017 01:53 PM
Seems like this should be a simple thing, but I have yet to find a solution, so I'm posting the question.
Our process is this.
Demand -> creates -> Project -> creates -> Change Request
The demand has a reference field for Project and Change Request to provide on screen status of the state of each of them
The project has a reference field for Demand and Change Request
The change request has a reference field for Demand and Project
What I'm trying to do is trigger a business rule when the Change Request state changes to "Closed"
I want this rule to run a script to change the state fields on the related Demand and Project records to a Closed state. (For this example, let's say that the state == 3)
I've tried a couple of options, but not getting anything to change the state.
scripting level = rookie
Here was an attempt that did not work:
var relproject = new GlideRecord("pm_project");
relproject.addQuery("change_request", current.sys_id);
relproject.query();
while (relproject.next()) {
relproject.state.setValue(3);
relproject.update();
}
var reldemand = new GlideRecord("dmn_demand");
reldemand.addQuery("dmn_demand", current.sys_id);
reldemand.query();
while (reldemand.next()) {
reldemand.state.setValue(3);
reldemand.update();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2017 07:34 AM
Thank you so much Sholke for pointing me in the right direction. I used your example, but couldn't get the Demand state to update. I verified the reference field names and it looks like it should work, but just wouldn't. My thought was that maybe the demand record was not updating after the state field was changed. I tried to dot walk an update command, but it didn't seem to work.
I ended up using the same syntax that you provided to perform a get on the dmn_demand table and update the state there and it works. Here's what my Business Rule script looks like.
(function executeRule(current, previous /*null when async*/) {
var prj = new GlideRecord("pm_project");
prj.get(current.u_project);
prj.state = '3';
prj.update();
var dmn = new GlideRecord("dmn_demand");
dmn.get(current.u_demand);
dmn.state = '3';
dmn.update();
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2017 01:59 PM
Update your script like below.
relproject.state = 3;
reldemand.state= 3;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2017 02:55 PM
replace the following lines
relproject.state.setValue(3);
reldemand.state.setValue(3);
with
relproject.setValue('state', 3);
reldemand.setValue('state', 3);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2017 03:57 PM
Ok, so we are closer, thank you for the input. Below is the current script.
The issue is that when I change the state of a change control, it changes the state of ALL projects and ALL demands to Closed/Complete. Good thing I'm in a test/sandbox instance.
If someone can help with the query part, I think we can get this working as designed. Thanks
(function executeRule(current, previous /*null when async*/) {
var relproject = new GlideRecord("pm_project");
relproject.addQuery("change_request", current.sys_id);
relproject.query();
while (relproject.next()) {
relproject.setValue('state', 3);
relproject.update();
}
var reldemand = new GlideRecord("dmn_demand");
reldemand.addQuery("dmn_demand", current.sys_id);
reldemand.query();
while (reldemand.next()) {
reldemand.setValue('state', 3);
reldemand.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2017 06:07 PM
Hi Michael,
The issue is the fields you're currently querying in the pm_project & dmn_demand tables. OOTB there is no pm_project.change_request or dmn_demand.dmn_demand (project doesn't have a link to change & the dmn_demand table has a field named just 'change').
If your company has added these reference fields as customizations then please recheck the field names & update the below lines accordingly;
- relproject.addQuery("change_request", current.sys_id);
- reldemand.addQuery("dmn_demand", current.sys_id);
An example of doing this with the OOTB fields;
(function executeRule(current, previous /*null when async*/ ) {
var rel_projects = [];
var demand_gr = new GlideRecord("dmn_demand");
demand_gr.addQuery("change", current.sys_id);
demand_gr.query();
while (demand_gr.next()) {
rel_projects.push(demand_gr.project);
gs.log("Updating " + demand_gr.getDisplayValue());
demand_gr.state = 9; // the 'Completed' state on the demand table has a value of 9 rather than 3
demand_gr.update();
}
var project_gr = new GlideRecord("pm_project");
project_gr.addQuery("sys_id", "IN", rel_projects.toString());
project_gr.query();
while (project_gr.next()) {
gs.log("Updating " + project_gr.getDisplayValue());
project_gr.state = 3;
project_gr.update();
}
})(current, previous);
Cheers,
Matt