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, Deepa,



I need a help in the below scenario...



I created a column as "Change Status" in the task_ci table. I set a default value as "In Progress". That column is a choice field, where the choices are "Successful" & "Unsuccessful".



I need that default value as "Waiting For Review" when I am moving to Review state of a change request form.



So for this, I tried a lot with Business Rule but not succeeded yet. As I don't have the knowledge regarding Glide so I am unable to do this.



Can you please help me with the code to do the same???


David,



It could be tricky for somebody who never followed scripting courses or for somedody new into ServiceNow.


I think that we should assist them and provide them articles that could help them to better understand the platform.


Those who used to script and already gone through the same query could easely achieve it because we already know the secret but It is not easy for new people in the way that you have to take care about the value you put, the script in itself, etc...


I have a few remarks actually



1) the code was assuming the instance was running Geneva or Helsinki right? I'm not sure the code would have worked on Fuji and I'm quite sure it wouldn't work on Eureka.


2) "It could be tricky for somebody who never followed scripting courses or for somedody new into ServiceNow." ==> This is (from my point of view) the difference between "training" and "community", the role of the community isn't to undermine training, it's complementary .


3) Maybe I'll try to write a blog post one day about "how do we learn" because we have different way to learn and each method has pros and cons. And here for mandatory knowledge I do think the "step-by-step exercise" is better than "do this and mark my answer correct"  



Obviously, I wouldn't enforce anything, just propose and if people prefer to learn by exercising more than by copying, it'll become the main approach on the community.


LOL, when IS ServiceNow community going to add that NOT HELPFUL button??? 

If I had a nickel for every time I learned something from the community, I'd be, well richer than I am now. 

 

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