Filling the Description Field of a Record Producer With Only Answered Questions?

Sysop
Tera Contributor

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.

1 ACCEPTED SOLUTION

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 ..

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

View solution in original post

9 REPLIES 9

sushantmalsure
Mega Sage
Mega Sage

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?

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

No value is being set in the description.

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 ..

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

@Sysop worked ?

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure