Trying to get my head around coding in general so full disclaimer i'm new as they come!
What I am trying to achieve is to pass an ID using force:hasRecordId on the aura:component to an APEX CLASS (Apex Controller) in order to return the data for use within the component.
I can see that the ID of the current record (Opportunity) is being passed successfully since the data in the Console logs returns the values from the record iD the component is launched from. Its being launched via a quick action. I can also see the record ID on the component within p tags so no issues here.
Where the issue arises is the return of the data to the component itself. I have been through every thread I can find relating to action.setCallback behaviour and have retyped the code in various manners to try resolve but I think its a general lack of understanding that is making me miss something obvious within the posts. Any help would be greatly appreciated.
Here is the Component In its simplest form: (I have no issue viewing the {!v.recordId}
<aura:component controller="eveGetOpportunity" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:availableForFlowScreens,lightning:isUrlAddressable" access="global">
<aura:attribute name="opp" type="Opportunity[]"/>
<aura:attribute name="recordId" type="string" />
<aura:attribute name="pageReference" type="Object"/>
<aura:handler name="init" value="{! this }" action="{! c.init }"/>
<p>{!v.recordId}</p>
<p>{!v.opp.ContractStartDate__c}</p>
<p>{!v.opp.ContractEndDate__c}</p>
<p>{!v.opp.Name}</p>
</aura:component>
Here is the Controller in its simplest form:
({
init: function (cmp,event,helper){
helper.getData(cmp);
}
})
Here is the Helper in its simplest form: (I would mention that I initially had this in the Contoller rather than as a helper with no difference in format, this did not work either. I also had $A.getCallback included on the callback so - action.setCallback(this, $A.getCallback(function(response) - this also did nothing)
({
getData: function (cmp) {
var action = cmp.get("c.getOppHeader");
action.setParams({ OppId: cmp.get("v.recordId") });
action.setCallback(this, function (response) {
var state = response.getState();
if (state === "SUCCESS") {
cmp.set("v.opp", response.getReturnValue());
} else if (state === "ERROR") {
var errors = response.getError();
console.error(errors);
}
});
$A.enqueueAction(action);
},
})
Here is the Apex Class / Controller:
public class eveGetOpportunity {
@AuraEnabled
public static List<Opportunity> getOppHeader(Id OppId) {
List<Opportunity> Opp =
[SELECT Id, Name,ContractStartDate__c,ContractEndDate__c,ContractTerm__c, DiscountTarget__c FROM Opportunity WHERE id=:OppId];
return Opp;
}
}
{}
code formatting tool in the editor to make sure your code appears correctly (I've done this for you this time, though). I just wanted to take a moment to compliment you on your fine post and welcome you to the community.