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

Berin
Tera Guru

Hi Dlam, what are you expecting to trigger your script? A UI Action is triggered by someone clicking a button on a form or list. If you want an incident to be automatically created when the substate of a contract changes (which could be the result of many things, eg user action, workflow, scheduled script, etc), then you'll need to use a Business Rule instead.

Thanks Berin. It appears that I have followed the wrong suggestion to use UI Action to achieve what I expected. I updated the UI Action to add a form button but after I clicked on it takes me back the Contracts table instead of the incident form to be completed.

Hi Dlam, Sorry, I should also have said to make sure you include the code edits that Harish suggested above (especially the change to the last line which sets where the user should be taken to next). 🙂

Thanks Berin.

However, I have tested both scripts. Using the updated script suggested by Harish it takes back to the Contract table. With my original script, it takes me a new incident form.

That's interesting... both ways work for me, the only difference is the caller_id being populated or not.  That suggests that your code is running to the end and successfully setting the redirectUrl, but that Harish's is failing somewhere along the way. Maybe there's a permission issue with you running .insert()?

 

When it takes you to the new incident form, does it have the short_description and caller_id filled in? (If you have any other mandatory fields, then you'll need to populate them too before it will save.)