2

i have this attribute in my component:

<aura:attribute name="currentRecordDuns" type="String" /> 

im trying to send it to the apex controller.

this is the javascript controller:

({
doInit : function(component, event, helper) {    
    helper.getAccounts(component);
},})  

this is the helper:

({
getAccounts: function(component) {        
    var action = component.get("c.getAccounts");
    action.setParams({
        "currentDuns": component.get("v.currentRecordDuns"),
        "freeTextFilter": component.find("freeTextFilter").get("v.value")            
    });
    //Set up the callback
    var self = this;
    action.setCallback(this, function(actionResult) {
        component.set("v.Accounts", actionResult.getReturnValue());            
    });        
    $A.enqueueAction(action);                
},})

this is the apex code:

public class AccConListController {
    @AuraEnabled
    public static List<Account> getAccounts(String currentDuns,String freeTextFilter) {
        String q = 'SELECT Id, Name,DunsNumber, (select name, Phone, Email FROM Contacts where Name like \'%' + freeTextFilter +  '%\' or Phone like \'%' + freeTextFilter +  '%\' or Email like  \'%' + freeTextFilter +  '%\') FROM Account  where DunsNumber=:currentDuns';
        //q+= ' and Name like \'%' + freeTextFilter +  '%\'';
        List<Account> accounts= Database.query(q);
        return accounts;
    }
}

i didnt get the results i wanted, so i looked at the debug log and saw:

12:13:25:003 VARIABLE_ASSIGNMENT [19]|currentDuns|null

i have no idea why, please advise

EDIT: how do i assign a value to the currentRecordDuns: in my component i call:

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

this is doInit in the controller:

({
doInit : function(component, event, helper) {    
    helper.getDuns(component);
    helper.getLeads(component);
    helper.getAccounts(component);

    component.set("v.showContacts", component.find("checkboxContacts").get("v.value"));
    component.set("v.showLeads", component.find("checkboxLeads").get("v.value"));
},  })

the function the retrieves the currentRecordDuns:

helper.getDuns(component);

this is the function in the helper:

getDuns:  function(component) {  
    var action = component.get("c.getDunsByRecordId");
    action.setParams({
        "currentRecordID": component.get("v.recordId")        
    });
    //Set up the callback
    var self = this;
    action.setCallback(this, function(actionResult) {
        component.set("v.currentRecordDuns", actionResult.getReturnValue());            
    });        
    $A.enqueueAction(action); 
},

and this is the function in the apex controller:

    @AuraEnabled
public static string getDunsByRecordId(id currentRecordID) { 
    String q = 'SELECT DunsNumber FROM Account  where Id=:currentRecordID limit 1';
    List<Account> accounts = Database.query(q);
    return (!accounts.isEmpty()) ? accounts[0].DunsNumber : '';
}
1

found the answer!

thanks to this comment "Since you are calling getDuns and getAccounts methods one after another, the issue is that getDuns's server is not yet finished by the time getAccount's server call is made. When you do a $A.enqueueAction(action);, it enqueues the call, doesn't guarantee realtime response. So you should move your getAccount call inside the setCallback of getDuns"

i just moved the function call from the js controller to the callback function of getDuns.

so instead of:

    doInit : function(component, event, helper) {    
    helper.getDuns(component);
    helper.getLeads(component);
    helper.getAccounts(component);

},  

i wrote:

doInit : function(component, event, helper) {    
    helper.getDuns(component);

},  

and in the function itself i wrote:

getDuns:  function(component) {  
    var action = component.get("c.getDunsByRecordId");
    action.setParams({
        "currentRecordID": component.get("v.recordId")        
    });
    //Set up the callback
    var self = this;
    action.setCallback(this, function(actionResult) {
        component.set("v.currentRecordDuns", actionResult.getReturnValue());  
        this.getAccounts(component);
        this.getLeads(component);
    });        
    $A.enqueueAction(action); 
},

Thanks for everyone who helped.

| improve this answer | |
0

Implement force:hasRecordId to get record id of current record

<aura:component controller="Your" implements="flexipage:availableForAllPageTypes,force:hasRecordId">

Or try to hard-code default id for testing

// In Component //

 <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
 <aura:attribute name="recordId" type="Id" default="001W000000Jrkic"/>

// In Controller //

doInit : function(component, event, helper) {    
    debugger;
    var action = component.get("c.getAccounts");
    action.setParams({
     currentDuns: component.get("v.recordId"),
     freeTextFilter: component.find("freeTextFilter").get("v.value") 
    }); 
    action.setCallback(this,function(response){
            if (response.getState() === "SUCCESS"){
                console.log(response.getReturnValue());
            }
        }
    );
    $A.enqueueAction(action);

}
| improve this answer | |
  • that wasn't my question, i had a record id... – Dana Griff Mar 28 '17 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.