You need to sign in to do that
Don't have an account?
Leticia Monteiro Freitas 4
My records doesent show up on a lightning component
Hey, My data doesent show up in my componenet even when I get succes. Can anyone helpe me?
Component:
<aura:component controller="CEC_SearchAccount" implements="force:appHostable">
<aura:attribute name="acctList" type="Account[]" />
<b> <lightning:input aura:id="searchText" name="searchText" label="Enter some text" />
</b><lightning:button variant="brand" label="Buscar" onclick="{!c.handleClick}"/>
<aura:iteration items="{!v.acctList}" var="a">
<p>{!a.Name}</p>
</aura:iteration>
</aura:component>
controller:
({
/* Dentro da ação de clicar no botão, a classe controladora aponta para a função validKey*/
handleClick : function(component, event, helper) {
var action = component.get("c.getvalidKey");
var input_text = component.find("searchText").get("v.value");
console.log("input_text"+ input_text);
debugger;
action.setParams({"text":input_text});
action.setCallback(this,function(response)
{
var state = response.getState();
if(component.isValid() && state=="SUCCESS")
{
component.set("v.acctList",response.getReturnValue());
debugger;
}
});
$A.enqueueAction(action);
}
})
class:
/*Versão 3.00 - US_179 - Leticia Freitas - 13/11
Classe que recebe uma string, determina se é um cpf ou um cnpj e a partir dele, realiza a busca dessa conta.
Caso exista o registro, retorna as informações: Nome, Tipo de Documento (CPF ou CNPJ), Número do documento, Número do Documento Formatado, Logradouro, CEP, Cidade e Estado.
*/
public class CEC_SearchAccount{
public static List<Account> getfoundAccount(String Key, String documentType)
{
system.debug('Entrou na busta de conta');
List<Account> Conta = [Select Name from Account
where DocumentNumber__c =:Key AND DocumentType__c =:documentType];
if(Conta != NULL)
{
system.debug('Tem registros');
}
return Conta;
}
public static void getvalidCPF(String CPF)
{
/* Cálcula se o cpf é valido através do cálculo dos digitos validadores.*/
integer value;
integer coeff = 10, sum=0, rest;
String dig10,dig11;
system.debug('Entrou na getValidCPF');
//Calcula o primeiro digito validador do CPF
for(integer i=0; i<9;i++)
{
system.debug('I: '+ i);
system.debug('Coeff: '+ coeff);
value = Integer.valueOf(CPF.substring(i,i+1))* coeff;
system.debug('Valor da Substring:'+ value);
coeff--;
system.debug('Coeff depois do incremento:'+ coeff);
sum = sum + value ;
system.debug('Sum:'+ sum);
}
rest = 11 - math.mod(sum,11);
system.debug('Resto:'+rest);
if((rest == 10) || (rest == 11))
{
dig10 = '0';
}else{
dig10 = String.valueOf(rest);
}
sum = 0;
coeff = 11;
value = 0;
rest =0;
system.debug('Começou a lógica do digito 10');
for(integer i=0;i<10;i++)
{
system.debug('I: '+ i);
system.debug('Coeff: '+ coeff);
value = Integer.valueOf(CPF.substring(i,i+1))*coeff;
coeff--;
sum = sum + value;
system.debug('Coeff depois do incremento:'+ coeff);
system.debug('Soma'+ sum);
}
system.debug('Soma fora do loop'+ sum);
system.debug('Resto antes da função'+rest);
integer restb = math.mod(sum,11);
rest = 11 - restb;
system.debug('Res normal'+rest);
if((rest == 10) || (rest ==11))
{
dig11 = '0';
}else{
dig11=String.valueOf(rest);
system.debug('Entrou no if do dig 11');
}
system.debug('CPF dig 11'+cpf.substring(10,11));
system.debug('CPF dig 10'+cpf.substring(9,10));
system.debug('DIG 11'+dig11);
system.debug('DIG 10'+dig10);
// Validate if the calculus match
if((dig11 == cpf.substring(10,11)) && (dig10 == cpf.substring(9,10)))
{
system.debug('Match');
String documentType = 'CPF';
getfoundAccount(CPF,documentType);
}else{
system.debug('CNPJ iNVÁLIDO');
}
}
public static void getvalidCNPJ(String CNPJ)
{
integer value;
integer coeff=12,sum,rest;
String dig13,dig14;
for(integer i = 0; i < 12;i++)
{
value = Integer.valueOf(CNPJ.substring(i,1)) * coeff;
coeff--;
sum = sum + value;
}
rest =Math.mod(sum,11);
if((rest == 0) || (rest == 1))
{
dig13 = '0';
}else{
dig13 = String.valueOf(11 - rest);
}
// Calculando o segundo digito verificador do CNPJ
rest=0;
coeff=13;
for(integer i = 0;i<13;i++)
{
value = Integer.valueOf(CNPJ.substring(i,1)) * coeff;
coeff--;
sum = sum + value;
}
rest = math.mod(sum,11);
if((rest == 0) || (rest == 1))
{
dig14 = '0';
}else{
dig14 = String.valueOf(11 - rest);
}
if((CNPJ.substring(0,13) == dig13) && (CNPJ.substring(0,14)== dig14))
{
String documentType ='CNPJ';
getfoundAccount(CNPJ, documentType);
}else{
//
}
}
@AuraEnabled
Public static void getvalidKey(String text)
{
/*Após ler a string de caracteres, o sistema chama a função de cpf ou cnpj conformo o tamanho da string */
system.debug('Chegou na função da classe agora o/');
system.debug('Texto:'+text);
system.debug(text.length());
if(text.length() == 11)
{ system.debug('CPF');
getvalidCPF(text);
}else if(text.length() == 14)
{
system.debug('CNPJ');
getvalidCNPJ(text);
}else{
system.debug('CPF/CNPJ Inválido');
}
}
}
Component:
<aura:component controller="CEC_SearchAccount" implements="force:appHostable">
<aura:attribute name="acctList" type="Account[]" />
<b> <lightning:input aura:id="searchText" name="searchText" label="Enter some text" />
</b><lightning:button variant="brand" label="Buscar" onclick="{!c.handleClick}"/>
<aura:iteration items="{!v.acctList}" var="a">
<p>{!a.Name}</p>
</aura:iteration>
</aura:component>
controller:
({
/* Dentro da ação de clicar no botão, a classe controladora aponta para a função validKey*/
handleClick : function(component, event, helper) {
var action = component.get("c.getvalidKey");
var input_text = component.find("searchText").get("v.value");
console.log("input_text"+ input_text);
debugger;
action.setParams({"text":input_text});
action.setCallback(this,function(response)
{
var state = response.getState();
if(component.isValid() && state=="SUCCESS")
{
component.set("v.acctList",response.getReturnValue());
debugger;
}
});
$A.enqueueAction(action);
}
})
class:
/*Versão 3.00 - US_179 - Leticia Freitas - 13/11
Classe que recebe uma string, determina se é um cpf ou um cnpj e a partir dele, realiza a busca dessa conta.
Caso exista o registro, retorna as informações: Nome, Tipo de Documento (CPF ou CNPJ), Número do documento, Número do Documento Formatado, Logradouro, CEP, Cidade e Estado.
*/
public class CEC_SearchAccount{
public static List<Account> getfoundAccount(String Key, String documentType)
{
system.debug('Entrou na busta de conta');
List<Account> Conta = [Select Name from Account
where DocumentNumber__c =:Key AND DocumentType__c =:documentType];
if(Conta != NULL)
{
system.debug('Tem registros');
}
return Conta;
}
public static void getvalidCPF(String CPF)
{
/* Cálcula se o cpf é valido através do cálculo dos digitos validadores.*/
integer value;
integer coeff = 10, sum=0, rest;
String dig10,dig11;
system.debug('Entrou na getValidCPF');
//Calcula o primeiro digito validador do CPF
for(integer i=0; i<9;i++)
{
system.debug('I: '+ i);
system.debug('Coeff: '+ coeff);
value = Integer.valueOf(CPF.substring(i,i+1))* coeff;
system.debug('Valor da Substring:'+ value);
coeff--;
system.debug('Coeff depois do incremento:'+ coeff);
sum = sum + value ;
system.debug('Sum:'+ sum);
}
rest = 11 - math.mod(sum,11);
system.debug('Resto:'+rest);
if((rest == 10) || (rest == 11))
{
dig10 = '0';
}else{
dig10 = String.valueOf(rest);
}
sum = 0;
coeff = 11;
value = 0;
rest =0;
system.debug('Começou a lógica do digito 10');
for(integer i=0;i<10;i++)
{
system.debug('I: '+ i);
system.debug('Coeff: '+ coeff);
value = Integer.valueOf(CPF.substring(i,i+1))*coeff;
coeff--;
sum = sum + value;
system.debug('Coeff depois do incremento:'+ coeff);
system.debug('Soma'+ sum);
}
system.debug('Soma fora do loop'+ sum);
system.debug('Resto antes da função'+rest);
integer restb = math.mod(sum,11);
rest = 11 - restb;
system.debug('Res normal'+rest);
if((rest == 10) || (rest ==11))
{
dig11 = '0';
}else{
dig11=String.valueOf(rest);
system.debug('Entrou no if do dig 11');
}
system.debug('CPF dig 11'+cpf.substring(10,11));
system.debug('CPF dig 10'+cpf.substring(9,10));
system.debug('DIG 11'+dig11);
system.debug('DIG 10'+dig10);
// Validate if the calculus match
if((dig11 == cpf.substring(10,11)) && (dig10 == cpf.substring(9,10)))
{
system.debug('Match');
String documentType = 'CPF';
getfoundAccount(CPF,documentType);
}else{
system.debug('CNPJ iNVÁLIDO');
}
}
public static void getvalidCNPJ(String CNPJ)
{
integer value;
integer coeff=12,sum,rest;
String dig13,dig14;
for(integer i = 0; i < 12;i++)
{
value = Integer.valueOf(CNPJ.substring(i,1)) * coeff;
coeff--;
sum = sum + value;
}
rest =Math.mod(sum,11);
if((rest == 0) || (rest == 1))
{
dig13 = '0';
}else{
dig13 = String.valueOf(11 - rest);
}
// Calculando o segundo digito verificador do CNPJ
rest=0;
coeff=13;
for(integer i = 0;i<13;i++)
{
value = Integer.valueOf(CNPJ.substring(i,1)) * coeff;
coeff--;
sum = sum + value;
}
rest = math.mod(sum,11);
if((rest == 0) || (rest == 1))
{
dig14 = '0';
}else{
dig14 = String.valueOf(11 - rest);
}
if((CNPJ.substring(0,13) == dig13) && (CNPJ.substring(0,14)== dig14))
{
String documentType ='CNPJ';
getfoundAccount(CNPJ, documentType);
}else{
//
}
}
@AuraEnabled
Public static void getvalidKey(String text)
{
/*Após ler a string de caracteres, o sistema chama a função de cpf ou cnpj conformo o tamanho da string */
system.debug('Chegou na função da classe agora o/');
system.debug('Texto:'+text);
system.debug(text.length());
if(text.length() == 11)
{ system.debug('CPF');
getvalidCPF(text);
}else if(text.length() == 14)
{
system.debug('CNPJ');
getvalidCNPJ(text);
}else{
system.debug('CPF/CNPJ Inválido');
}
}
}
Greetings to you!
The problem is you are not returning any value in Apex controller. Your method getvalidKey return type is void. That's why you are getting null in JavaScript controller. You need to return value in Apex controller if you want that value in client controller.
I have made a few changes in Apex controller and it is working fine now.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas