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
Thierry JORANDThierry JORAND 

Trailhead Using Apex in Components : how to make it better

Hi,

I have just passed the challenge Using Apex in Components. But i am not satisfied with my solution. Here is it :
Component :
<aura:component controller="DisplayCaseController">
    <aura:attribute name="record" type="Case"/>
    <aura:attribute name="caseId" type="String" default="500F000000XgaGj"/>
    <br/>
    <ui:inputText label="Case Id: " value="{!v.caseId}"/>
    <ui:button label="Get Case" press="{!c.getCase}" />
    <br/>
    <ui:outputText value="{!v.record.Subject}"/>
    <br/>
    <ui:outputTextArea value="{!v.record.Description}"/>
    <br/>
    <ui:outputText value="{!v.record.Status}"/>
    <br/>
</aura:component>

Client Side Controller
({
    getCase : function(component) {
        var action=component.get("c.getCaseFromId");
        action.setParams({"caseID" : component.get("v.caseId")});
        action.setCallback(this, function(a){
            if (a.getState() === "SUCCESS") {
                component.set("v.record",a.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})
The weaknesses are in my opinion :
* if no id or a bad id like 25000 or 0 is given as input then " An internal server error has occurred Error ID: 1632438745-20058 (-467814789)" is displayed.
  The server side component got an exception 15:10:49:826 FATAL_ERROR System.StringException: Invalid id: 25000 or 0
* If the input id is a contact id for example then the apex controller works as expected and returns the '"first" contact.
* I can figure out how to get a case id set to null in the server side controller nor how to validate the id in the client side controller.
Could someone give some clue or some reference to improve this solution ?
Thanks in advance

 
Best Answer chosen by Thierry JORAND
ShotShot
Ok, i checked again, some small modification:
({
    getCase : function(component) {
        var action=component.get("c.getCaseFromId");
        var caseID = component.get("v.caseId");
        if (caseID.toString().length < 15){
            alert("Wrong ID");
            component.set("v.caseId", "");
        } else{        
            action.setParams({"caseID" : caseID});
            action.setCallback(this, function(a){
                if (a.getState() === "SUCCESS") {
                    component.set("v.record",a.getReturnValue());
                }
            });
            $A.enqueueAction(action);
        }
    }
})

 

All Answers

ShotShot
I spent near two hours, couldn't understand why it doesn't work..
I wrote CallBack, but had to write Callback
That's absolutely terrible that dev console doesnt check and doesnt show this kind of mistake!

About your question, you can validate it in your client-side controller:
var isEmpty = $A.util.isEmpty(cmp.get("v.record"));
if (!isEmpty){
 //Callback to serverSide
} else {
   alert("You didnt input Id")
   break;
}
Also with using of Regular Expressions we can validate an ID, for at least we can check the length of the inputed id.
 
 
ShotShot
Ok, i checked again, some small modification:
({
    getCase : function(component) {
        var action=component.get("c.getCaseFromId");
        var caseID = component.get("v.caseId");
        if (caseID.toString().length < 15){
            alert("Wrong ID");
            component.set("v.caseId", "");
        } else{        
            action.setParams({"caseID" : caseID});
            action.setCallback(this, function(a){
                if (a.getState() === "SUCCESS") {
                    component.set("v.record",a.getReturnValue());
                }
            });
            $A.enqueueAction(action);
        }
    }
})

 
This was selected as the best answer
Thierry JORANDThierry JORAND
Thank you very much, Bogdan. Your answers are very helpful. That's exactly what I needed to better understand this exercise.
And, by the way, I spent some hours too for a typo mistake between setparams and setParams. ;-)