Can someone help with the condition and script for creating an incident with UI Action?

Dlam
Tera Expert

I am trying to create a UI Action for an incident to be created when a contract substate is aborted.

I am very new to script and UI action is not covered in the SNAF course. It will be much appreciated if anyone could help out. 

I have tried to create a UI Action below but it does not work.

Show insert YES

Show update YES

Application GLOBAL

Form context menu YES

Condition: (substate=='Aborted') && gs.hasRole("contract_manager")

Script:

createIncident();
function createIncident() {
    var newIncident = new GlideRecord('incident');
    newIncident.initialize();
    newIncident.short_description = 'New incident recorded at '+gs.nowDateTime();
newIncident.setDisplayValue('caller_id','Abby.Lincoln');
    var sys_id = newIncident.insert();   
action.setRedirectURL(newIncident);
}

Dlam_0-1693486136557.png

 

 

1 ACCEPTED SOLUTION

Hi Dlam, you need to include current. before substate.

 

Also, make sure you're checking for the value of the substate, not the label.  The value is probably 'aborted' in lower case (I don't have 'Aborted' as an option in my list, so I assume it's either part of something I don't have installed, or a customisation, either way, I can't check it to be sure.)

 

That would mean your condition should be:

   current.substate == 'aborted' && gs.hasRole("contract_manager")

View solution in original post

14 REPLIES 14

Harish Bainsla
Kilo Patron
Kilo Patron

if (current.substate == 'Aborted' && gs.hasRole('contract_manager')) {
createIncident();
}

function createIncident() {
var newIncident = new GlideRecord('incident');
newIncident.initialize();
newIncident.short_description = 'New incident recorded at ' + gs.nowDateTime();
newIncident.caller_id = 'Abby.Lincoln'; // Use the appropriate sys_id or name of the caller
var sysId = newIncident.insert();

// Redirect the user to the newly created incident
action.setRedirectURL('incident.do?sys_id=' + sysId);
}

Thank you Harish.

Unfortunately after I updated the scripts as yours and tested to submit another catalog item request, still no incident was created. I wonder if it is related the contracts are in a different catalog (Contract Catalog) other than our IT Service Catalog. Any suggestions please?

Ankur Bawiskar
Tera Patron
Tera Patron

@Dlam 

your script looks good.

what's the issue?

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

Thanks Ankur.

The issue is that when a contract is aborted no incident was created as expected. 

 I wonder if it is related the contracts are in a different catalog (Contract Catalog) other than our IT Service Catalog. Any suggestions please?