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

johnfeist
Mega Sage
Mega Sage

Hi Osvald,

Assuming that the script include runs correctly strictly on hte server side, the typical reason it won't run via AJAX is that the Client Callable check box has not been checked.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Osvald
Giga Contributor

johnfeist, This field 'Client Callable' has been verified! And yet it doesn't work

I suspect that Sanjiv and Brad have the answer.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

SanjivMeher
Kilo Patron
Kilo Patron

GlideAjax async will not work in onSubmit client script.

Can you try getAnswer() instead?

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.getXMLWait();
      alert(ga.getAnswer());

	}

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