NEW UI Action button

Aishwarya3017
Tera Contributor
3 REPLIES 3

Dnyaneshwaree
Mega Sage

Hello @Aishwarya3017 ,

Create a UI action in ServiceNow that will populate the parent field in the Incident Task form when a new Incident Task is created from an Incident form using below script:

// Set the properties of the UI Action
name: "Create Incident Task with Parent",
table: "incident",
action name: "new_incident_task",
show insert: true,
show update: false,
show on form: true,
form button: true,
client: true,
client script: true,

// Client-side script
function createIncidentTaskWithParent() {
    var incidentSysId = g_form.getUniqueValue(); // Get the Sys ID of the current incident record
    var incidentNumber = g_form.getValue('number'); // Get the incident number
    
    // Open a new Incident Task form and populate the 'parent' and 'incident' fields
    var url = '/nav_to.do?uri=incident_task.do?sys_id=-1&sysparm_query=incident=' + incidentSysId + '^parent=' + incidentSysId;
    window.open(url, '_blank'); // Open the form in a new tab or window
}


 If my solution helps you any way then mark it as accepted and helpful.

Thank You!!

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Aishwarya3017
Tera Contributor

I dont want a new button actually i need to modify the existing Ui Action named New button in the incident task related list of the incident form.

Dnyaneshwaree
Mega Sage

Hello @Aishwarya3017

Okay, Try this updated code ones:

var uri = action.getGlideURI();
var path = uri.getFileFromPath() + '';
path = path.substring(0, path.length - 5) + '.do';

// Retrieve the incident number from the current form
var incidentNumber = g_form.getValue('number');

// Add the incident number to the URI as a parameter
uri.set('sys_id', '-1');
uri.set('sysparm_query', 'incident.number=' + incidentNumber);

// Check for wizard and redirect URL
path = checkWizard(uri, path);

if (path) {
    action.setRedirectURL(uri.toString(path));
}

action.setNoPop(true);

function checkWizard(uri, path) {
    var already = uri.get('WIZARD:action');
    if (already == 'follow')
        return null;

    var wizID = new GlideappWizardIntercept(path).get();
    if (!wizID)
        return path;

    uri.set('sysparm_parent', wizID);
    uri.deleteParmameter('sysparm_referring_url');
    uri.deleteMatchingParameter('sysparm_list_');
    uri.deleteMatchingParameter('sysparm_record_');
    uri.deleteParmameter('sys_is_list');
    uri.deleteParmameter('sys_is_related_list');
    uri.deleteParmameter('sys_submitted');
    uri.deleteParmameter('sysparm_checked_items');
    uri.deleteParmameter('sysparm_ref_list_query');
    uri.deleteParmameter('sysparm_current_row');

    uri.set('sysparm_referring_url', uri.toString());
    uri.deleteMatchingParameter('fancy.');
    uri.deleteMatchingParameter('sys_rownum');
    uri.deleteMatchingParameter('sysparm_encoded');
    uri.deleteMatchingParameter('sysparm_query_encoded');
    uri.deleteParmameter('sysparm_refer');

    return 'wizard_view.do';
}

 If my solution helps you any way then mark it as accepted and helpful.

Thank You!!

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru