What variable type would be required in a catalog item to get the value in object format below

GD11
Tera Expert

I have requirement that value of the variable should be stored in this format with name and quantity of hardware equipment.

 

[
{
"Part": "Standard Laptop",
"Qty": "1"
}
]

1 ACCEPTED SOLUTION

@GD11 

try this

(function execute(inputs, outputs) {
    var body = {};
    var all_comments = [];
    var all_worknotes = [];
    var all_attachments = [];
    var source_info = {};
    var source_contact = {};
    var source_variables = {};
    var varValueArray = [];
    var varValue;
    var OrderVariable1 = {};

    // Capture all the comment information on ticket
    var comments = new GlideRecord('sys_journal_field');
    comments.addQuery('element', 'comments');
    comments.addQuery('element_id', inputs.task_id);
    comments.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
    comments.query();
    while (comments.next()) {
        var comment = {};
        comment.value = comments.value.toString();
        comment.time = comments.sys_created_on.toString();
        comment.created_by = comments.sys_created_by.toString();
        all_comments.push(comment);
    }

    // Capture all the work note information on ticket
    var work_notes = new GlideRecord('sys_journal_field');
    work_notes.addQuery('element', 'work_notes');
    work_notes.addQuery('element_id', inputs.task_id);
    work_notes.addQuery('sys_created_by', 'DOES NOT CONTAIN', 'insight');
    work_notes.query();
    while (work_notes.next()) {
        var worknote = {};
        worknote.value = work_notes.value.toString();
        worknote.time = work_notes.sys_created_on.toString();
        worknote.created_by = work_notes.sys_created_by.toString();
        all_worknotes.push(worknote);
    }

    // Add comments and work notes to the body
    body.comments = all_comments;
    body.work_notes = all_worknotes;

    // Add in mandatory fields to the body
    body.description = checkForNull(inputs.description);
    body.short_description = checkForNull(inputs.short_description);
    body.correlation_id = checkForNull(inputs.correlation_id);
    body.correlation_display = checkForNull(inputs.correlation_display);
    body.sys_id = checkForNull(inputs.integrated);

    function checkForNull(inputs) {
        if (inputs != null || inputs != undefined) {
            return inputs.toString();
        } else {
            return '';
        }
    }

    // Capture all attachments associated to this ticket
    var attachment = new GlideSysAttachment();
    var agr = attachment.getAttachments('sc_task', inputs.task_id);
    while (agr.next()) {
        var attachment_body = {};
        attachment_body.attachment_name = agr.file_name.toString();
        attachment_body.attachment_type = agr.content_type.toString();
        attachment_body.attachment_content = attachment.getContentBase64(agr);
        all_attachments.push(attachment_body);
    }
    body.attachments = all_attachments;

    // Populates the source_info, source_contact and source_variables objects
    var task = new GlideRecord('sc_task');
    task.addQuery('sys_id', inputs.task_id);
    task.query();
    if (task.next()) {
        source_info.number = task.number.toString();
        source_info.raised_by = task.sys_created_by.toString();
        source_info.priority = task.priority.toString();
        source_info.ritm = task.request_item.number.toString();
        source_contact.name = task.opened_by.name.toString();
        source_contact.email = task.opened_by.email.toString();
        source_contact.phone = task.opened_by.mobile_phone.toString();

        // Collects the variable data from the RITM ticket. Works for Variables and variable sets
        var question_answers = new GlideRecord('sc_item_option_mtom');
        question_answers.addQuery('request_item', task.request_item.sys_id.toString());
        question_answers.orderBy('sc_item_option.order');
        question_answers.query();
        while (question_answers.next()) {
            var varName = question_answers.sc_item_option.item_option_new.name;

            if (question_answers.sc_item_option.item_option_new.type == '8') { // 8 = reference
                varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.reference, question_answers.sc_item_option.value);
            } else if (question_answers.sc_item_option.item_option_new.type == '21') { // 21 = List Collector
                if (question_answers.sc_item_option.value != '') {
                    var list = question_answers.sc_item_option.value.split(",");
                    for (i = 0; i < list.length; i++) {
                        if (i == 0) {
                            varValueArray.push(getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
                        } else {
                            varValueArray.push(' ' + getReferenceValue(question_answers.sc_item_option.item_option_new.list_table, list[i]));
                        }
                    }
                    varValue = varValueArray.toString();
                } else {
                    varValue = '';
                }
            } else if (question_answers.sc_item_option.item_option_new.type == '18') { // 18 = Lookup Select Box
                varValue = getReferenceValue(question_answers.sc_item_option.item_option_new.lookup_table, question_answers.sc_item_option.value);
            } else {
                varValue = question_answers.sc_item_option.value.getDisplayValue();
            }

            if (varName != "" && varValue != "") {
                source_variables[varName] = varValue;
                gs.info('name of field ' + varName + ' and value ' + varValue);
            }
        }

        var mrvsJson = task.request_item.variables.ordervariable1;
        var parsedData = JSON.parse(mrvsJson);
        var arr = [];
        for (var i = 0; i < parsedData.length; i++) {
            var obj = {};
            obj["Part"] = parsedData[i].part; // give the part variable name
            obj["Qty"] = parsedData[i].quantity; // give the quantity variable name
            arr.push(obj);
            gs.info('value of final MRVS ' + JSON.stringify(arr));
        }

        // Concatenate MRVS data into source_variables
        source_variables['OrderVariable1'] = arr;
    }

    function getReferenceValue(table, sys_id) {
        var gr = new GlideRecord(table);
        gr.get(sys_id);
        return gr.getDisplayValue();
    }

    body.source_variables = source_variables;
    body.source_info = source_info;
    body.source_contact = source_contact;
    outputs.body = JSON.stringify(body);
    gs.info('value of output ' + outputs.body);
})(inputs, outputs);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Chaitanya naram
Kilo Sage

@GD11 There is no variable available in the catalog form to store a value something like an object. But you can use a string (multiline text) to store the value in a object format using some script.

 

Whenever & wherever you want to utilise this object, using parser you can access this. And if you want to set the value, set the value using stringify the object.

Thanks & Regards | Chiranjeevi Chaitanya Naram
Kindly mark the answer Correct and Helpful if it helps to resolve your issue.

Ankur Bawiskar
Tera Patron
Tera Patron

@GD11 

you can use string variable and hide it on your catalog form during submission

Post submission populate that JSON structure in that hidden variable

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

GD11
Tera Expert

@Ankur Bawiskar @Chaitanya naram  Shall we create another variable for Quantity of equipment? can we do it using MRVS by that we can have both name of equipment and quantity in same variable set, but how we do we print/sent it to another system as an Object?

@GD11 

quantity and part are variables in your catalog form?

If yes and if you want to send it to 3rd party over integration then you can form that json string and send over

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader