Skip to Content
Author's profile photo Anderson Balieiro

Create validation in Tax Number CNPJ

In order to develop a validation within extended fields that do not exist as standard as the TaxNumber in SAP CRM related to TaxType “BR1” in Brazil, follows the algorithm model that is possible used to perform validation tax document CNPJ.

This procedure can be improved and optimized by reusing libraries and loops

The following coding is only an initial reference

Create the follow objects

Put in the Validation “OnSave” the follow code

import ABSL;

       var cnpj = this.CurrentCommon.CNPJ;

       cnpj = cnpj.Trim();

       cnpj = cnpj.Replace(“.”, “”).Replace(“-“, “”).Replace(“/”, “”);

       var cnpjLength = cnpj.Length();

       if (cnpjLength != 14){

             raise CX_INVALIDTAX.Create(“E”);

             posicao = false;

       }

       else{

             var tempCnpj = cnpj.Substring(0, 12);

             var soma;

             var calc;

             var calcCnpj;

             var resto;

             var digito;

    

             // Valida primeiro Digito 

             soma = 0;

             calcCnpj = cnpj.Substring(0, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*5;

                       calcCnpj = cnpj.Substring(1, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*4;

             calcCnpj = cnpj.Substring(2, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*3;

                       calcCnpj = cnpj.Substring(3, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*2;

             calcCnpj = cnpj.Substring(4, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*9;

                       calcCnpj = cnpj.Substring(5, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*8;

             calcCnpj = cnpj.Substring(6, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*7;

                       calcCnpj = cnpj.Substring(7, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*6;

             calcCnpj = cnpj.Substring(8, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*5;

             calcCnpj = cnpj.Substring(9, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*4;

             calcCnpj = cnpj.Substring(10, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*3;

                       calcCnpj = cnpj.Substring(11, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*2;

             resto = (soma % 11);

             if (resto < 2)

                    resto = 0;

             else

                    resto = 11resto;

          

             digito = resto.ToString();

             tempCnpj = tempCnpj + digito;

             if (digito != cnpj.Substring(12, 1)){

                    raise CX_INVALIDTAX.Create(“E”);

                    posicao = false;

             }

             // Valida segundo Digito

                       soma = 0;

             calcCnpj = cnpj.Substring(0, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*6;

             calcCnpj = cnpj.Substring(1, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*5;

                       calcCnpj = cnpj.Substring(2, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*4;

                       calcCnpj = cnpj.Substring(3, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*3;

                       calcCnpj = cnpj.Substring(4, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*2;

                       calcCnpj = cnpj.Substring(5, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*9;

             calcCnpj = cnpj.Substring(6, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*8;

             calcCnpj = cnpj.Substring(7, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*7;

             calcCnpj = cnpj.Substring(8, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*6;

                       calcCnpj = cnpj.Substring(9, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*5;

             calcCnpj = cnpj.Substring(10, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*4;

                       calcCnpj = cnpj.Substring(11, 1);

             calc = Numeric.ParseFromString(calcCnpj);

                       soma = soma + calc*3;

             calcCnpj = cnpj.Substring(12, 1);

             calc = Numeric.ParseFromString(calcCnpj);

             soma = soma + calc*2;

             resto = (soma % 11);

             if (resto < 2)

                    resto = 0;

             else

                    resto = 11resto;

          

             digito.Clear();

             digito = digito + resto.ToString();

             tempCnpj = tempCnpj + digito;

             if (digito != cnpj.Substring(13, 1)){

                    raise CX_INVALIDTAX.Create(“E”);

                    posicao = false;

             }

       }

       return posicao;


Remember to create the following object to use of the extended field for use principally on screen

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.