enable ui action in workspace level and makes it as workable

mastanbe
Tera Contributor

Hi All,

 

I need your help to enable one Discover now ui action in workspace level and makes its as workable in workspace level.

Below is the ui action script : 

var type = 'Cloud';
if ((/Managed AWS Cloud/gi).test(current.asset.u_solution_type)) {
    type = 'AWS';
}
if ((/Managed Azure Cloud/gi).test(current.asset.u_solution_type)) {
    type = 'Azure';
}
if ((/Managed Google Cloud/gi).test(current.asset.u_solution_type)) {
    type = 'Google';
}

if (type != 'Cloud' && (gs.nil(current.getValue('u_cloud_account_id')) || gs.nil(current.getValue('u_region')))) {
    gs.addErrorMessage("Sorry, Cloud Account Id and Region cannot be empty. Please populate and run again");
    action.setRedirectURL(current);
    current.update();
} else {
    var startDate = new GlideDateTime();
    startDate.addSeconds(0);
    var scheduleJob = new global.ScheduleOnce();
    scheduleJob.script = "new global.CMDBDiscoveryHelper().handleCustomerDailySchedule('" + type + "', '" + current.name + "', true);";
    scheduleJob.setLabel("Discover Now");
    scheduleJob.setTime(startDate);
    scheduleJob.schedule();

    gs.addInfoMessage("Discovery job started. Job will complete in < 5 mins. In case of any issue, please reach out to CD230066@Teradata.com");

    if (gs.hasRole("discovery_admin")) {
        action.setRedirectURL("/discovery_status_list.do?sysparm_query=sys_created_onONLast 15 minutes@javascript&colon;gs.beginningOfLast15Minutes()@javascript&colon;gs.endOfLast15Minutes()^sys_created_onONToday@javascript&colon;gs.beginningOfToday()@javascript&colon;gs.endOfToday()^dscheduler.nameLIKE" + current.name);
    } else {
        action.setRedirectURL(current);
    }

    current.update();
}

i have to enable that in workspace level kindly please help me on this one.
3 REPLIES 3

palanikumar
Mega Sage

Hi,

 

Workspace supports only Client Side UI Action. Convert your logic to GlideAjax and call them from the UI Action

Refer the below article for more details

https://www.servicenow.com/community/next-experience-articles/how-to-use-ui-actions-in-workspaces/ta...

 

Thank you,
Palani

hi @palanikumar , i have tried like script include and workspace client script but not working.

script inclue :

var EventRefreshHelper = Class.create();
EventRefreshHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
 getRefreshData: function() {
    var eventid = this.getParameter('sysparm_eventSysId');
    var source = this.getParameter('sysparm_sourceValue');
        // Set session client data for the event ID
        gs.getSession().putClientData('em_refresh_event_id', eventid);

        // Construct the redirect URL based on the source field value
        var redirectURL = '/em_match_rule_list.do?sysparm_view=refresh_event_rule&sysparm_query=event_classISEMPTY^ORevent_class=' + source;

        // Prepare the info message
        var infoMessage = gs.getMessage("Select an event rule to update with event data and then click \"Refresh Selected\" action");

        return {
            redirectURL: redirectURL,
          infoMessage: infoMessage
       };
    },

    type: 'EventRefreshHelper'
});


workspace client script : 
function onClick(g_form) {
 var ga = new GlideAjax('EventRefreshHelper');
    ga.addParam('sysparm_name', 'getRefreshData');
    ga.addParam('sysparm_eventSysId', g_form.getUniqueValue());
    ga.addParam('sysparm_sourceValue', g_form.getValue('source'));
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        if (result.infoMessage)
            g_form.addInfoMessage(result.infoMessage);
        if (result.redirectURL)
            window.location.href = result.redirectURL;
    });
}

Hi,

Hope this is a UI Action with client flag checked. In this case, do not pass g_form as parameter. g_form is out of box attribute, the action could over rite. Set the onClick attribute of UI action to onClick(). The script should be below:

function onClick() {
 var ga = new GlideAjax('EventRefreshHelper');
    ga.addParam('sysparm_name''getRefreshData');
    ga.addParam('sysparm_eventSysId', g_form.getUniqueValue());
    ga.addParam('sysparm_sourceValue', g_form.getValue('source'));
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        if (result.infoMessage)
            g_form.addInfoMessage(result.infoMessage);
        if (result.redirectURL)
            window.location.href = result.redirectURL;
    });
}
Thank you,
Palani