Copy the variables label and value in to target record

sivananda80
Tera Expert

Hi Every one,

I have record producer. After submitting the catalog i need copy all variables labe one value in to the description. I tried below script in record producer script. its working as expected, Can you please help on that

var vars = '';
var variables = producer.variables.getElements();
for (var i = 0; i < variables.length; i++){
    var v = variables[i];
    if(!v.getQuestion())
    continue;
    var label =
    v.getQuestion().getLabel();
    var value = v.getDisplayValue();
    if(value){
        vars += label + ": " + value + "\n";
    }
}

current.description = "\n\nVariables Sumitted:\n" + vars;
2 REPLIES 2

nitin gupta1
Tera Contributor

Your script looks good and should work for most Record Producer use cases. It loops through all submitted variables, retrieves the variable label and display value, and appends them to the Description field of the target record.

 

var vars = '';
var variables = producer.variables.getElements();

 

for (var i = 0; i < variables.length; i++) {
    var v = variables[i];

 

    if (!v.getQuestion())
        continue;

 

    var label = v.getQuestion().getLabel();
    var value = v.getDisplayValue();

 

    if (value) {
        vars += label + ": " + value + "\n";
    }
}

 

current.description = "Variables Submitted:\n\n" + vars;
 
 
 

A few notes:

  • Using getDisplayValue() is recommended because it returns user-friendly values for reference, choice, and date fields.
  • The check if (value) prevents empty variables from being included in the description.
  • This approach will work for most variable types and makes it easier for fulfillers to view request details directly from the Description field.

If you need to include variables even when they are empty, you can remove the if (value) condition.

Overall, your script is correct and follows a standard approach for copying Record Producer variable labels and values into the target record description.

Tanushree Maiti
Tera Patron

Hi @sivananda80 

 

Script looks good. Test it once .  what help you need as you stated script is working fine.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti