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

Many thanks to   Zic,


Sorry for confusion .I am explaining again . we have custom field called State and we have   written   ACL   .After upgrade to Helsinki new field introduced   called Status(state)is contradicting our ACL(which having OBT functionality on server side ) .Our requirement to   200 project before upgrade to Helsinki .     so we have to set the filled values of   State   to >>>>>new OBT field Staus(state)   .



We have to write server side script to achive this .



Please suggest .



Regards,


Sadashiva


In that case, you would need to use "Fix script"


And on this this script, I would query the necessary projects using GlideRecord and on a "while loop", I would update the state.



One strong advice, in your query, use "setLimit" method so you would be able to make tests with few projects before saying "my script is good enough, I can do a final test to make sure everything is working fine".


ServiceNow Developers



Please make a try and come back with your proposition


renukaca
Kilo Contributor

Do this script updates the expected value old records also?


zica
Giga Guru

Das,



David is right, a fix script will do the work and do not forget his strong advice.


I don't know which table you are working on, here again a sample of the script that could be used (I am not the expert, David feel free to correct my script please )



   


UpdateStatusfromState();  
function UpdateStatusfromState ()   {  
var approval = new GlideRecord("tableName");  
approval.addQuery('state', 'value of work in progress'); // this only to update for instance records that have work in progress status
approval.addQuery(You can add more query here to be more accurate);


approval.setLimit(5); // Sets the limit for how many records are in the GlideRecord.
approval.query();      
while (approval.next()) {    
          approval.u_status = 'Value You want to put';  


approval.update();  


}}


zica
Giga Guru

David,



Thank you for your feedbacks


The sample of the Business rules proved is actually for Geneva version. However the fix script was made on Eureka version (and it is working).


I agree with you the role of the community isn't to undermine training, it's complementary. I just meant that we should assist them more and it is important for them to better understand the platform.