Make use of Code Snippet in a UI Action button

Travis Michigan
Mega Sage

Can anyone help point to more information about making use of Code Snippets, particularly if it pertains to using them in a UI Action button?  

 

I created a button that only shows up when a particular field is filled (policy number).  What I would like to do is when this button is clicked, it would make use of a Flow Designer action I have that takes in the policy number and returns a url for the fulfiller to use for more information.  The action works and returns the proper url on its own, but I can't seem to use the code snippet properly so the button opens a new window to that url.  I've read some information but I think I'm missing some basic info on its use to do so properly and I'm just not getting any more clarity from reading the information linked by ServiceNow.

 

This is the snippet:

 

(function() {
	
	var inputs = {};

	inputs['policy_number'] = g_form.getValue("u_policy_number"); // String 

	GlideFlow.startAction('global.go_to_uc_link', inputs)
		.then(function(execution) {
			return execution.awaitCompletion();
		}, errorResolver)
		.then(function(completion) {

			var status = completion.status;

			// Available Outputs:
			var outputs = completion.outputs;		
			var url = outputs['url']; // String
		});

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

 

 

And I'm just trying to get it to open a new window so in the Workspace Client Script I currently have the following:

 

function onClick(g_form) {

	var page = "https://www.google.com";
    top.window.open(page, "_blank");
};

 


I'm just trying to get it so that the url generated from the action gets used instead of the current static link to google but every way I try to do this turns out to not function and I must be missing some basic information of it's use.  Can anyone help to direct me to where I can get a few examples of something similar to this to help me to figure out what I'm missing?

Thank you in advance!

1 ACCEPTED SOLUTION

Ratnakar7
Mega Sage
Mega Sage

Hi @Travis Michigan ,

 

If you want to open a new window with a dynamic URL generated from a Flow Designer action, you need to handle the asynchronous nature of GlideFlow actions appropriately. Here's how you might modify your code to achieve this:

// Function to open a new window with the given URL
function openNewWindow(url) {
    window.open(url, "_blank");
}

// Function to handle the UI Action button click
function onClick() {
    // Fetch the policy number from the form
    var policyNumber = g_form.getValue("u_policy_number");

    // Check if the policy number is available
    if (policyNumber) {
        // Create inputs for the Flow Designer action
        var inputs = { 'policy_number': policyNumber };

        // Start the Flow Designer action
        GlideFlow.startAction('global.go_to_uc_link', inputs)
            .then(function (execution) {
                // Await completion of the action
                return execution.awaitCompletion();
            })
            .then(function (completion) {
                // Handle completion and get the URL
                var status = completion.status;
                var outputs = completion.outputs;
                var url = outputs['url'];

                // Check if the URL is available
                if (url) {
                    // Open a new window with the generated URL
                    openNewWindow(url);
                } else {
                    // Handle the case when the URL is not available
                    alert('URL not available');
                }
            })
            .catch(function (error) {
                // Handle errors
                alert('Error: ' + error.message);
            });
    } else {
        // Handle the case when the policy number is not available
        alert('Policy number is not filled');
    }
}

// Attach the onClick function to the UI Action button
onClick();

Also, refer https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G...

 

Thanks,

Ratnakar

View solution in original post

1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

Hi @Travis Michigan ,

 

If you want to open a new window with a dynamic URL generated from a Flow Designer action, you need to handle the asynchronous nature of GlideFlow actions appropriately. Here's how you might modify your code to achieve this:

// Function to open a new window with the given URL
function openNewWindow(url) {
    window.open(url, "_blank");
}

// Function to handle the UI Action button click
function onClick() {
    // Fetch the policy number from the form
    var policyNumber = g_form.getValue("u_policy_number");

    // Check if the policy number is available
    if (policyNumber) {
        // Create inputs for the Flow Designer action
        var inputs = { 'policy_number': policyNumber };

        // Start the Flow Designer action
        GlideFlow.startAction('global.go_to_uc_link', inputs)
            .then(function (execution) {
                // Await completion of the action
                return execution.awaitCompletion();
            })
            .then(function (completion) {
                // Handle completion and get the URL
                var status = completion.status;
                var outputs = completion.outputs;
                var url = outputs['url'];

                // Check if the URL is available
                if (url) {
                    // Open a new window with the generated URL
                    openNewWindow(url);
                } else {
                    // Handle the case when the URL is not available
                    alert('URL not available');
                }
            })
            .catch(function (error) {
                // Handle errors
                alert('Error: ' + error.message);
            });
    } else {
        // Handle the case when the policy number is not available
        alert('Policy number is not filled');
    }
}

// Attach the onClick function to the UI Action button
onClick();

Also, refer https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G...

 

Thanks,

Ratnakar