How to call flow designer action from OnSubmit client Script

JRY
Mega Guru

Hello,

 

From the Onsubmit Client script, I'm trying to trigger the Flow Designer action. But it is not working.
As you can see from the code below, I'm trying to call a flow designer action, and based on the output, if output is true, I need to send an error message and stop from submitting the request, and if output is false, it can submit the request. However, the code below is not working. Can somebody tell me what went wrong in the code below?

 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    (function() {
        var userID = g_form.request_For.sys_id;
        var denyInternet = 'DENYINTERNETACCESS.LG';
        var phising = 'PHISHING.DIA';
        var inputs = {};
        inputs['user_sys_id'] = userID; // String 
        inputs['group_name'] = denyInternet, phising; // String 

        GlideFlow.startAction('x_iem_iam.privisusermemberofgroup', inputs)
            .then(function(execution) {
                return execution.awaitCompletion();
            }, errorResolver)
            .then(function(completion) {
                var status = completion.status;
                // Available Outputs:
                var outputs = completion.outputs;
                var ismemberofgroup = outputs['ismemberofgroup']; // True/False
                if (ismemberofgroup == false) {
                    return true;
                } else {
                    g_form.addErrorMessage('You are part of the Phishing group which restricts access to internet. Please contact the CyberSecurity team for additional details');
                    return false;
                }
            });

        function errorResolver(error) {
            // Handle errors in error resolver

        }
    })();
}

 

 

Thanks,

JRY

 

11 REPLIES 11

@Elena Pomana 

Since flow runs in background, I would recommend you run the flow once the record is created.

any form based validation for user should be handled via client script

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

Hi Elena,

 

  1. Create a Catalog Client Script: You are on the right track by considering an onSubmit catalog client script. This script will run when the record producer is submitted.

  2. Fetch Field Values: In your script, you can fetch the values of the three fields: demand number, requester's company code, and the service.

 

 

var demandNumber = g_form.getValue('demand_field_name');
var companyCode = g_form.getValue('company_code_field_name');
var service = g_form.getValue('service_field_name');
var specificServices = ['Service1', 'Service2', 'Service3', 'Service4'];
if (specificServices.indexOf(service) !== -1) {
    // Check SharePoint folder and perform actions
}

 

 

Check SharePoint Folder: To interact with SharePoint, you can use REST API calls from your ServiceNow script. Here's a high-level approach:

a. You can use the gs.ajax or g_request functions in ServiceNow to make HTTP requests to SharePoint.

b. Perform a GET request to check if the company code folder exists in SharePoint. You may need to authenticate using SharePoint credentials.

c. If the folder doesn't exist, display an error message.

d. If the folder exists, use a POST request to create a subfolder with the demand number inside the "initial" folder.

  • Here's an example of how you might structure the SharePoint API requests:

 

 

// Example REST API URL for SharePoint folder existence check
var folderExistenceURL = 'https://sharepoint-site-url/_api/web/GetFolderByServerRelativeUrl(\'/sites/site-name/library/company-code\')';

// Example REST API URL for creating a subfolder
var createSubfolderURL = 'https://sharepoint-site-url/_api/web/GetFolderByServerRelativeUrl(\'/sites/site-name/library/initial\')/folders/add(\'demand-number\')';

// Make the GET request to check folder existence
gs.ajax({
    url: folderExistenceURL,
    method: 'GET',
    headers: {
        'Accept': 'application/json;odata=verbose',
        'Authorization': 'Bearer <your-access-token>',
    },
    onComplete: function(response) {
        // Process the response and handle folder existence
    }
});