- 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-09-2023 12:11 PM
Hi @Sysop
When you say it didnt work, then does it mean it added all the questions and its answers (along with empty string) or it just not setting any value in description?
Regards,Sushant Malsure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 10:40 AM
No value is being set in the description.
- 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:45 PM
@Sysop worked ?
Regards,Sushant Malsure