function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
brunol11brunol11 

CNPJ Validation Rules - Regras de Validação de CNPJ

Parte 1 - Digito Verificador 1 ( aos brasileiros: dá jóinha, please!)
IF( LEN(CPF_ou_CNPJ__c)= 14 , OR(AND(MOD((
( VALUE( MID( CPF_ou_CNPJ__c , 1, 1 ) ) * 6 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 2, 1 ) ) * 5 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 3, 1 ) ) * 4 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 4, 1 ) ) * 3 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 5, 1 ) ) * 2 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 6, 1 ) ) * 9 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 7, 1 ) ) * 8 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 8, 1 ) ) * 7 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 9, 1 ) ) * 6 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 10, 1 ) ) * 5 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 11, 1 ) ) * 4 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 12, 1 ) ) * 3 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 13, 1 ) ) * 2 )
), 11
) <= 1
, VALUE( MID( CPF_ou_CNPJ__c , 14, 1 ) ) != 0
)
, AND(MOD((
( VALUE( MID( CPF_ou_CNPJ__c , 1, 1 ) ) * 6 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 2, 1 ) ) * 5 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 3, 1 ) ) * 4 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 4, 1 ) ) * 3 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 5, 1 ) ) * 2 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 6, 1 ) ) * 9 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 7, 1 ) ) * 8 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 8, 1 ) ) * 7 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 9, 1 ) ) * 6 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 10, 1 ) ) * 5 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 11, 1 ) ) * 4 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 12, 1 ) ) * 3 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 13, 1 ) ) * 2 ))
, 11 ) > 1
, ( 11 - MOD((
( VALUE( MID( CPF_ou_CNPJ__c , 1, 1 ) ) * 6 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 2, 1 ) ) * 5 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 3, 1 ) ) * 4 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 4, 1 ) ) * 3 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 5, 1 ) ) * 2 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 6, 1 ) ) * 9 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 7, 1 ) ) * 8 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 8, 1 ) ) * 7 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 9, 1 ) ) * 6 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 10, 1 ) ) * 5 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 11, 1 ) ) * 4 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 12, 1 ) ) * 3 ) +
( VALUE( MID( CPF_ou_CNPJ__c , 13, 1 ) ) * 2 )
)
, 11
) ) != VALUE( MID( CPF_ou_CNPJ__c , 14, 1 ))
)), false)
brunol11brunol11
Parte 2 - Dígito verificador 2 - (Dá joinha! - Thumbs up!)

IF(LEN(CPF_ou_CNPJ__c)=14,OR(AND(MOD((
(VALUE(MID(CPF_ou_CNPJ__c,1,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,2,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,3,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,4,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,5,1))*2)+
(VALUE(MID(CPF_ou_CNPJ__c,6,1))*9)+
(VALUE(MID(CPF_ou_CNPJ__c,7,1))*8)+
(VALUE(MID(CPF_ou_CNPJ__c,8,1))*7)+
(VALUE(MID(CPF_ou_CNPJ__c,9,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,10,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,11,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,12,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,13,1))*2))
,11)<=1
,VALUE(MID(CPF_ou_CNPJ__c,14,1))!=0)
,AND(MOD((
(VALUE(MID(CPF_ou_CNPJ__c,1,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,2,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,3,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,4,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,5,1))*2)+
(VALUE(MID(CPF_ou_CNPJ__c,6,1))*9)+
(VALUE(MID(CPF_ou_CNPJ__c,7,1))*8)+
(VALUE(MID(CPF_ou_CNPJ__c,8,1))*7)+
(VALUE(MID(CPF_ou_CNPJ__c,9,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,10,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,11,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,12,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,13,1))*2))
,11)>1
,(11-MOD((
(VALUE(MID(CPF_ou_CNPJ__c,1,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,2,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,3,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,4,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,5,1))*2)+
(VALUE(MID(CPF_ou_CNPJ__c,6,1))*9)+
(VALUE(MID(CPF_ou_CNPJ__c,7,1))*8)+
(VALUE(MID(CPF_ou_CNPJ__c,8,1))*7)+
(VALUE(MID(CPF_ou_CNPJ__c,9,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,10,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,11,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,12,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,13,1))*2)
),11
))!=VALUE(MID(CPF_ou_CNPJ__c,14,1)))),false)
 
brunol11brunol11
Eu uso um campo só para CPF e CNPJ, portanto valido a entrada primeiro pelo número de caracteres e depois valido separado o CPF e o CNPJ de acordo com o número de caracteres registrado no campo.

Valida Tamanho CPF ou CNPJ:
AND( NOT(ISBLANK(CPF_ou_CNPJ__c)), AND(NOT(LEN(CPF_ou_CNPJ__c)=11), NOT(LEN(CPF_ou_CNPJ__c)=14) ))

Será que há uma maneira de fazer um loop nas regras de vaildação para simplificar esse código? Se alguém souber, posta, por favor.
Is there any way to write a loop in validation rule so it could be more simple?
William TranWilliam Tran
Could you translate into English?

Thx
brunol11brunol11
Those are 3 validation rules for the Brazilian  "Enterprises National Code" (or something like that) so called CNPJ. An unique identifier for bussines.
 Is there any way to write a loop in validation rule so it could be more simple? the number has 14 digits, and the last 2 are the checksum.
William TranWilliam Tran
The easiest is to define another formula field say, Another_fomula__c ( of type number)

Another_fomula__c=
MOD((
(VALUE(MID(CPF_ou_CNPJ__c,1,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,2,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,3,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,4,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,5,1))*2)+
(VALUE(MID(CPF_ou_CNPJ__c,6,1))*9)+
(VALUE(MID(CPF_ou_CNPJ__c,7,1))*8)+
(VALUE(MID(CPF_ou_CNPJ__c,8,1))*7)+
(VALUE(MID(CPF_ou_CNPJ__c,9,1))*6)+
(VALUE(MID(CPF_ou_CNPJ__c,10,1))*5)+
(VALUE(MID(CPF_ou_CNPJ__c,11,1))*4)+
(VALUE(MID(CPF_ou_CNPJ__c,12,1))*3)+
(VALUE(MID(CPF_ou_CNPJ__c,13,1))*2))
,11)

Your final formula is:
IF(LEN(CPF_ou_CNPJ__c)=14,OR(AND(Another_fomula__c<=1
,VALUE(MID(CPF_ou_CNPJ__c,14,1))!=0)
,AND(Another_fomula__c>1
,(11-Another_fomula__c)!=VALUE(MID(CPF_ou_CNPJ__c,14,1)))),false)

Thx
 
William TranWilliam Tran
Brunol, did you try it out?

Thx
brunol11brunol11
That was kind of brilliant! But only validates the first of the last two digits. I still cant figure out how to apply for the last one.
William TranWilliam Tran

What is the logic for last one?

Thx.

William TranWilliam Tran
Brunol,  let me know if you still have questions.

Otherwise, as a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

This will help keep the forum clean and help future users determine what answers are useful
and what answer was the best in resolving the user's issue. 

Thanks
Ricardo MendonçaRicardo Mendonça
Olá @brunol11, estarei fazendo a dica que você postou. Eu colocaria toda essa formula (Parte 1 e 2) junta em um campo formulá tipo número?
Padre DaviPadre Davi
Oi @brunol11 . Sou um usuário iniciante. Também tive a mesma dúvida do amigo acima. Essas 3 fórmulas são colocadas juntas ???? 1ª Parte, 2ª Parte e a fórmula da validação se é cpf ou cnpj???? Se são separadadas como seria.... Estou precisando validar e não consigo...

Obrigado

Hi @ brunol11. I'm a novice user. I also had the same question as the friend above. These 3 formulas are put together ???? 1st Part, 2nd Part and the validation formula if it is cpf or cnpj ???? If they are separated as it would be .... I need to validate and I can't ...

Thanks
transe divain 1transe divain 1
Jack Listens Survey lets the company find out what their customers think about their services, foods, and stores, so they can improve to the next level.
www jacklistens com
jesszi12 joejesszi12 joe
Great Post! Thank you for visiting here. https://nextgen-patientportal.com/