Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How do I combine record producer variable fields into one comment?

jjones4773
Tera Guru

I have the following script for a record producer.

if (producer.url == "")
current.comments = " ";
else
current.comments = "URL in question: " + '\n' + producer.url;

if (producer.loaner_laptop == "")
current.comments = " ";
else
current.comments = "Loaner Laptop reservation requested: " + '\n' + producer.loaner_laptop.getDisplayValue();

It creates two comments for the incident.

How do I makes these into one with separation of values on different lines?

3 REPLIES 3

Logan Poynter
Mega Sage

Hello,

Can you attempt to attach your screenshot again? It doesn't appear to be visible. 

To your question though, are you trying to combine both the URL and Loaner comments into one?  

 


Please mark my answer as correct/helpful if it has helped you.

Thanks,
Logan

ccajohnson
Kilo Sage

What you might want to do is create a variable to store all of the values, then push the contents of the variable into the comments field:

var comString;
if (producer.url == ""){
    current.comments = " ";
} else {
    comString = "URL in question: " + '\n' + producer.url;

if (producer.loaner_laptop == "") {
    comString = " ";
} else {
    comString = "Loaner Laptop reservation requested: " + '\n' + producer.loaner_laptop.getDisplayValue();
}
current.comments = comString;

I was thinking the same -- javascript arrays and the .join() function are useful for doing this with less lines of code:

var msgs = [];
if (producer.getValue('url')) {
	msgs.push('URL in question: \n' + producer.getValue('url'));
}
if (producer.getValue('loaner_laptop')) {
	msgs.push('Loaner Laptop reservation requested: \n' + producer.getDisplayValue('loaner_laptop'));
}
current.setValue('comments', msgs.join('\n\n'));

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry