The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to replace string value in a workflow script?

ishaanvohra
Kilo Contributor

Hello,

I have written a script in workflow and in that I have used the "replace" to replace the value in string value which I am getting through current.variables.

var Environment_Name = current.variables.Environment_Name ;

gs.info("varable value ",Environment_Name);

var env = Environment_Name.replace("Replace : ", "Hello");

variables.setDisplayValue('default_value',env);

variables.update();

Is there anything wrong with this script because it is storing "undefined " in the variable field.If I remove this "replace("Replace : ", "Hello")" then value stores perfectly in the variable. Can someone please tell where I am going wrong? Why is it showing undefined in variable value while using "replace"?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Ishaan,



Try this... (bold indicates what I changed)



var Environment_Name = current.variables.Environment_Name.toString() ;


gs.info("varable value " + Environment_Name);


var env = Environment_Name.replace("Replace : ", "Hello");


variables.setDisplayValue('default_value',env);


// variables.update(); // No such method update() for variables object


View solution in original post

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Can you please provide some example data?   For example is the Environment_Name variable a string and if so what is an example value?



This code isn't correct as "variables" isn't an available object:


variables.setDisplayValue('default_value',env);


variables.update();



You would need:


current.variables.default_value.setDisplayValue(env);


current.update();


Hi Michael,


This is the complete code.And assume sample data to be "Replace : xyz" and I want to remove "Replace : " and want to store "xyz" in that.


var variables=new GlideRecord("item_option_new");


var Environment_Name = current.variables.Environment_Name ;


gs.info("varable value ",Environment_Name);


var env = Environment_Name.replace("Replace : ", "Hello");


variables.setDisplayValue('default_value',env);


variables.update();


Chuck Tomasi
Tera Patron

Hi Ishaan,



Try this... (bold indicates what I changed)



var Environment_Name = current.variables.Environment_Name.toString() ;


gs.info("varable value " + Environment_Name);


var env = Environment_Name.replace("Replace : ", "Hello");


variables.setDisplayValue('default_value',env);


// variables.update(); // No such method update() for variables object


Hi Chuck,


Thanks for your response,script has worked.