function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Heather_HansonHeather_Hanson 

Need to pass record ID to class related to Lightning Component for .csv download

I've been working with the compent offered on this site:
https://sfdcmonkey.com/2017/04/19/download-csv-file-data-salesforce-lightning-component/

It's working perfectly, but one thing I missed is that it seems to pull all user records in my org, but really I just want to see those that are related to the Programming__c record where the button is clicked.

Is there a way to achieve this in my class?
 
public class User_List {
 
@AuraEnabled
   public static list <User_List__c> fetchList(){
      
      List <User_List__c> returnUserList = new List < User_List__c > ();
        
      for(User_List__c ul: [SELECT 	id, First_Name__c, Last_Name__c, Email__c, Extension_Number__c, Type_of_User_License__c, Phone_Model__c, Language__c, Outbound_Number_Display__c, Outbound_Name_Display__c, Voicemail_Option__c, CFNR_Number__c From User_List__c LIMIT 1000]) {
             returnUserList.add(ul);
          }
         return returnUserList;
   }
}

 
Ravi Dutt SharmaRavi Dutt Sharma
Hi Heather,

How are Programming__c and User_List__c associated with each other? If the button is placed on the Programming__c object record page, then you need to implement the force:hasRecordId interface in your aura component. By implementing this interface, you will get the record if of programming record in your aura component by using component.get("v.recordId"). You can then pass this recordId to your apex controller and use the recordId in the WHERE clause of the SOQL to filter the records. Please let me know if you have any questions.

Thanks,
Ravi
Heather_HansonHeather_Hanson
Hi Ravi,

Thanks for your response!  You assumed correctly.  The button is on Programming and the User List is a related list.  They are linked by Lookup fields...they are not Master/Detail.

I'm still a little stuck...here's what I have so far....

I added the recordID attribute to my component:
<aura:component controller="User_List" implements="flexipage:availableForAllPageTypes">  
    <!--aura init handler , call js "loadUserList" function on component load, and display contact data on table-->   
    <aura:handler name="init" value="{!this}" action="{!c.loadUserList}"/> 
    
    <!--Declare Attribute for store User Records List-->  
    <aura:attribute name="ListOfUsers" type="User_List__c[]"/> 
    <aura:attribute name="recordId" type="String" />

  <div >   
    <button class="slds-button slds-button--brand slds-button_stretch" onclick="{!c.downloadCsv}">Export User List</button> <br/><br/>

    </div>
</aura:component>
I then added the compenent.get to my controller: 
({
    // ## function call on component load  
    loadUserList: function(component, event, helper){
       helper.onLoad(component, event);
    },
    
    // ## function call on Click on the "Download As CSV" Button. 
    downloadCsv : function(component,event,helper){
        
        var programmingID = component.get("v.recordId");
        
        // get the Records [User_List__c] list from 'ListOfUsers' attribute 
        var stockData = component.get("v.ListOfUsers");
        
        // call the helper function which "return" the CSV data as a String   
        var csv = helper.convertArrayOfObjectsToCSV(component,stockData);   
         if (csv == null){return;} 
        
        // ####--code for create a temp. <a> html tag [link tag] for download the CSV file--####     
	     var hiddenElement = document.createElement('a');
          hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
          hiddenElement.target = '_self'; // 
          hiddenElement.download = 'User_List.csv';  // CSV file Name* you can change it.[only name not .csv] 
          document.body.appendChild(hiddenElement); // Required for FireFox browser
    	  hiddenElement.click(); // using click() js function to download csv file
        }, 
 })

Do I need to add anything to my helper? Like should I also be adding "component.set" for the programming?
 
({
    onLoad: function(component, event) {
        //call apex class method
        var action = component.get('c.fetchList');
        action.setCallback(this, function(response){
            //store state of response
            var state = response.getState();
            if (state === "SUCCESS") {
                //set response value in ListOfUsers attribute on component.
                component.set('v.ListOfUsers', response.getReturnValue());

            }
        });
        $A.enqueueAction(action);
    },

Finally, I'm not sure how to phrase my WHERE statement in my class.  I thought I would say WHERE the lookup fields matches my variable, but I get errors so I'm assuming I misunderstand.  
 
public class User_List {
 
@AuraEnabled
   public static list <User_List__c> fetchList(){
      
      List <User_List__c> returnUserList = new List < User_List__c > ();
        
      for(User_List__c ul: [SELECT 	id, First_Name__c, Last_Name__c, Email__c, Extension_Number__c, Type_of_User_License__c, Phone_Model__c, Language__c, Outbound_Number_Display__c, Outbound_Name_Display__c, Voicemail_Option__c, CFNR_Number__c 
                            From User_List__c 
                            WHERE ul.Programming__c = programmingID
                            LIMIT 1000]) {
             returnUserList.add(ul);
          }
         return returnUserList;
   }
}
Any additional help would be appreciated!!