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
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4 

Error in display my account on lightning component

I'm Trying display the result of my query in salesforce lightning component, but when I hava recieve this error:
User-added image
My code:

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" onblur="{! c.handleBlur }"/>
</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();x
        if(component.isValid() && STATE==Sucess)
        {
            component.set("v.acctList",response.getReturnValue());
        }
        });
        $A.enqueueAction(action);
    }
})
Best Answer chosen by Leticia Monteiro Freitas 4
Khan AnasKhan Anas (Salesforce Developers) 
Yes, that's what I told you earlier. Your method return type is void. That's why you are getting null in JavaScript controller. getvalidKey return type is void. Nothing is returning to JS controller, that's why you are getting null in JS controller.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Leticia,

Greetings to you!

In the controller, you have extra in this line: var state = response.getState();x

Change your controller to:
 
({
    /* 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==Sucess)
        {
            component.set("v.acctList",response.getReturnValue());
        }
        });
        $A.enqueueAction(action);
    }
})

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
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
Thank You Khan :) But my data stil doesent show up in my component. Can you help me?

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Leticia,

As lightning is based on aura framework which is a type of JavaScript and JavaScript is case sensitive so lightning is case sensitive. So, instead of if(component.isValid() && STATE==Sucess) you need to use  if(component.isValid() && state=="SUCCESS")

I hope it helps you!

Regards,
Khan Anas
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
Khan, I've already done this, but I still can show the records.
Khan AnasKhan Anas (Salesforce Developers) 
The problem is you are not returning any value in Apex controller. Your method 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.

Regards,
Khan Anas
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
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;
Khan, I've return a Account List from my query.
 
Khan AnasKhan Anas (Salesforce Developers) 
You are calling getvalidKey method in JS controller: var action = component.get("c.getvalidKey");
But, there is a void return type for that method.

You need to call getfoundAccount in JS controller and set both the parameters correctly in JS controller.
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
Khan, my method validKey call my method getFoundAccount. 
 
Khan AnasKhan Anas (Salesforce Developers) 
Yes, that's what I told you earlier. Your method return type is void. That's why you are getting null in JavaScript controller. getvalidKey return type is void. Nothing is returning to JS controller, that's why you are getting null in JS controller.
This was selected as the best answer