How to write server side script to update the field values from one field to another field in Project module ?

Sadashiva Das
Tera Expert

How to write server side script to update the field values from one field   to another field     in Project   module ?

In Project we have field called State and Status.

My question is how to set the field value on State ( current value of state )will update or stet to   to Status field values in Project

Screen shot attached for reference

find_real_file.png

Please suggest

Regards,

Sadashiv Das

1 ACCEPTED SOLUTION

zica
Giga Guru

Das,



if you create a business rule, it will run on the server side (In opposite of client script that runs on client side)


This following article explains the difference between the scripts into servicenow, it will help you to get a broader perspective of how and where to script in Servicenow : Differences Among Scripts - ServiceNow Wiki



To answer to your question, you can create a Before business rules.


Put the table name on which the Business rule should operate. Do not forget to check also the "advanced" checkbox (as you will need to script a little bit)


Under the filter condition, you can put something similar to this condition : State changes


Under advanced tab, You have to write your script. I only provide you the sample of the script that you can use :



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


     


      if (current.state == '3')   { // make sure to put the value and not the label


              current.u_status = "ValueOfTheStateOfYourChoice1";


      }



      if (current.state == '4') {


              current.u_status = "ValueOfTheStateOfYourChoice2";


      }



})(current, previous);


View solution in original post

18 REPLIES 18

Hi Zic,



Thanks for reply


i am sory to say that i unable to find the answer and tried in community so that   some one can will help   help on this issue  


As i said about requirement that we have to set the field values   State u_state (old ) to Status(status) (New)


we have to update the values to   Status(status) we have around 200 old projects. and also help in roll back functionality .  


find_real_file.pngand


find_real_file.png


Regards,


Sadashiva Das


Sadashiva,



Have you got the admin right in your instance ? if so, search for 'Fix script" in the left navigation menu. Then create a "new" one.


Under the script tab, copy and paste the following script :



UpdateStatusfromState();  
function UpdateStatusfromState ()   {  


var count = '';
var approval = new GlideRecord("tableName");   // put the exact table name here
approval.addQuery('state', 'value of work in progress'); // this only to update for instance records that have work in progress status
approval.addQuery();// Delete it if no need, You can add more query here to be more accurate);
approval.query();      
while (approval.next()) {    
        count = count   + 1;


        gs.log('Number of record found is :   ' + count);          



}}



Make sure to put the right table name and one you are sure that the script retrieves all the record with the exact requirement, then add these following rows after the gs.log.


Rows to add :


        approval.u_status = 'Value You want to put';  


        approval.update();  




These two rows aim to change the status value and update the record.


If you want to have the same value than state, I think you don't need to script, just right click on status field, then dictionary. Scroll down and you should see the list choice. Add the value you want to display and make active false the old one.



You just need to script to update the old records with the right value


Hi Sadashiva



I just wanted to pop in to make sure you had all the information you need about the Helsinki change of cleaning up the ability to change project state / project task state without dealing with bugs from all the tentacles between project state and task state. You probably already know about it hence this change, but wanted to provide as much as possible to make this easier.



Here's some notes:



Added following attributes to state dictionary of planned_task


  • pending_states
  • default_pending_state
  • open_states
  • default_open_state
  • work_in_progress_states
  • default_work_state
  • default_skipped_state
  • close_states
  • default_close_state


find_real_file.png



PlannedTaskStateUtil provides key APIs


  • To find state bucket for a particular state or
  • To return default state for a state bucket



Few important APIs provided by PlannedTaskStateUtil


  • getDefaultStateForBucket
  • getBucketForState
  • isValidState
  • isStateInactive, isStatePending, isStateOpen and isStateWIP
  • getDefaultPendingState, getDefaultOpenState, getDefaultWorkState and getDefaultCloseState