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
bperry8086bperry8086 

Challenge "using apex in components" - where to put code?

Where is the client side controller code supposed to be saved?  Does it go in the Lighning component?  A static resource?  Somewhere else?

I've pasted in into the component so that it is between the "aura:component" tags.  I'm getting a

"The client side controller does not refer to the 'getCaseFromId' method of the Apex controller class"

error.  I suspect I don't have the code in the right place.
Best Answer chosen by bperry8086
Neetu_BansalNeetu_Bansal
Hi,

When you open the component in developer console, in the right hand, you will see a section, Click on controller and paste the code. See the below screen shot:
User-added image
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi,

You need to create a Apex class with the following code:
public with sharing class DisplayCaseController
{
	@AuraEnabled
    public static Case getCaseFromId( Id caseID )
    {
        if( caseID == null )
        {
            return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
        }
        
        List<Case> cases = [ SELECT Id, Subject, Description, Status from CASE where ID = :caseID ];
        
        if( cases.size() == 0 )
        {
            return [SELECT ID, Subject, Description, STATUS from Case LIMIT 1];
        }
        else
        {
            return cases[0];
        }        
    }
}
Let me know, if you need any other help.

Thanks,
Neetu
bperry8086bperry8086
That's the server side Apex controller.  I'm talking about the client-side controller.  Here's one example from the challenge.  Unfortunately, the challenge does not explain where to save it.  It may be obvious for those who already know how to do this, but it is not clear to me.
 
({
    getOpps: function(cmp){
        var action = cmp.get("c.getOpportunities");
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.opportunities", response.getReturnValue());
            }
        });
     $A.enqueueAction(action);
    }
})

 
Neetu_BansalNeetu_Bansal
Hi,

When you open the component in developer console, in the right hand, you will see a section, Click on controller and paste the code. See the below screen shot:
User-added image
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
bperry8086bperry8086
Thanks Neetu, that's exactly what I was looking for.  I'm stil a bit lost in this new Lightning world.