How to set multiple variable values in a single variable using Run Script(workflow)

Anusha25
Tera Contributor

After creating Request, few variables like,

variable 1 : A 
                  B
                  C
                  D

variable 2: A1
                  B1
                  C1

variable 3:A2
                 B2
                 C2 etc

In Variable 4(multiple line text)the above 3 variable values should populate  with semicolon and space for each value.

29 REPLIES 29

Hi Anusha,

Just tried in my PDI as you expected.

find_real_file.png

Logic:

var c, d;
c = current.variables.details1.toString().split("\n"); //map your variable names
d = current.variables.details2.toString().split("\n");
for (var i = 0; i < c.length; i++) {
    current.variables.details3 = current.variables.details3 + c[i] + ";";
}
for (var j = 0; j < d.length; j++) {
    if (d.length == j + 1) {
        current.variables.details3 = current.variables.details3 + d[j] ;// to ignore semicolon for the last word
    } else {
        current.variables.details3 = current.variables.details3 + d[j]+ ";";
    }
}

Hope it helps

 

Thanks,

Murthy

Thanks,
Murthy

Hi Murthy, It was worked for 2 variables , but in my scenario there are 7 variables

but those variables based on condition will populate ,how can i set those variable values in one variable. 

below is the attachment that may use

 

var a, b, c, d, e, f, g;
a = current.variables.active_directory_groups.getDisplayValue().toString().split("\n");
b = current.variables.active_directory_groups_sit_env.getDisplayValue().toString().split("\n");
c = current.variables.active_directory_groups_dev_env.getDisplayValue().toString().split("\n");
d = current.variables.modify_active_directory_groups.getDisplayValue().toString().split("\n");
e = current.variables.active_directory_groups_UAT.getDisplayValue().toString().split("\n");
f = current.variables.active_directory_groups_newordelete.getDisplayValue().toString().split("\n");
g = current.variables.modify_active_directory_groups_dailydropdown.getDisplayValue().toString().split("\n");

for (var i = 0; i < a.length; i++) {
current.variables.ad_groups = current.variables.ad_groups + a[i] + ";" + " ";
}
for (var j = 0; j < c.length; j++) {
if (c.length == j + 1) {
current.variables.ad_groups = current.variables.ad_groups + c[j];

}

Hi,

Instead of splitting it, you can just try to replace '\n' with other character( semicolumn in your case) , it will make easy for you.

Please find updated code below, if you want you can semicolumn while concatenating a,b,c variables at the end.

var a, b, c, d, e, f, g;
a = current.variables.active_directory_groups.getDisplayValue().toString().replace("\n",";");
b = current.variables.active_directory_groups_sit_env.getDisplayValue().toString().replace("\n",";");
c = current.variables.active_directory_groups_dev_env.getDisplayValue().toString().replace("\n",";");
d = current.variables.modify_active_directory_groups.getDisplayValue().toString().replace("\n",";");
e = current.variables.active_directory_groups_UAT.getDisplayValue().toString().replace("\n",";");
f = current.variables.active_directory_groups_newordelete.getDisplayValue().toString().replace("\n",";");
g = current.variables.modify_active_directory_groups_dailydropdown.getDisplayValue().toString().replace("\n",";");

current.variables.ad_groups=a+b+c+d+e+f+g;

}

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hi Abhijit, getting error and also undefined 

find_real_file.png

Hi Anusha,

Can you confirm all the 7 variables are multi line text type?

And I hope you are setting all values in AD groups variable(so you are not taking the values from that variable?) 

e = current.variables.active_directory_groups_UAT

and the above code is for which variable?

Thanks,
Murthy