2

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;

    }

}
1
  • 1
    Hi, and welcome to SFSE! This is a pretty great first post; you included all the information we needed without too much extraneous fluff. If you haven't yet, please take a moment to read How to Ask and take the tour. In the future, try to remember to use the {} 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.
    – sfdcfox
    Commented May 5, 2020 at 10:57

1 Answer 1

1

opp is a List (Opportunity[] is a list, while Opportunity would be just a record), but you treat it as a single object ({!v.opp.ContractStartDate__c}), which is why you don't see anything. You need to either iterate over the List, or, more likely, simply just not use it like a List to begin with.

<aura:attribute name="opp" type="Opportunity"/>

...

@AuraEnabled(cacheable=true)
public static Opportunity getOppHeader(Id OppId) {
  return [SELECT Id, Name,ContractStartDate__c,ContractEndDate__c,ContractTerm__c, DiscountTarget__c FROM Opportunity WHERE id=:OppId];
}

These two changes should take your code from non-functional to working just fine.

P.S. It is recommended that you do not define recordId in order to prevent potential errors.

<!-- REMOVE ME -->
<aura:attribute name="recordId" type="string" />

By using the appropriate interface (via implements automatically provides the attribute, and so doing this may redefine the attribute's properties and cause errors.

The same is also true for pageReference.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .