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
Luke Higgins 23Luke Higgins 23 

Passing values from a lightning component to an apex controller

I am trying to pass 2 object ids to an apex controller in order to use for a SOQL query. I then want to pass the list created in the SOQL query back to the javascript controller. There, I want to see if the list is empty or not and fire a message depending on the result.

component:

<aura:component  implements="flexipage:availableForRecordHome,force:hasRecordId,flexipage:availableForAllPageTypes,force:lightningQuickAction,force:appHostable" controller="getPTM" access="global">
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="plc" type="ts2__Placement__c"/>
    <aura:attribute name="user" type="String"/>
    <aura:attribute name="plan" type="String"/>
     <aura:attribute name="cpaList" type="jstcl__TG_Commission_Plan_Assignment__c[]"/>
    <aura:attribute name="parentId" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"></aura:handler>
	<aura:handler event="c:selectedsObjectRecordsEvent" action="{!c.submitIt}" />
    <lightning:recordEditForm aura:id="editfrm" recordId="{!v.recordId}" objectApiName="jstcl__PlacementTeamMember__c" onload="{!c.onloadrec}" onsuccess="{!c.successsave}"  > 
        <div class="slds-grid slds-gutters slds-p-horizontal_small">  
            <div class="slds-p-around_xx-small slds-size_2-of-8">   <lightning:inputField aura:id="plc" fieldName="jstcl__Placement__c" value="{!v.parentId}"/></div>
            <div class="slds-p-around_xx-small slds-size_2-of-8">   <lightning:inputField aura:id="user" fieldName="jstcl__User__c" value="{!v.user}"/></div>
            <div class="slds-p-around_xx-small slds-size_2-of-8">   <lightning:inputField aura:id="plan" fieldName="jstcl__CommissionPlan__c" value="{!v.plan}"/></div>
            <div class="slds-p-around_xx-small slds-size_1-of-8">   <lightning:inputField aura:id="split" fieldName="jstcl__SplitPercent__c"/></div>
            <div class="slds-p-around_xx-small slds-size_1-of-8 slds-m-top_large">   <lightning:button variant="brand"
			iconName="utility:add" label="Add User" title="Add User" onclick="{! c.greyOut }" /></div>
        </div>
    </lightning:recordEditForm>
</aura:component>
Component Controller
doInit: function(component, event, helper) {
        //Create the action
       var action = component.get("c.plcGetCPA");
       var user = component.get("v.user");
        var plan = component.get("v.plan");
        action.setParams({
            "user" : user,
            "plan" : plan
        });
        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
               component.set("v.cpaList", response.getReturnValue());
            }
            else { console.log("Failed with state: " + state); }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
    },
    greyOut : function(component , event , helper){
        var toastEvent = $A.get("e.force:showToast");
        var user = component.get("v.user");
        var plan = component.get("v.plan");
        var cpaList = component.get("v.cpaList");
		var cpaLength = cpaList.length;
        component.set("v.isOpen", !component.get("v.isOpen"));
        
        if(cpaLength > 0){
        toastEvent.setParams({
            title : 'Success',
            message:'Commission Plan matches.' + cpaList +" -------- "+ plan,
            duration:'5000',
            key: 'info_alt',
            type: 'error',
            mode: 'pester' });
        toastEvent.fire();}   
     else { toastEvent.setParams({
            title : 'Error',
            message:'Commission Plan does not match the user.' + user +' '+ plan,
            duration:'5000',
            key: 'info_alt',
            type: 'error',
            mode: 'pester' });
        toastEvent.fire();}    
    }

Apex Controller:
public class getPTM {
AuraEnabled
    public static List<jstcl__TG_Commission_Plan_Assignment__c> plcGetCPA(List<Id> userId, List<Id> planId){
       
        List<jstcl__TG_Commission_Plan_Assignment__c> cpaList= new List<jstcl__TG_Commission_Plan_Assignment__c>();
        cpaList = [SELECT Id
                   FROM jstcl__TG_Commission_Plan_Assignment__c 
                   WHERE jstcl__User__r.Id IN :userId AND jstcl__Commission_Plan__r.Id IN :planId];
       
    return cpaList;
        
    }

}

 
Govind TalariGovind Talari

Hi Liuke,
It seems like your not passing the parameters correctly in apex you are expecting for List<Id> userId, List<Id> planId , how ever in component you have created two attributes 
   <aura:attribute name="user" type="String"/>
   <aura:attribute name="plan" type="String"/>

try to change the code as below and the parmeters names should be same in the apex method and in lightning component setparams

User-added image

Let me know if it helps.

Cheers,
Akshay

 

Luke Higgins 23Luke Higgins 23
Hi,

This didn't work unfortunately. I think the 'userId' and the 'planId' are still not being properly passed into the apex method. The values it returns for the two lookup fields are either null or blank which makes the query return blank as well. Any ideas?

Thank you!
Govind TalariGovind Talari

Hi,
I see that you are not assigning the values of user and plan in lightning component controller. 


var user = component.get("v.user"); // its empty 
var plan = component.get("v.plan"); // its empty 
var plan = component.get("v.plan"); action.setParams({ "user" : user, "plan" : plan });

Try to implement
for example if you set an attribute in component

<aura:attribute name="user" type="String" default="005SomeUserId"/>

and in class try to verify 

public static List<jstcl__TG_Commission_Plan_Assignment__c> plcGetCPA(String userId, String planId){
system.debug('userId : '+ userId);    //005SomeUserId this value wil get printed
List<jstcl__TG_Commission_Plan_Assignment__c> cpaList= new List<jstcl__TG_Commission_Plan_Assignment__c>();
cpaList = [SELECT Id FROM jstcl__TG_Commission_Plan_Assignment__c WHERE jstcl__User__r.Id IN :userId AND jstcl__Commission_Plan__r.Id IN :planId];
return cpaList;
}

 

Hope this helps!

Ajay K DubediAjay K Dubedi
Hi Luke,
I have gone through your code everything seems to alright, To get what you need follow these steps:
1. In your controller, line number 14 assign response.getReturnValue(); to a list variable or array.
2. Check length of array if it is zero alert an error message else success message.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi