1

I am trying out lightning in salesforce. Trying to get Opportunity list on button click.

code for component

<aura:component controller="OpportunityController">
<aura:attribute name="getOpportunities" type="Opportunity[]"/>

<ui:button label="Get Opportunities" press="{!c.getOpps}"/>
<aura:iteration var="getOpportunity" items="{!v.opportunities}">
    <p>{!opportunity.Name} : {!opportunity.CloseDate}</p>
</aura:iteration>
</aura:component>

JS Controller

({
getOpps: function(hello){
    var action = hello.get("c.getOpportunities");
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS") {
            hello.set("v.opportunities", response.getReturnValue());
        }
    });

}
})

Apex Controller

public class OpportunityController {
@AuraEnabled
public static List<Opportunity> getOpportunities() {
    List<Opportunity> opportunities = 
            [SELECT Id, Name, CloseDate FROM Opportunity];
    return opportunities;
}
}

Error

Uncaught error in $A.run() : Unable to find 'getOpportunities' on 'compound://Ravi5380.hello'.

2

There are two potential issues

1)your attribute is wrongly named.It should be just opportunities.

2)your var in the aura:iteration should read opportunity.

Check the below trail

https://developer.salesforce.com/trailhead/lightning_components/lightning_components_apex

2

I wasn't able to reproduce the error message that you were getting, but I got it to work by making changes:

({
getOpps: function(hello){
    var action = hello.get("c.getOpportunities");
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS") {
            hello.set("v.getOpportunities", response.getReturnValue());  // here
        }
    });
    $A.enqueueAction(action);  // here
}
})

and

<aura:component controller="OpportunityController">
<aura:attribute name="getOpportunities" type="Opportunity[]"/>

<ui:button label="Get Opportunities" press="{!c.getOpps}"/>
<aura:iteration var="opportunity" items="{!v.getOpportunities}"> <!-- here -->
    <p>{!opportunity.Name} : {!opportunity.CloseDate}</p>
</aura:iteration>
</aura:component>

As Mohith was saying, watch the variable names, and also make sure that you are enqueueing the Action.

0

Try something like this:

page.cmp

<aura:component description="something" controller="apexController">

<aura:attribute name="opportunities" type="Opportunity[]"/>
<aura:handler name="init" action="{!c.doinit}" value="{!this}"/>

{!v.opportunities.Name}

Controller.js

doinit: function (component, event, helper) {
    helper.getRecords(component, event);



},

Helper.js

({
getRecords: function (component, event) {
    var action = component.get("c.getOpportunities");
    console.log(action);
    action.setCallback(this, function (a) {

        var state = a.getState();

        if(component.isValid() && state === "SUCCESS")
        {
            //debugger;
            var result = a.getReturnValue();
            component.set("v.opportunities", result);
            //  console.log('The opps are :'+JSON.stringify(a.getReturnValue()));
            console.log(a.getReturnValue());
            console.log(component.get("v.opportunities"));
        }
        else
        {
            console.log("Failed to retrieve tests...");
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "title": "Error",
                "message": "Failed to get tests.",
                "type": "error"
            });
            resultsToast.fire();
        }


    });
    $A.enqueueAction(action);
}, })

apexController.cls

public with sharing class FluidConsumptionCtrl {

@AuraEnabled
public static List<Opportunity> getOpportunities() {
    return [select id, name from Opportunity];
} }

I used a helper at my way of solving this problem. So you have a page, on the page you define attributes (object opportunities), which you will use for querying and displaying results (on the page and in the controller in my case also in a component helper). You also need to define a apex controller in which you access apex functions (querying, upserting, deleting...) in this case we only need to query the object Opportunities. In my component I used JS controller and I called a helper with more JS code, to make all work. More words about the helper, I seted a callback (this thread: http://salesforce.stackexchange.com/questions/132629/use-of-setcallback-in-lightning/132638?noredirect=1#comment188941_132638I ) where I checked if component is Valid and value of state is SUCCESS, if valid and state is SUCCESS that it fills the opportunities attribute with results, if not valid than It will throw an error on a Toast.

2
  • Can you add some explanation as you go rather than just a code dump?
    – Adrian Larson
    Mar 8 '17 at 14:23
  • I added some explenation on the code I added, if that will help anyone.
    – ZigaG
    Mar 9 '17 at 7:12

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.