ITSM

mahesh009
Mega Guru

How to redirect users to a specific form based on a form field selection in Service Portal?

3 ACCEPTED SOLUTIONS

Shubham_Jain
Mega Sage

@mahesh009  You can do it in two way: 

 

1. Client Script: onChange: 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;  // Do not run if the form is still loading or no selection has been made
    }
    
    // Check the field value and redirect accordingly
    if (newValue == 'Option1') {
        // Redirect to a specific URL in the Service Portal
        window.location.href = "/sp?id=form&table=incident&sys_id=xxxxxxxx"; // Replace with target form URL and sys_id
    } else if (newValue == 'Option2') {
        window.location.href = "/sp?id=form&table=task&sys_id=yyyyyyyy"; // Another form based on selection
    }
}

 

 

Modify the URLs in the window.location.href lines to point to the correct form/table in the Service Portal. The URL format typically looks like:

  • /sp?id=form&table={table_name}&sys_id={record_id}

2. Use Widget Controllers: 

 

 

  • Create a Custom Widget:

    • Navigate to Service Portal > Widgets and create a custom widget.
    • In the widget’s controller, monitor the field changes and implement redirection logic using $window.location.href for client-side redirection.
  • Controller Example:

 

function($scope, $window) {
    $scope.onFieldChange = function(selectedValue) {
        if (selectedValue === 'Option1') {
            $window.location.href = '/sp?id=form&table=incident';
        } else if (selectedValue === 'Option2') {
            $window.location.href = '/sp?id=form&table=task';
        }
    };
}

 

 

Add your custom widget to the portal page where the form is located, and ensure it captures the form field changes and performs redirection.

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


View solution in original post

VishaalRanS
Tera Guru

Hi @mahesh009 

 

Basically, to redirect users to a specific form in ServiceNow's Service Portal based on a form field selection, you can utilize the SP Entry Page script include. This involves customizing the redirect logic to check for specific field values and then directing users accordingly. 

 

Else simply, you can write on change client script on the required variable and use below functions

 

top.window.location = URL;
top.window.onbeforeunload = null;

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

Sai Krishna6147
Mega Guru

HI @mahesh009 

you have to use the client script (OnChange).

for example ,Redirecting based on the selection of a dropdown

 

 

 

// This script goes in the Client Script or widget client controller

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === oldValue) {
        return;
    }

    // Check the value of the dropdown (field_name should be your actual field name)
    var selectedValue = g_form.getValue('field_name');
    
    // Redirect based on the selection
    if (selectedValue == 'option1') {
        // Redirect to form 1
        window.location.href = '/sp?id=form_1';
    } else if (selectedValue == 'option2') {
        // Redirect to form 2
        window.location.href = '/sp?id=form_2';
    } else if (selectedValue == 'option3') {
        // Redirect to form 3
        window.location.href = '/sp?id=form_3';
    }
}

 

 

If you are client controller in widget this script will helpful

 

 

$scope.$watch('c.data.selectedField', function(newValue, oldValue) {
    if (newValue !== oldValue) {
        if (newValue == 'option1') {
            window.location.href = '/sp?id=form_1';
        } else if (newValue == 'option2') {
            window.location.href = '/sp?id=form_2';
        }
    }
});

 

 

or 

The alternative way write a On change client script on the required variable and use below functions

 

 

top.window.location = URL;

top.window.onbeforeunload = null;

 

 

Thanks, and Regards

Sai Krishna

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

3 REPLIES 3

Shubham_Jain
Mega Sage

@mahesh009  You can do it in two way: 

 

1. Client Script: onChange: 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;  // Do not run if the form is still loading or no selection has been made
    }
    
    // Check the field value and redirect accordingly
    if (newValue == 'Option1') {
        // Redirect to a specific URL in the Service Portal
        window.location.href = "/sp?id=form&table=incident&sys_id=xxxxxxxx"; // Replace with target form URL and sys_id
    } else if (newValue == 'Option2') {
        window.location.href = "/sp?id=form&table=task&sys_id=yyyyyyyy"; // Another form based on selection
    }
}

 

 

Modify the URLs in the window.location.href lines to point to the correct form/table in the Service Portal. The URL format typically looks like:

  • /sp?id=form&table={table_name}&sys_id={record_id}

2. Use Widget Controllers: 

 

 

  • Create a Custom Widget:

    • Navigate to Service Portal > Widgets and create a custom widget.
    • In the widget’s controller, monitor the field changes and implement redirection logic using $window.location.href for client-side redirection.
  • Controller Example:

 

function($scope, $window) {
    $scope.onFieldChange = function(selectedValue) {
        if (selectedValue === 'Option1') {
            $window.location.href = '/sp?id=form&table=incident';
        } else if (selectedValue === 'Option2') {
            $window.location.href = '/sp?id=form&table=task';
        }
    };
}

 

 

Add your custom widget to the portal page where the form is located, and ensure it captures the form field changes and performs redirection.

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


VishaalRanS
Tera Guru

Hi @mahesh009 

 

Basically, to redirect users to a specific form in ServiceNow's Service Portal based on a form field selection, you can utilize the SP Entry Page script include. This involves customizing the redirect logic to check for specific field values and then directing users accordingly. 

 

Else simply, you can write on change client script on the required variable and use below functions

 

top.window.location = URL;
top.window.onbeforeunload = null;

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

Sai Krishna6147
Mega Guru

HI @mahesh009 

you have to use the client script (OnChange).

for example ,Redirecting based on the selection of a dropdown

 

 

 

// This script goes in the Client Script or widget client controller

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === oldValue) {
        return;
    }

    // Check the value of the dropdown (field_name should be your actual field name)
    var selectedValue = g_form.getValue('field_name');
    
    // Redirect based on the selection
    if (selectedValue == 'option1') {
        // Redirect to form 1
        window.location.href = '/sp?id=form_1';
    } else if (selectedValue == 'option2') {
        // Redirect to form 2
        window.location.href = '/sp?id=form_2';
    } else if (selectedValue == 'option3') {
        // Redirect to form 3
        window.location.href = '/sp?id=form_3';
    }
}

 

 

If you are client controller in widget this script will helpful

 

 

$scope.$watch('c.data.selectedField', function(newValue, oldValue) {
    if (newValue !== oldValue) {
        if (newValue == 'option1') {
            window.location.href = '/sp?id=form_1';
        } else if (newValue == 'option2') {
            window.location.href = '/sp?id=form_2';
        }
    }
});

 

 

or 

The alternative way write a On change client script on the required variable and use below functions

 

 

top.window.location = URL;

top.window.onbeforeunload = null;

 

 

Thanks, and Regards

Sai Krishna

Please mark this response as correct or helpful if it assisted you with your question.