I have tried to put multiple functions into the one Component Controller unsuccessfully. I then created a helper class that the Component Controller calls and will run the different methods. This was unsuccessful as well. The error I am getting is either: Uncaught Action failed: c:RealEstateComponent$controller$newMortgage [cmp is not defined]
when I attempt to pass the Helper class a parameter — or — Uncaught Action failed: c:RealEstateComponent$controller$newMortgage [Cannot read property 'get' of undefined]
It can not understand the .get() method and I am not sure how to move forward with putting in multiple JS functions in either the Controller or the Helper class. Could someone shed some light on what I am doing wrong?
COMPONENT
({
"newHome" : function(cmp, event, helper) {
helper.newHome(cmp);
/*var action = cmp.get("c.newHomeAPEX");
action.setParams({
"name" : cmp.find("houseNameInput").get("v.value"),
"bed" : cmp.find("bedInput").get("v.value"),
"bath" : cmp.find("bathInput").get("v.value"),
"state" : cmp.find("stateInput").get("v.value"),
"city" : cmp.find("cityInput").get("v.value"),
"zipCode" : cmp.find("zipCodeInput").get("v.value"),
"listingPrice" : cmp.find("listingPriceInput").get("v.value"),
"address" : cmp.find("houseAddressInput").get("v.value") });
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert('it worked!');
}
else if (state === "INCOMPLETE") {
alert('incomplete');
}
else if (state === "ERROR") {
alert('error');
}
});
$A.enqueueAction(action);*/
},
"newEscrow" : function(cmp, event, helper) {
helper.newEscrow(cmp);
/*var action = cmp.get("c.newEscrowAPEX");
action.setParams({
"name" : cmp.find("escrowNameInput").get("v.value"),
"mortgage" : cmp.find("mortgageCompanyInput").get("v.value"),
"cost" : cmp.find("escrowCostInput").get("v.value"),
"yearFounded" : cmp.find("foundedInput").get("v.value")
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert('it worked!');
}
else if (state === "INCOMPLETE") {
alert('incomplete');
}
else if (state === "ERROR") {
alert('error');
}
});
$A.enqueueAction(action);*/
},
"newMortgage" : function(cmp, event, helper) {
helper.newMortgage(cmp);
/*var action = cmp.get("c.newMortgageAPEX");
action.setParams({
"name" : cmp.find("mortgageNameInput").get("v.value"),
"loanOfficer" : cmp.find("loanOfficerInput").get("v.value"),
"loanAmount" : cmp.find("loanAmountInput").get("v.value"),
"underwriter" : cmp.find("underwriterInput").get("v.value"),
"escrowName" : cmp.find("escrowInput").get("v.value")
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert('it worked!');
}
else if (state === "INCOMPLETE") {
alert('incomplete');
}
else if (state === "ERROR") {
alert('error');
}
});
$A.enqueueAction(action);*/
}
})
HELPER
({
newHome : function(cmp) {
var action = cmp.get("c.newHomeAPEX");
action.setParams({
"name" : cmp.find("houseNameInput").get("v.value"),
"bed" : cmp.find("bedInput").get("v.value"),
"bath" : cmp.find("bathInput").get("v.value"),
"state" : cmp.find("stateInput").get("v.value"),
"city" : cmp.find("cityInput").get("v.value"),
"zipCode" : cmp.find("zipCodeInput").get("v.value"),
"listingPrice" : cmp.find("listingPriceInput").get("v.value"),
"address" : cmp.find("houseAddressInput").get("v.value") });
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert('it worked!');
}
else if (state === "INCOMPLETE") {
alert('incomplete');
}
else if (state === "ERROR") {
alert('error');
}
});
$A.enqueueAction(action);
},
newEscrow: function(cmp){
var action = cmp.get("c.newEscrowAPEX");
action.setParams({
"name" : cmp.find("escrowNameInput").get("v.value"),
"mortgage" : cmp.find("mortgageCompanyInput").get("v.value"),
"cost" : cmp.find("escrowCostInput").get("v.value"),
"yearFounded" : cmp.find("foundedInput").get("v.value")
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert('it worked!');
}
else if (state === "INCOMPLETE") {
alert('incomplete');
}
else if (state === "ERROR") {
alert('error');
}
});
$A.enqueueAction(action);
},
newMortgage : function(cmp){
var action = cmp.get("c.newMortgageAPEX");
action.setParams({
"name" : cmp.find("mortgageNameInput").get("v.value"),
"loanOfficer" : cmp.find("loanOfficerInput").get("v.value"),
"loanAmount" : cmp.find("loanAmountInput").get("v.value"),
"underwriter" : cmp.find("underwriterInput").get("v.value"),
"escrowName" : cmp.find("escrowInput").get("v.value")
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert('it worked!');
}
else if (state === "INCOMPLETE") {
alert('incomplete');
}
else if (state === "ERROR") {
alert('error');
}
});
$A.enqueueAction(action);
}
})
APEX
public class RealEstateClass {
@AuraEnabled
public static String newHomeAPEX(String city, String state, Integer zipCode, String name, String address, Integer bed, Integer bath, Double listingPrice){
system.debug('values:: ' + name);
Real_Estate__c re = new Real_Estate__c(Zip_Code__c = zipCode, State__c = state, City__c=city, Name = name, Street_Address__c = address, Bed__c = bed, Bath__c = bath, Listing_Price__c = listingPrice);
insert re;
system.debug('re: ' + re);
return ('Hello from the server, ' + name);
}
@AuraEnabled
public static void newEscrowAPEX(Id mortgage, Date yearFounded, String name, Double cost){
system.debug('values:: ' + name);
Escrow__c es = new Escrow__c(Mortgage_Company__c = mortgage, Year_Founded__c = yearFounded, Name =name, Cost__c = cost);
insert es;
system.debug('es: ' + es);
}
@AuraEnabled
public static void newMortgageAPEX(Id loanOfficer, String name, Double loanAmount, Id underwriter, Id escrowName){
system.debug('values:: ' + name);
Mortgage_Company__c mort = new Mortgage_Company__c(Name = name, Loan_Amount__c = loanAmount, Loan_Officer__c =loanOfficer, Underwriter__c = underwriter, Escrow__c = escrowName);
insert mort;
system.debug('mort: ' + mort);
}
}
COMPONENT INPUT
<!--Escrow-->
<div class="slds-form-element">
<label class="slds-form-element__label" for="text-input-id-1">Escrow Name</label>
<ui:inputText class="form-control" aura:id="escrowNameInput" />
</div>
<!--<div class="slds-form-element">
<label class="slds-form-element__label" for="text-input-id-1">Mortgage Company</label>
<ui:inputText class="form-control" aura:id="mortgageCompanyInput" />
</div>-->
<div class="slds-form-element">
<label class="slds-form-element__label" for="text-input-id-1">Cost</label>
<ui:inputText class="form-control" aura:id="escrowCostInput" />
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="text-input-id-1">Year Founded</label>
<ui:inputText class="form-control" aura:id="foundedInput" />
</div>
<ui:button label="Save" press="{!c.newEscrow}" class="btn" />
<hr></hr>
COMPONENT OUTPUT
<table class="slds-table slds-table--bordered slds-table--striped">
<thead>
<tr>
<th scope="col"><span class="slds-truncate">Escrow Company</span></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.getEscrow}" var="e">
<tr>
<td>{!e.Name}</td>
<td><button class="slds-button"><a href="{!e.Id}" target="_blank"> Edit</a></button></td>
</tr>
</aura:iteration>
</tbody>
</table>
Best Answer
In your helper, modify the function definitions to accept the component as a parameter, which you're already passing from the controller:
newHome : function() {
tonewHome : function(cmp) {
and
newEscrow: function(){
tonewEscrow: function(cmp){
For the last one
newMortgage : function(){
you're not passing anything to it but then inside the function you're trying to referencecmp
... Change both, the controller -helper.newMortgage(cmp);
and the helpernewMortgage : function(cmp){