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

Add/remove option filed is not working properly based on user location from script include in catalog client script

Bhuvi1
Tera Contributor

Based on user location catalog variable option will change. have got location from script include but after that if else condition is not working properly. Any one condition works properly. Below is my script. Please help me with this

find_real_file.png

1 ACCEPTED SOLUTION

maroon_byte
Mega Sage

In add option, try putting quotes around 1 and 2... such as '1' and '2'.

Reference: https://developer.servicenow.com/dev.do#!/reference/api/quebec/client/c_GlideFormAPI#r_GlideformAddOption_String_String_String

addOption(String fieldName, String choiceValue, String choiceLabel)

Adds a choice to the end of a choice list field.
Parameters
Name Type Description
fieldName String The name of the field.
choiceValue String The value to be stored in the database.
choiceLabel String The value displayed.

 

OR

 

addOption(String fieldName, String choiceValue, String choiceLabel, Number choiceIndex)

Adds a choice to the list field at the position specified.

 

Note: Duplicate list labels are not supported in Service Portal. For example, items with label text matching another label are ignored and not added to the list.

 

Parameters
Name Type Description
fieldName String The field name.
choiceValue String The value stored in the database.
choiceLabel String The value displayed.
choiceIndex Number Order of the choice in the list. The index is into a zero based array.

View solution in original post

4 REPLIES 4

Rahul Talreja
Mega Sage
Mega Sage

Hi Bhuvi,

Can you please share your desired output for each country?
and also the default backed option already added to list.

Regards,
Rahul

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Hi Rahul, 

These are the  3 options and conditions needs to apply on "i want to" variable based on Requester for (user's location).

  • Display “Generate statement” to Bahrain user.
  • Display “Reprint statement” & “Reprint report” to Indonesia
  • Display “Reprint statement”  to Malaysia user.

maroon_byte
Mega Sage

In add option, try putting quotes around 1 and 2... such as '1' and '2'.

Reference: https://developer.servicenow.com/dev.do#!/reference/api/quebec/client/c_GlideFormAPI#r_GlideformAddOption_String_String_String

addOption(String fieldName, String choiceValue, String choiceLabel)

Adds a choice to the end of a choice list field.
Parameters
Name Type Description
fieldName String The name of the field.
choiceValue String The value to be stored in the database.
choiceLabel String The value displayed.

 

OR

 

addOption(String fieldName, String choiceValue, String choiceLabel, Number choiceIndex)

Adds a choice to the list field at the position specified.

 

Note: Duplicate list labels are not supported in Service Portal. For example, items with label text matching another label are ignored and not added to the list.

 

Parameters
Name Type Description
fieldName String The field name.
choiceValue String The value stored in the database.
choiceLabel String The value displayed.
choiceIndex Number Order of the choice in the list. The index is into a zero based array.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Bhuvi,

I've changed so the value of the selection will be "generate_statement", "reprint_statement", "reprint_report".

Argument to pass to .removeOption() is the value of the option. So, it the value is set to 1, then 1 should be passed.

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

    var user = new GlideAjax('GetUserLocationDetails');
    user.addParam('sysparm_name', 'getuserlocation');
    user.addParam('sysparam_user', newValue);
    user.getXML(getRequireddata);

    function getRequireddata(response) {
        var answer = response.response.documentElement.getAttribute('answer');

        if (answer == 'Bahrain') {
            g_form.addOption('i_want_to', 'generate_statement', 'Generate statement');
            g_form.removeOption('i_want_to', 'reprint_statement');
            g_form.removeOption('i_want_to', 'reprint_report');
        } else if (answer == 'Indonesia') {
            g_form.addOption('i_want_to', 'reprint_statement', 'Reprint Statement');
            g_form.addOption('i_want_to', 'reprint_report', 'Reprint Report');
            g_form.removeOption('i_want_to', 'generate_statement');

        } else if (answer == 'Malaysia') {
            g_form.addOption('i_want_to', 'reprint_statement', 'Reprint statement');
            g_form.removeOption('i_want_to', 'generate_statement');
            g_form.removeOption('i_want_to', 'reprint_report');
        }
    }
}