Get current variables value by setting variable name from a table

raj_singh
Giga Contributor

Requirement - Populate Short description based in choice selected for a variable.

I am configuring a workflow which can be utilize by many SR, for that I have created a table from where I am fetching the required data and setting it for task details.

Few SR's task have short description based on selected request type. Which can be achieved by

task.short_description = "xyxyxyxyxyxy" + current.variables.req_type;

Now for few SR, dependent variable name is other than req_type;

So I found a solution that I will pass the variable name from 'abcabc' table and based on this variable name, I will set Short description.

task.short_description = "xyxyxyxyxyxy" + current.variables.Dynamic_Variable_from_abcabc_table;

 

Below script is not working to setting the task short description.

Let me know how this will work or any workaround for it.

 

var config = new GlideRecord('abcabc');
config.addQuery('u_req','TEST SR');
config.query();
while(config.next()){
task.short_description = "xyxyxyxyxyxy" + current.variables.config.u_variable_name;

}

@Chuck Tomasi @Pradeep Sharma @Ankur Bawiskar @Brad Tilton @Mark Roethof @Michael Fry 

1 ACCEPTED SOLUTION

Hi

I think I got yout requirement. What I prepared for you, is a small script, which shows, how you could to dynamically, about what you want to do:

var gr = new GlideRecord('sc_req_item');

gr.addQuery('number', 'RITM0010032');
gr.query();

if (gr.next()) {
   gs.info('got it: ' + gr.number);
   gs.info(gr.variables.getElements());
   gs.info(gr.variables.what_county.getValue());

   var current = gr;
   var useThisVariableName   = 'what_country';
   var theValueToSet = '';

    var variables = current.variables.getElements(); 
    for (var i=0;i<variables.length;i++) {
        var question = variables[i].getQuestion(); 
        if (question.name == useThisVariableName) {
            theValueToSet = question.getValue();
        }
//      gs.addInfoMessage(question.name + " / " + question.getLabel() + ":" + question.getValue()); 
    } 

    gs.info('The value of the variable called ' + useThisVariableName + ' is ' + theValueToSet);

}

Take this as a starting point to get variable values for having the NAME of the variable to use, in a different variable.

 

Let me know if that answers your question and mark my answer as correct helpful.

Thanks & BR

Dirk

 

 

 

View solution in original post

14 REPLIES 14

No..It is giving 'undefined' in short description.

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Don't really get this part:

while(config.next()){
task.short_description = "xyxyxyxyxyxy" + current.variables.config.u_variable_name;

}

This would actually override your task description over and over and over. That doesn't sound correct? Why the while? Because this is on a create task utility, so only one task.

So is your short description not set at all? Or the current.variables.etc part just not correct?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello Roethof,

There would be only one record after the Glide query and this is only testing, I will do the required changes once I achieved the requirement.

As I mentioned in requirement, I want to reuse this workflow for all SR having single task. I set all required details of task in 'abcabc' table and Gliding this table to set the task value.

My issue is all SRs don't have 'req_type' as variable whose value should be populated in task description. How to achieve this one?

I want to populate short description like below using abcabc table data or any other way possible - 

Here variable name is different in each case which I have to set in short desc. 

task.short_description = "xyxyxyxyxyxy" + current.variables.config.u_variable_name;

SR1 (user selecting req_type as Laptop)

Please provide user - Laptop

SR2 (user selecting access_type as Database)

User want access on  - Database

Would using something like below help?

current.variables['req_type'];

With this, you could actually make the variable name dynamic. For example:

var dynamic_variable = "req_type";
current.variables[dynamic_variable];

Obviously, depending on your logic, you could fill dynamic_variable now with anything you are after. With script, if statements, switch, of maybe even querying a table for the variable name you are after.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

This is the my issue. Even using Variable name as string giving undefined.

var reqt_type = "req_type";


  task.short_description = gr.u_short_description + " "+ current.variables.req_type + "::::" + " "+current.variables.reqt_type;

 

Giving Result as - Please provide user laptop:::: undefined