0

I have created an Apex class which takes the opportunity Id and finds me the days between two today and future date, what i'm now struggling to do is display this within my lightning aura component.

Apex Class

public with sharing class DaysUntilShowController {

@AuraEnabled
public static Integer getShowDate(Id OpportunityId){
    system.debug(OpportunityId);

    BaseOpportunities opportunityController = Application.sobjectController.getBaseOpportunityController( new Set<Id> { opportunityId } );
    Opportunity oppRecord = (Opportunity)opportunityController.getRecord( opportunityId );
    system.debug(oppRecord);
    if (oppRecord.CXT_Master_Festival__c == NULL) {
        throw new Application.MissingRequiredValueException( 'Opportunity has no '+SObjectUtil.getFieldLabel(BaseOpportunities.describeResult,'CXT_Master_Festival__c')+' set.', oppRecord );
    }

    CXT_Festivals__c oppFestival = (CXT_Festivals__c)Application.dbController.getRecords( new Set<Id> { oppRecord.CXT_Master_Festival__c },
                        BaseFestivals.getQuerySelect()+' WHERE Id IN :recordIds' )[0];

    Date showDate = oppFestival.CXT_Start_Date__c;
    Date todaysDate = Date.today();

    Integer daysBetween = todaysDate.daysBetween(showDate);

    system.debug(daysBetween);
    return daysBetween;
    }
}

JS Controller

({
doInit : function(component, event, helper) {
    var action = component.get("c.getShowDate");
    action.setParams({ opportunityId : component.get("v.recordId") });

            action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var returnValue = response.getReturnValue();
            component.set( "v.daysBetween", returnValue.daysBetween );

        }
    });

    $A.enqueueAction(action);
},

handleOpportunityUpdated: function(component, event, helper) {
    var eventParams = event.getParams();
    if(eventParams.changeType === "CHANGED") {
        var reInit = component.get('c.doInit');
        $A.enqueueAction(reInit);
    }
}
})

Component

<aura:component controller="DaysUntilShowController" implements="flexipage:availableForRecordHome,force:hasRecordId"
            access="global" >

<aura:handler event="force:refreshView" action="{!c.doInit}"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<aura:attribute name="thisFestival" type="CXT_Festivals__c" />
<aura:attribute name="thisOpportunity" type="Opportunity" /> 
<aura:attribute name="DaysBetween" type ="Integer"/>

<force:recordData aura:id="recordLoader"
    recordId="{!v.recordId}" 
    fields="Id"
    targetFields="{!v.thisOpportunity}"/>

    <lightning:card variant="Narrow">
    <aura:set attribute="title">
        <div class="slds-media slds-media--center slds-has-flexi-truncate">
            <div class="slds-media__figure"><lightning:icon iconName="custom:custom11" class="m2020-icon-sponsorship_level" size="small"/></div>
            <div class="slds-media__body"><span class="slds-card__header-link">Days Left to Show</span></div>
        </div>
    </aura:set>
        <lightning:tile> 
            <dl class="slds-list_horizontal slds-wrap">
            <dt class="slds-item_label slds-text-color_weak slds-truncate">Days to Show:</dt>
                <dd class="slds-item_detail slds-truncate">
                    {!v.daysBetween}
                </dd>
            </dl>
        </lightning:tile>

</lightning:card>

It just displays blank and i can't understand what i'm missing even though the system.debug displays a value in "daysBetween".

1
  • Also it's case sensitive. May 14, 2019 at 4:37

2 Answers 2

2

You have mostly everything right except that you are not setting the return value correctly. The format to set a value is component.set("v.attributeName", value) where it expects the name of the attribute and the value to be set. So in your doInit() function, where you are getting the response, you will need to set the attribute as below assuming you are getting successful response back.

if (state === "SUCCESS") {
    component.set( "v.daysBetween", response.getReturnValue();
}
0

in your callback:

action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
        component.set( "v.daysBetween" ); --> Does nothing
        var returnValue = response.getReturnValue(); --> assigns the server response to an unused variable.

    }
});

You don't seem to actually be setting the attribute value based on the response from your apex class, you are simply calling set attribute to nothing and assigning the response to an unused variable returnValue

Additionally, this is not a lighting web component, it is a lightning aura component.

The documentation, Handling Events with Client-Side Controllers is pretty clear on how to set values based on a server response.

9
  • if you set the value to the variable before declaring it, it will be undefined and wont populate the integer in your markup
    – glls
    May 13, 2019 at 20:00
  • i think i realized my mistake, i updated the these lines - var returnValue = response.getReturnValue(); component.set( "v.daysBetween", returnValue.daysBetween ); this has fixed the undefined issue just need it to appear now on the aura component May 13, 2019 at 20:07
  • var returnValue should be returning the integer, since this is what you are telling your apex method to return, and you are assigning the server response to the variable
    – glls
    May 13, 2019 at 20:08
  • 1
    returnValue.daysBetween wont work - returnValue should suffice
    – glls
    May 13, 2019 at 20:18
  • 1
    exactly =), also make sure you declare your variables before trying to assign them to attributes.
    – glls
    May 13, 2019 at 20:26

You must log in to answer this question.

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