Help with a UI Action for SOW

Madeleine Eves1
Tera Contributor

Hi,

My requirement is to create a parent incident using a UI action in Service Operations workspace. Essentially I want to copy an existing incident and, once copied, the original incident's parent field should reference the newly created incident. 

 

I am trying to re-use the script for 'Copy Incident' to do but I'm going really stuck on how to go back to the original incident and update a field. Here's what I have so far:

 

  getMessages(["Create Parent Incident", "Caller", "Failed to copy Incident.", "Include attachments", "Create", "Create a parent incident by copying the current incident. Specify a caller to proceed."], onCopy);

    function onCopy(tMsgs) {
        var fields = [];
        fields.push({
            type: 'reference',
            name: 'caller_id',
            label: tMsgs["Caller"],
            mandatory: true,
            reference: 'sys_user',
            referringTable: 'incident',
            referringRecordId: g_form.getUniqueValue()
        });

        // Get the original incident's short description
        var shortDescGa = g_form.getValue('short_description');

        var ga = new GlideAjax('IncidentUtils2');
        ga.addParam('sysparm_name', 'getIncidentCopyAttachDetails');
        ga.addParam('sysparm_sys_id', g_form.getUniqueValue());

        ga.getXMLAnswer(function(response) {
            response = JSON.parse(response + '');
            if (response && response.hasAttachments) {
                fields.push({
                    type: 'boolean',
                    name: 'include_attachments',
                    label: tMsgs["Include attachments"],
                    mandatory: false,
                    value: response.copyAttachProp + '' === "true" ? true : false,
                    referringTable: 'incident',
                    referringRecordId: g_form.getUniqueValue()
                });
            }

            g_modal.showFields({
                title: tMsgs["Create Parent Incident"],
                size: "md",
                fields: fields,
                confirmTitle: tMsgs["Create"],
                confirmType: "confirm",
                instruction: tMsgs["Create a parent incident by copying the current incident. Specify a caller to proceed."]
            }).then(function(fieldValues) {
                var updatedFields = {};
                fieldValues.updatedFields.forEach(function(field) {
                    updatedFields[field.name] = field.value;
                });

                // Prefix the short description with "PARENT: "
                updatedFields["short_description"] = "PARENT: " + shortDescGa;

                ga = new GlideAjax('IncidentUtils2');
                ga.addParam('sysparm_name', 'makeIncidentCopy');
                ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
                ga.addParam('sysparm_fields', JSON.stringify(updatedFields));
                ga.getXMLAnswer(function(response) {
                    var newIncidentId = response + '';
                    if (newIncidentId !== "false") {
                        // Open the new incident
                        g_aw.openRecord("incident", newIncidentId);

                        // Set the original incident's parent field to the new incident
                        var updateGa = new GlideAjax('IncidentUtils2');
                        updateGa.addParam('sysparm_name', 'setIncidentParent');
                        updateGa.addParam('sysparm_child_id', g_form.getUniqueValue()); // Original incident
                        updateGa.addParam('sysparm_parent_id', newIncidentId); // New copied incident
                        updateGa.getXMLAnswer(); // Fire & forget
                    } else {
                        g_form.addErrorMessage(tMsgs["Failed to copy Incident."]);
                    }
                });
            });
        });
    };
}
 
Thank you in advance for helping!
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Madeleine Eves1 

try this, ensure parent field is present on the view "Service Operations Workspace"

This worked for me in workspace client script and it populated the parent field on the older incident with the newly created incident

2 lines highlighted in bold

 

function onClick(g_form) {
    getMessages(["Copy Incident", "Caller", "Failed to copy Incident.", "Include attachments", "Copy", "Create a new incident by copying the current incident. Specify a caller to proceed."], onCopy);

    function onCopy(tMsgs) {
        var fields = [];
        fields.push({
            type: 'reference',
            name: 'caller_id',
            label: tMsgs["Caller"],
            mandatory: true,
            reference: 'sys_user',
            referringTable: 'incident',
            referringRecordId: g_form.getUniqueValue()
        });
        var ga = new GlideAjax('IncidentUtils2');
        ga.addParam('sysparm_name', 'getIncidentCopyAttachDetails');
        ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
        ga.getXMLAnswer(function(response) {
            response = JSON.parse(response + '');
            if (response && response.hasAttachments) {
                fields.push({
                    type: 'boolean',
                    name: 'include_attachments',
                    label: tMsgs["Include attachments"],
                    mandatory: false,
                    value: response.copyAttachProp + '' === "true" ? true : false,
                    referringTable: 'incident',
                    referringRecordId: g_form.getUniqueValue()
                });
            }
            g_modal.showFields({
                title: tMsgs["Copy Incident"],
                size: "md",
                fields: fields,
                confirmTitle: tMsgs["Copy"],
                confirmType: "primary",
                instruction: tMsgs["Create a new incident by copying the current incident. Specify a caller to proceed."]
            }).then(function(fieldValues) {
                var updatedFields = {};
                fieldValues.updatedFields.forEach(function(field) {
                    updatedFields[field.name] = field.value;
                });
                ga = new GlideAjax('IncidentUtils2');
                ga.addParam('sysparm_name', 'makeIncidentCopy');
                ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
                ga.addParam('sysparm_fields', JSON.stringify(updatedFields));
                ga.getXMLAnswer(function(response) {
                    if (response + '' !== "false") {
                        g_form.setValue('parent', response); // response holds the sysId of the new incident
                        g_form.save();
                        g_aw.openRecord("incident", response + "");
                    } else
                        g_form.addErrorMessage(tMsgs["Failed to copy Incident."]);
                });
            });
        });
    }
}

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

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Madeleine Eves1 

try this, ensure parent field is present on the view "Service Operations Workspace"

This worked for me in workspace client script and it populated the parent field on the older incident with the newly created incident

2 lines highlighted in bold

 

function onClick(g_form) {
    getMessages(["Copy Incident", "Caller", "Failed to copy Incident.", "Include attachments", "Copy", "Create a new incident by copying the current incident. Specify a caller to proceed."], onCopy);

    function onCopy(tMsgs) {
        var fields = [];
        fields.push({
            type: 'reference',
            name: 'caller_id',
            label: tMsgs["Caller"],
            mandatory: true,
            reference: 'sys_user',
            referringTable: 'incident',
            referringRecordId: g_form.getUniqueValue()
        });
        var ga = new GlideAjax('IncidentUtils2');
        ga.addParam('sysparm_name', 'getIncidentCopyAttachDetails');
        ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
        ga.getXMLAnswer(function(response) {
            response = JSON.parse(response + '');
            if (response && response.hasAttachments) {
                fields.push({
                    type: 'boolean',
                    name: 'include_attachments',
                    label: tMsgs["Include attachments"],
                    mandatory: false,
                    value: response.copyAttachProp + '' === "true" ? true : false,
                    referringTable: 'incident',
                    referringRecordId: g_form.getUniqueValue()
                });
            }
            g_modal.showFields({
                title: tMsgs["Copy Incident"],
                size: "md",
                fields: fields,
                confirmTitle: tMsgs["Copy"],
                confirmType: "primary",
                instruction: tMsgs["Create a new incident by copying the current incident. Specify a caller to proceed."]
            }).then(function(fieldValues) {
                var updatedFields = {};
                fieldValues.updatedFields.forEach(function(field) {
                    updatedFields[field.name] = field.value;
                });
                ga = new GlideAjax('IncidentUtils2');
                ga.addParam('sysparm_name', 'makeIncidentCopy');
                ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
                ga.addParam('sysparm_fields', JSON.stringify(updatedFields));
                ga.getXMLAnswer(function(response) {
                    if (response + '' !== "false") {
                        g_form.setValue('parent', response); // response holds the sysId of the new incident
                        g_form.save();
                        g_aw.openRecord("incident", response + "");
                    } else
                        g_form.addErrorMessage(tMsgs["Failed to copy Incident."]);
                });
            });
        });
    }
}

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

@Madeleine Eves1 

Hope you are doing good.

Did my reply answer your question?

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

@Ankur Bawiskar 

Thank you so much for replying to my query; I really appreciate it. 

Unfortunately, this didn't work for me. It created the incident correctly but did not add the parent reference to the original incident.

Kind regards,

Madeleine

@Madeleine Eves1 

it worked for me in workspace client script as per screenshots shared

I informed to have this -> did you do that?

try this, ensure parent field is present on the view "Service Operations Workspace"

Also share your workspace client script

AnkurBawiskar_0-1741874263714.png

 

copy incident parent field.gif

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