You need to sign in to do that
Don't have an account?
Thierry 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
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
All Answers
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: Also with using of Regular Expressions we can validate an ID, for at least we can check the length of the inputed id.
And, by the way, I spent some hours too for a typo mistake between setparams and setParams. ;-)