Business rule script to close/change state of related record

michaelsharp
Mega Expert

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();

  }

1 ACCEPTED SOLUTION

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);


View solution in original post

13 REPLIES 13

The project field referencing pm_project on the change_request table is u_project


The demand field referencing dmn_demand on the pm_project table is u_demand


The demand field referencing dmn_demand on the change_request table is also called u_demand



I had attempted to use the following script to dot walk the demand state through the project record and it did not update the demand state field



var prj = new GlideRecord("pm_project");


  prj.get(current.u_project);


  prj.state = '3';


  prj.u_demand.state = '3';


  prj.update();


Strange Michael.



Same code worked for me. Can you put some log statements in your BR and check? Also just wanted to double check the BR you have written is on the Change Request Table and it's an After Update one.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Shloke, I have it working with the other script I posted. As much as I'd like to figure out why the original suggestion didn't work, I'm fine with the solution as it is.



Thanks again


Ohh Ohkk. My Bad. Didn't read your earlier post completely.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke