The Zurich release has arrived! Interested in new features and functionalities? Click here for more

I have a requirement to create incident record from HR case, I was trying the below logic

shaikimran4
Tera Contributor

I have create new UI action with below logic in UI action with client box checked

 

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

 function hr_create_incident() {
        if (g_form.getValue('cmdb_ci') == "") {
            g_form.setDisplay('cmdb_ci', true);
            g_form.setMandatory('cmdb_ci', true);
            g_form.showFieldMsg('cmdb_ci', 'Please select the appropriate CI', 'error');
            return false;
        }
        g_form.showFieldMsg('cmdb_ci', 'Appropriate CI is selected', 'info');
        gsftSubmit(null, g_form.getFormElement(), 'hr_create_incident');

    }

        function setRedirect(){
        gs.info('excuting server side script');
        var inc = new GlideRecord('incident');
        inc.initialize();
        inc.short_description = current.short_description;
        inc.caller_id = current.subject_person;
        inc.insert();
 
Client side logic is executing properly but server side script is not working properly and incident record is not getting created. Can someone help me out on this issue.
4 REPLIES 4

garimakharb
Giga Guru

in your UI Action, your server-side logic (the setRedirect() function with GlideRecord) is not wrapped inside the if (g_form.submitted()) or if (action == 'hr_create_incident').

That’s why it’s never triggered. 

// This runs client-side when the UI Action button is clicked
function hr_create_incident() {
if (g_form.getValue('cmdb_ci') == "") {
g_form.setDisplay('cmdb_ci', true);
g_form.setMandatory('cmdb_ci', true);
g_form.showFieldMsg('cmdb_ci', 'Please select the appropriate CI', 'error');
return false;
}
g_form.showFieldMsg('cmdb_ci', 'Appropriate CI is selected', 'info');
// Submit to server-side part of this UI Action
gsftSubmit(null, g_form.getFormElement(), 'hr_create_incident');
}

// This runs ONLY on the server side
if (typeof window == 'undefined') {
if (action == 'hr_create_incident') {
gs.info('executing server side script');
var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = current.short_description;
inc.caller_id = current.subject_person;
inc.insert();
}

try this .. and if it doesn't work you can make separate script include and call it in ui action

@garimakharb  - This still doesn't seems to be working

@garimakharb I have created UI action in "Human Resource Core" scope and incident table will be usually in global scope. Does that make some difference?

kaushal_snow
Mega Sage

Hi @shaikimran4 ,

 

The reason your server side code inside the UI Action isn’t executing is likely because with Client = true, all the code inside the script is treated as client side by default and current, gs, GlideRecord, action.setRedirectURL() etc. aren’t available there, so to make it work you need to split your logic:.......validate client side first, then use gsftSubmit() to call the same UI Action name and in the server side block (wrapped by if (typeof window == 'undefined')) do the GlideRecord insert/update and redirection....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/