0

I am new to coding and need some assistance on adding in a parameter to my lightning component button on my Opportunity which displays only the records that have the same Trip_File_LEAD__c(which is a lookup field for a custom object I created called Trip File). I just need some assistance in writing this. Any help is much appreciated! Below is my current code:

APEX:

   public with sharing class UpdateExistingPaxController {

 @AuraEnabled
  public static List <Opportunity> fetchOpportunity() {

    return [SELECT Id, Name, Trip_File_LEAD__c
            FROM Opportunity Limit 30];
    
}

Lightning Component:

  <aura:component controller="UpdateExistingPaxController"
            implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

<aura:attribute name="OpportunityList" type="List" access="global" />
<aura:handler name="init" value="{!this}" action="{!c.loadOpportunities}"/>
<aura:handler event="force:refreshView" action="{!c.loadOpportunities}" />

<div class="slds-grid slds-grid--align-end"> 
    <button class="slds-button slds-button--brand" onclick="{!c.updateFields}">Update</button>
</div>


<table class="slds-table slds-table--bordered slds-table--cell-buffer">
    <thead>
        <tr class="slds-text-title--caps">
            <th style="width:3.25rem;" class="slds-text-align--right">
                <div class="slds-form-element">
                    <div class="slds-form-element__control">
                        <label class="slds-checkbox">
                            <!--header checkbox for select all-->
                            <ui:inputCheckbox aura:id="box3" change="{!c.selectAll}"/>
                            <span class="slds-checkbox--faux"></span>
                            <span class="slds-form-element__label text"></span>
                        </label>
                    </div>
                </div>
            </th>
            <th>
                <span class="slds-truncate">Name</span>      
            </th>
            <th>
                <span class="slds-truncate">Trip File Record</span>
            </th>
           
            </tr>
    </thead>
    
    <tbody>
        <aura:iteration items="{!v.OpportunityList}" var="Opp">
            <tr>
                <td scope="row" class="slds-text-align--right" style="width:3.25rem;">
                    <div class="slds-form-element">
                        <div class="slds-form-element__control">
                            <label class="slds-checkbox">
                                <ui:inputCheckbox text="{!Opp.Id}" aura:id="boxPack" value=""/>
                                <span class="slds-checkbox--faux"></span>
                                <span class="slds-form-element__label text"></span>
                            </label>
                        </div>
                    </div>
                </td>
                <td scope="row">
                    <div class="slds-truncate" title="{!Opp.Name}"><a>{!Opp.Name}</a></div>
                </td>
                <td scope="row">
                    <div class="slds-truncate" title="{!Opp.Trip_File_LEAD__c}">{!Opp.Trip_File_LEAD__c}</div>
                </td>
                 </tr>
        </aura:iteration>
    </tbody>
</table>
     </aura:component>

Controller:

    ({

loadOpportunities: function(component, event, helper) {
    var action = component.get('c.fetchOpportunity');

          action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {

            component.set('v.OpportunityList', response.getReturnValue());
            component.find("box3").set("v.value", false);
        }
    });

    $A.enqueueAction(action);
},
4
  • So if I have 4 record, two records with Trip_File_LEAD__c as 123 and other two records with Trip_File_LEAD__c as 1234, then what should it show? What is the input parameter to check if Trip_File_LEAD__c is same? Is there a filter box somewhere in UI? Dec 15 '20 at 4:32
  • @NagendraSingh Hi Nagendra, so if you have 4 records with the Trip_File_LEAD__c as 123 and the record the Lightning component has been actioned from also has the Trip_File_LEAD__c as 123, it should only display those 4 records as they are related to eachother through the Trip_File_LEAD__c. I don't have a filter box on my UI at the moment, I was just hoping that I could query the Trip_File_LEAD__c field from the hasRecordId, and that would bring up the related records with the same Trip_File_LEAD__c. Is this possible?
    – Sophia
    Dec 15 '20 at 23:14
  • Yes if you already have the component on a record page then you can use hasRecordId, I think I have already answered this type of question here salesforce.stackexchange.com/questions/328837/… Dec 16 '20 at 1:19
  • @NagendraSingh Yes, thank you!
    – Sophia
    Dec 16 '20 at 1:46
0

I figured it out! I just added the recordId into my controller and Apex as follows.

    ({

loadOpportunities: function(component, event, helper) {
    var recordId = component.get('v.recordId')
    var action = component.get('c.fetchOpportunity');
     action.setParams({oppRecordId:recordId});

          action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {

            component.set('v.OpportunityList', response.getReturnValue());
            component.find("box3").set("v.value", false);
        }
    });

Apex:

   public with sharing class UpdateExistingPaxController {

 @AuraEnabled
public static List <Opportunity> fetchOpportunity(String oppRecordId) {

    return [SELECT Id, Name, Trip_File_LEAD__c
            FROM Opportunity
            WHERE Lead_PAX__c = :oppRecordId];
    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.