redirect to new window through UI Action

DB1
Tera Contributor

Hello All,

 

I have the below script UI Action which converts RITM to INC.

I need help in opening the newly created INC in a new tab. How do I do it from client side as I now understand it cannot be done through server side

 

function openinc() {

    var usrResponse = confirm('Are you sure you would like to proceed?');

    if (usrResponse)
        gsftSubmit(null, g_form.getFormElement(), 'transfer_incident');

}

if (typeof window == 'undefined')
    runBusRuleCode();

//Server-side function
function runBusRuleCode() {

    var ritmTable = current.getTableName();
    var ritmSysID = current.getUniqueValue();
    var ritmNum = current.getValue('number');
    var comments = 'Incident created from ' + ritmNum;

    var inc = new GlideRecord("incident");

    inc.caller_id = current.request.requested_for;
    //inc.assignment_group = assignmentGroup;
    inc.short_description = current.short_description;
    inc.cmdb_ci = current.cmdb_ci;
    inc.assignment_group = current.assignment_group;
    inc.business_service = current.business_service;
    inc.impact = current.impact;
    inc.urgency = current.urgency;
    inc.description = current.variables.description;
    inc.contact_type = 'self-service';
    inc.u_initiated_by = current.request.opened_by;
    inc.comments = comments;
    inc.parent = ritmSysID;
    var incSysID = inc.insert();
    var incTable = inc.getTableName();
    var incNum = inc.getValue('number');
    GlideSysAttachment.copy(ritmTable, ritmSysID, incTable, incSysID);
    current.comments = 'RITM converted to  ' + incNum + '. Please use this reference from now on.';
    current.state = 4;


    //

    var taskRec = new GlideRecord('sc_task');
    taskRec.addQuery('request_item', current.sys_id);
    taskRec.addEncodedQuery('stateIN-5,1,2');
    taskRec.query();
    while (taskRec.next()) {
        taskRec.state = 4;
        taskRec.update();
    }

    //

    current.update();
    //gs.log("state " + current.state);
    gs.addInfoMessage(gs.getMessage("Incident {0} created", incNum));
    //action.setRedirectURL(inc);
    //action.setReturnURL(current);
    //gs.setRedirect("incident.do?sys_id=" + incSysID);

    var url = "incident.do?sys_id=" + incSysID;
    g_navigation.open(url);

}

 

@Ankur Bawiskar @Ravi Gaurav @Runjay Patel @Sandeep Rajput @Chuck Tomasi 

1 ACCEPTED SOLUTION

@DB1 

I hope your UI action is client side

Script include is not client callable

seems you didn't copy it exactly what I gave in script include.

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

9 REPLIES 9

Thanks for the reply. I tried but it returns "Record not found"

 

var PH_RITMToINC = Class.create();
PH_RITMToINC.prototype = {
    initialize: function() {},


    convertMyRecord: function() {

        var ritmSysID = this.getParameter('sysparm_sysId');
        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmSysID)) {
            var comments = 'Incident created from ' + ritm.number;

            var inc = new GlideRecord("incident");
            inc.caller_id = ritm.request.requested_for;
            inc.short_description = ritm.short_description;
            inc.cmdb_ci = ritm.cmdb_ci;
            inc.assignment_group = ritm.assignment_group;
            inc.business_service = ritm.business_service;
            inc.impact = ritm.impact;
            inc.urgency = ritm.urgency;
            inc.description = ritm.variables.description;
            inc.contact_type = 'self-service';
            inc.u_initiated_by = ritm.request.opened_by;
            inc.comments = comments;
            inc.parent = ritmSysID;
            var incSysID = inc.insert();
            GlideSysAttachment.copy(ritm.getTableName(), ritmSysID, inc.getTableName(), incSysID);
            ritm.comments = 'RITM converted to ' + inc.number + '. Please use this reference from now on.';
            ritm.state = 4;
            ritm.update();

            var taskRec = new GlideRecord('sc_task');
            taskRec.addQuery('request_item', ritmSysID);
            taskRec.addEncodedQuery('stateIN-5,1,2');
            taskRec.query();
            while (taskRec.next()) {
                taskRec.state = 4;
                taskRec.update();
            }
            return incSysID;
        }
        return '';
    },


    type: 'PH_RITMToINC'
};

 

UI Action:

function openinc() {

    var usrResponse = confirm('Are you sure you would like to proceed?');
    if (usrResponse) {
        var ga = new GlideAjax('PH_RITMToINC');
        ga.addParam('sysparm_name', 'convertMyRecord');
        ga.addParam('sysparm_sysId', g_form.getUniqueValue());
        ga.getXMLAnswer(function(answer) {
            var url = "incident.do?sys_id=" + answer;
            g_navigation.open(url, '_blank');
        });
    }
}

@DB1 

I hope your UI action is client side

Script include is not client callable

seems you didn't copy it exactly what I gave in script include.

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

Runjay Patel
Giga Sage

Hi @DB1 ,

 

Try below .

 

in ui action 

function openinc() {
    var usrResponse = confirm('Are you sure you would like to proceed?');

    if (usrResponse) {
        var ga = new GlideAjax('RITMToINC'); // Call Script Include
        ga.addParam('sysparm_name', 'convertRITMToINC'); 
        ga.addParam('sysparm_ritm_sys_id', g_form.getUniqueValue());
        
        ga.getXMLAnswer(function(response) {
            if (response) {
                var url = '/incident.do?sys_id=' + response;
                window.open(url, '_blank'); // Opens the INC in a new tab
            }
        });
    }
}

 

In script include

var RITMToINC = Class.create();
RITMToINC.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    convertRITMToINC: function() {
        var ritmSysID = this.getParameter('sysparm_ritm_sys_id');
        if (!ritmSysID) {
            return '';
        }

        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmSysID)) {
            var comments = 'Incident created from ' + ritm.number;
            var inc = new GlideRecord('incident');

            inc.caller_id = ritm.request.requested_for;
            inc.short_description = ritm.short_description;
            inc.cmdb_ci = ritm.cmdb_ci;
            inc.assignment_group = ritm.assignment_group;
            inc.business_service = ritm.business_service;
            inc.impact = ritm.impact;
            inc.urgency = ritm.urgency;
            inc.description = ritm.variables.description;
            inc.contact_type = 'self-service';
            inc.u_initiated_by = ritm.request.opened_by;
            inc.comments = comments;
            inc.parent = ritmSysID;
            var incSysID = inc.insert();

            // Copy attachments
            GlideSysAttachment.copy('sc_req_item', ritmSysID, 'incident', incSysID);

            // Update RITM with reference
            ritm.comments = 'RITM converted to ' + inc.number + '. Please use this reference from now on.';
            ritm.state = 4;
            ritm.update();

            return incSysID; // Return the new incident sys_id
        }
        return '';
    }
});

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

DB1
Tera Contributor

Thanks for the reply I tried but did not work

 

function openinc() {

    var usrResponse = confirm('Are you sure you would like to proceed?');
    if (usrResponse) {
        var ga = new GlideAjax('PH_RITMToINC');
        ga.addParam('sysparm_name', 'convertMyRecord');
        ga.addParam('sysparm_sysId', g_form.getUniqueValue());
        ga.getXMLAnswer(function(answer) {
            var url = "incident.do?sys_id=" + answer;
            g_navigation.open(url, '_blank');
        });
    }
}

 

SI:

 

var PH_RITMToINC = Class.create();
PH_RITMToINC.prototype = {
    initialize: function() {},


    convertMyRecord: function() {

        var ritmSysID = this.getParameter('sysparm_sysId');
        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmSysID)) {
            var comments = 'Incident created from ' + ritm.number;

            var inc = new GlideRecord("incident");
            inc.caller_id = ritm.request.requested_for;
            inc.short_description = ritm.short_description;
            inc.cmdb_ci = ritm.cmdb_ci;
            inc.assignment_group = ritm.assignment_group;
            inc.business_service = ritm.business_service;
            inc.impact = ritm.impact;
            inc.urgency = ritm.urgency;
            inc.description = ritm.variables.description;
            inc.contact_type = 'self-service';
            inc.u_initiated_by = ritm.request.opened_by;
            inc.comments = comments;
            inc.parent = ritmSysID;
            var incSysID = inc.insert();
            GlideSysAttachment.copy(ritm.getTableName(), ritmSysID, inc.getTableName(), incSysID);
            ritm.comments = 'RITM converted to ' + inc.number + '. Please use this reference from now on.';
            ritm.state = 4;
            ritm.update();

            var taskRec = new GlideRecord('sc_task');
            taskRec.addQuery('request_item', ritmSysID);
            taskRec.addEncodedQuery('stateIN-5,1,2');
            taskRec.query();
            while (taskRec.next()) {
                taskRec.state = 4;
                taskRec.update();
            }
            return incSysID;
        }
        return '';
    },


    type: 'PH_RITMToINC'
};

Hi @DB1 ,
the client script can stay as is

The problem is with the script include script 
make sure the script include is  Client callable and update it with below script

var PH_RITMToINC = Class.create();
PH_RITMToINC.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    convertMyRecord: function() {

        var ritmSysID = this.getParameter('sysparm_sysId');
        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmSysID)) {
            var comments = 'Incident created from ' + ritm.number;

            var inc = new GlideRecord("incident");
            inc.initialize();
            inc.caller_id = ritm.request.requested_for;
            inc.short_description = ritm.short_description;
            inc.cmdb_ci = ritm.cmdb_ci;
            inc.assignment_group = ritm.assignment_group;
            inc.business_service = ritm.business_service;
            inc.impact = ritm.impact;
            inc.urgency = ritm.urgency;
            inc.description = ritm.variables.description;
            inc.contact_type = 'self-service';
            // inc.u_initiated_by = ritm.request.opened_by;
            inc.comments = comments;
            inc.parent = ritmSysID;
            var incSysID = inc.insert();
            GlideSysAttachment.copy(ritm.getTableName(), ritmSysID, inc.getTableName(), incSysID);
            ritm.comments = 'RITM converted to ' + inc.number + '. Please use this reference from now on.';
            ritm.state = 4;
            ritm.update();

            var taskRec = new GlideRecord('sc_task');
            taskRec.addQuery('request_item', ritmSysID);
            taskRec.addEncodedQuery('stateIN-5,1,2');
            taskRec.query();
            while (taskRec.next()) {
                taskRec.state = 4;
                taskRec.update();
            }
            return incSysID;
        }
        return '';
    },


    type: 'PH_RITMToINC'
});
}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya