If NewValue contains "#" then change it to %23

VIKAS MISHRA
Tera Contributor

We have written one on change client script , on change on field "Account_name".

While filling the request form on portal user can see one field "Account name" and there he can add any value. 

So now if user add any string and that string contanings "#" then my client script should change that # to %23, i and i am hoping that different should be making impact in the line no. 21 and 23. for the varaible odata

Please sugest how to do that.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
    g_form.clearOptions('select_technology_for_account');
    var typeOfAction = g_form.getValue('select_type_of_action');
    //var accountName = g_form.getValue('account_name');
    var domain = 'AD Domains(';
    var arr = [];
    var platcodecontains = ['OPSNETX', 'LABLAN', 'URCLAB', 'EMRELAB', 'XOMDMZ', 'CLNLAB', 'LABLAN.LOCAL'];
    setTimeout(function() {
        g_form.addOption('select_technology_for_account', '', '-- None --');
        g_form.showFieldMsg('select_technology_for_account', "Please wait until we finish querying the system");
        g_form.setMandatory('select_technology_for_account', false);
        g_form.setReadOnly('select_technology_for_account', true);
        var ga = new GlideAjax('x_iem_iam.IAM_API_Utils_Ajax');
        ga.addParam('sysparm_name', 'searchPrincipalOdata');
        var odata;
        if (typeOfAction == 'Reset Password') {
            odata = 'PrincipalTypeIsUserID eq true and IsPrimary eq false and (Techcode ne \'SAP/R3\' and Techcode ne \'SAP/HANA\') and UID eq ' + '\'' + newValue + '\'';
        } else {
            odata = 'PrincipalTypeIsUserID eq true and UID eq ' + '\'' + newValue + '\'';
        }
        ga.addParam('sys_param_odata', odata);

        function myfun(item) {
            var s = new Set(item);
            var it = s.values();
            return Array.from(it);
        }
        ga.getXML(function(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
			//console.log('vikas' + answer);
            var answer_obj = JSON.parse(answer);
            //alert(answer_obj);
            answer_obj.forEach(function(element) {
                if (element.techcode != 'EMAIL' && element.techcode != 'VPN' && element.techcode != 'ADLDS' && element.techcode != 'AZURE' && element.techcode != 'SAAS' && element.techcode != 'APPLICATN' && element.platcode != 'DRAC' && element.platcode != 'HP' && element.platcode != 'SAP/WAS' && element.platcode != 'XOMRES.CLOUD') {
                    if (element.techcode == 'NT' && !platcodecontains.includes(element.platcode)) {
                        arr.push(element.platcode);
                    } else if (element.techcode == 'NT' && platcodecontains.includes(element.platcode)) {
                        g_form.addOption('select_technology_for_account', element.platcode, element.platcode);
                    } else {
                        if (typeOfAction != 'Unlock Account') {
                            g_form.addOption('select_technology_for_account', element.techcode, element.techcode);
                        }
                    }

                }
            });
            if (typeOfAction == 'Reset Password' || typeOfAction == 'Unlock Account') {
				//alert("New API CALL")
                var ga2 = new GlideAjax('x_iem_iam_iga_2.IAM_API_V2_Utils_Ajax');
                ga2.addParam('sysparm_name', 'searchAccountOdata');
                var odataparam;

                odataparam = 'SourceType in (\'SAP\' ,\'SAP HANA\') and DataSource eq \'IDN\' and Name eq \'' + newValue + '\'';
					console.log(odataparam)
                ga2.addParam('sys_param_odata', odataparam);

                ga2.getXML(function(response) {
                    var resp = response.responseXML.documentElement.getAttribute('answer');
                    var json_value_obj = JSON.parse(resp);
                    //alert(json_value_obj);
                    json_value_obj.forEach(function(element)

                        {
                            if (element.sourcetype != 'EMAIL' && element.sourcetype != 'VPN' && element.sourcetype != 'ADLDS' && element.sourcetype != 'AZURE' && element.sourcetype != 'SAAS' && element.sourcetype != 'APPLICATN' && element.sourcename != 'DRAC' && element.sourcename != 'HP' && element.sourcename != 'SAP/WAS' && element.sourcename != 'XOMRES.CLOUD') {
                                if (element.sourcetype == 'NT' && !platcodecontains.includes(element.sourcename)) {
                                    arr.push(element.sourcename);
                                } else if (element.sourcetype == 'NT' && platcodecontains.includes(element.sourcename)) {
                                    g_form.addOption('select_technology_for_account', element.sourcename, element.sourcename);
                                } else {
                                    var source = element.sourcetype == "SAP" ? "SAP" : element.sourcetype == "SAP HANA" ? "SAP HANA" : element.sourcetype;
                                    console.log(source);
									g_form.addOption('select_technology_for_account', source, source);

                                }

                            }
                        });

                });
            }
            var b = myfun(arr);
           // g_form.addOption('select_technology_for_account', domain + b + ')', domain + b + ')');
			g_form.addOption('select_technology_for_account', platcodecontains.includes(b)? b: domain+ b+')', domain + b + ')');
            g_form.setReadOnly('select_technology_for_account', false);
            g_form.setMandatory('select_technology_for_account', true);
            g_form.hideFieldMsg('select_technology_for_account');
            var len = answer_obj.length;
            if (len == 0)
                g_form.showFieldMsg('select_technology_for_account', 'No data available with this name');
				g_form.hideFieldMsg('select_technology_for_account')

        });
    }, 1000);



    //Type appropriate comment here, and begin script below
}

 

5 REPLIES 5

Chaitanya ILCR
Kilo Patron

HI @VIKAS MISHRA ,

 

it's not sysid right?

it's the name of the account right? (I mean it's of single line text type and not reference type)

if yes

try adding this line as the 5 or 6th line

newValue = encodeURIComponent(newValue);

 

else 

you would need to use getReference or GlideAjax to get the account name value

and then use (encodeURIComponent()) function by passing the name value as parameter to the function

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Its not working at all. 

Yes account name is the single line text field. 

We are using a rest integration in this . 

So when we add a account name in field "Account name" in the portal then on the bases of that account name value it runs one integration and get the account technology name corresponding to that account name thorugh iontegration" 

But now we have seen that we have few account name which starts with the value # but the integration does not understand the # this is the reason we wanted our client script to run in that manner so that if user uses # in the account name then our client script change it to skyvalue to %23 and then integration will undertsand it and perfectly get the account name and on the bases of get the techology name and visible that technoilogy name in portal another drop down field "select technolgy. 

I made changes you told but not work. 

 

VIKASMISHRA_0-1752486582271.png

 

@VIKAS MISHRA 

to encode the value update these lines

 var odata;
        newValue = encodeURIComponent(newValue);
        if (typeOfAction == 'Reset Password') {

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@VIKAS MISHRA 

what's your actual business requirement?

what the user is suppose to do after entering the account?

please share those details along with screenshots

If my response helped please mark it correct and close the thread so that it benefits future readers.

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