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

Thank you for your reply.  Here is what i'm getting after implementing that script and testing the request. 

 

Sysop_1-1691700729944.png

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. 

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?

 

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

@Sysop 

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?

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?

Better late than never, for anyone who finds this while searching..  

change the array push to this:

                    questionsArray.push({
                        order: question.order,  // Capture the order of the question
                        text: question.question_text + ": " + producer[v].getDisplayValue()
                    });
 
that will resolve the SYS_IDs for you, if that's what they are.