Call script include function on submit client script

Osvald
Giga Contributor

Hi, All

I'm trying call a script include function via client script this my code below:

Script include:

 

var CNPJ_CPF_Validate = Class.create();
CNPJ_CPF_Validate.prototype = {
	initialize: function() {
    },
	validaDadosCNPJ: function () {
		var tamanho, numeros, digitos, soma, pos, resp;
		//...
		cnpj = this.getParameter('sysparm_cnpj');
		cnpj = cnpj.replace(/[^\d]+/g,'');
		resp = "OK";
		//...
		if(cnpj == ''){
			resp = "CNPJ vazio.";
		}
		//...
		if (cnpj.length != 14){
			resp = "CNPJ tem menos de 14 digitos.";
		}
		// Elimina CNPJs invalidos conhecidos
		if (cnpj == "00000000000000" ||
			cnpj == "11111111111111" ||
			cnpj == "22222222222222" ||
			cnpj == "33333333333333" ||
			cnpj == "44444444444444" ||
			cnpj == "55555555555555" ||
			cnpj == "66666666666666" ||
			cnpj == "77777777777777" ||
			cnpj == "88888888888888" ||
			cnpj == "99999999999999"){
			resp = "CNPJ com todos digitos iguais";
		}

		// Valida DVs
		tamanho = cnpj.length - 2;
		numeros = cnpj.substring(0,tamanho);
		digitos = cnpj.substring(tamanho);
		soma = 0;
		pos = tamanho - 7;
		//...
		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2){
				pos = 9;
			}
		}
		//...
		var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0)){
			resp = "CNPJ com digito verificador incorreto.";
		}
		//...
		tamanho = tamanho + 1;
		numeros = cnpj.substring(0,tamanho);
		soma = 0;
		pos = tamanho - 7;
		//...
		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2){
				pos = 9;
			}
		}
		//...
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1)){
			resp = "CNPJ incorreto.";
		}
		//...
		return resp;
	},
	type: 'CNPJ_CPF_Validate'
};

And Client Script:

function onSubmit() {
	//Type appropriate comment here, and begin script below
	if (g_form.isNewRecord()){
		
		var ga = new GlideAjax('CNPJ_CPF_Validate');//this is the script include
		ga.addParam("sysparm_name", "validaDadosCNPJ"); //this is the function within the script include
		ga.addParam("sysparm_cnpj", g_form.getValue('u_documento'));
		ga.getXML(getResponse);

	}
	// --------------------------------------------------------------
	function getResponse(resp) {
		g_form.addInfoMessage(resp);
		var validado = resp.responseXML.documentElement.getAttribute('resp').toString();
		alert(validado);
		if (validado != "OK"){
			g_form.addInfoMessage(validado);
		}
		//action.setRedirectURL(current);
	}
}

I don't know why, but this is not runnig..

7 REPLIES 7

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Osvald,

Doing async GlideAjax doesn't work in an onsubmit script because typically the form is submitted before you get a response back from the server. You could switch to using synchronous, but that doesn't work in scope and has its own issues. This is my favorite explanation and solution:

https://snprotips.com/blog/2018/10/19/synchronous-lite-onsubmit-catalogclient-scripts

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

I still suspect that script include is not client callable. If you tick that client callable checkbox after creating the script include it will not be treated as client callable. It is missing below things:

HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor this is important part and client callable script as far as i know will not have initialise function when it is created first time.

Thanks,
Ashutosh

asifnoor
Kilo Patron

Hi,

The SI is not client callable and hence it is not running.

Kindly go to your Script include and make the client callable checked and then it will start working.

The sample SI (beginning part) when client callable is checked will look like this

var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

Or refer to any existing SI which is client callble and you will get an idea.

Mark the comment as a correct answer and also helpful once worked.