- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2023 12:00 PM
Hello,
I am currently working with a record producer that contains multiple questions, approximately 40 in total. Depending on the user's response to certain questions, other questions may be unhidden. Once submitted, the record producer creates a ticket in a custom table, 'u_mktg'.
I have a specific requirement to populate the 'description' field of this ticket with ONLY the questions and their corresponding answers that have been responded to by the user. Questions that do not have an answer should not be included in the description.
I created this script on the record producer but it didn't work
(function run() { var description = ''; for (var key in producer.variables) { var variable = producer.variables[key]; var answer = variable.getValue(); if (answer) { var question = variable.getLabel(); description += question + ': ' + answer + '\n'; } } current.description = description; })();
The logic behind the script being that the script loops through each variable in the producer, checks if the variable has a value, and if it does, it appends the question (label of the variable) and answer to the description. The 'description' field in the newly created record will be populated with these question-answer pairs.
The getValue() function will return an empty string if the variable has not been filled by the user, which will evaluate to false in the if condition, effectively filtering out unanswered questions.
Well, like I said, it didn't work. I would appreciate some suggestions to either fix my script or on taking a different approach to the matter. Thank you in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 12:07 PM - edited 08-10-2023 12:10 PM
This must be happening because there is nothing like producer.variables exists.
But I get your concern and made a new solution to what you want to do and came up with new script.
Replace your logic with following:
var test = [];
for (var v in producer) {
if (v.startsWith("IO") && producer[v]!='' && producer[v]=='true') { //only variables and non-empty /false values
var question = new GlideRecord('item_option_new');
question.get(v.substring(2)); // Querying by sys_id to determine question text
test += question.question_text + ": " + producer[v] + "\n"; // Set key:value pair to variable
}
}
current.description = test;
I tested this on MY PDI under create incident record producer script and it worked well.
Give it a try ..
Regards,Sushant Malsure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 01:53 PM - edited 08-10-2023 02:27 PM
Thank you for your reply. Here is what i'm getting after implementing that script and testing the request.
I made some modifications to the script and it is now working properly. Thank you for your help and putting me on the right track!
var questionsArray = [];
for (var v in producer) {
// Check if the variable starts with 'IO', which means it's a variable from a catalog item
if (v.startsWith("IO")) {
// Ensure that the variable is not empty, undefined, or 'false'
if (producer[v] && producer[v] !== '' && producer[v] !== 'false') {
var question = new GlideRecord('item_option_new');
if (question.get(v.substring(2))) { // Successfully retrieved the question record
questionsArray.push({
order: question.order, // Capture the order of the question
text: question.question_text + ": " + producer[v]
});
}
}
}
}
// Sort the questions array based on the 'order' attribute
questionsArray.sort(function(a, b) {
return a.order - b.order;
});
// Construct the description string based on the sorted array
var test = '';
questionsArray.forEach(function(q) {
test += q.text + "\n\n";
});
current.description = test.trim(); // Using trim() to remove any potential trailing blank lines
This will also put them in order of the variables on the record producer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 02:11 PM
I see you added trim() and more validation to ignore some values.
Can you also mark my answer as correct? At least that gave you foundation to what you have acheived?
Regards,Sushant Malsure

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2024 05:51 PM - edited 08-25-2024 06:00 PM
I'm trying to use your script and it works as far as getting all values, but for a reference field that I have it is writing the sys_id rather than the display value - any ideas how the script could be updated to handle this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2024 01:23 AM
Hi @Bidduam I ended up with the same issue with the reference fields like caller , business service etc, did you manage to find a way around it at all?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2025 09:37 AM
Better late than never, for anyone who finds this while searching..
change the array push to this: