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

Lightning component server-side connection

I am trying to display a message after clicking a button and I want the content of the message to change depending on whether a list, returned from a SOQL query, is empty or not. 

plcTM.cmp
<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="User"/>
    <aura:attribute name="plan" type="jstcl__TG_Commission_Plan__c"/>
    
    <aura:attribute name="doesItPass" type="String" />
    <aura:attribute name="parentId" type="String"/>
    
	<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}" onsubmit="{!c.onsubmittxt}" > 
        <div class="slds-grid slds-gutters slds-p-horizontal_small">
       1.     
            <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>

plcTMController.js
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 action = component.get("c.plcGetCPA");
        action.setParams({"userId" : user,
                          "planId" : plan                         
                         });
       
        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
               component.set("v.doesItPass", response.getReturnValue());
            }
            else {
                console.log("Failed with state: " + state);
             }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
       
        
        var doesItPass = component.get("v.doesItPass");

   //     recUpdate.fire();
        component.set("v.isOpen", !component.get("v.isOpen"));
      if(doesItPass = 'yes'){
          toastEvent.setParams({
            title : 'Success',
              message:'Commission Plan does not match the user.' + doesItPass + user + 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.'+ doesItPass +' '+ user +' '+ plan,
            duration:'5000',
            key: 'info_alt',
            type: 'error',
            mode: 'pester' });
        toastEvent.fire();}   
        
    }
getPTM.apxc
public class getPTM {
@AuraEnabled
    public static String plcGetCPA(string userId, String planId){
        String doesItPass = '';
        List<jstcl__TG_Commission_Plan_Assignment__c> cpaList= new List<jstcl__TG_Commission_Plan_Assignment__c>();
        cpaList = [SELECT Id, jstcl__Commission_Plan__c, jstcl__User__c FROM jstcl__TG_Commission_Plan_Assignment__c WHERE jstcl__User__r.Id = :userId AND jstcl__Commission_Plan__r.Id = :planId];
        if(cpaList != null || cpaList.size() > 0){
            doesItPass = 'yes';
        }
        return doesItPass;
        
    }
}


 
Raj VakatiRaj Vakati
Change your code like below
 
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 action = component.get("c.plcGetCPA");
        action.setParams({"userId" : user,
                          "planId" : plan                         
                         });
       
        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
               component.set("v.doesItPass", response.getReturnValue());
			   var doesItPass = response.getReturnValue() ;
			   if(doesItPass = 'yes'){
          toastEvent.setParams({
            title : 'Success',
              message:'Commission Plan does not match the user.' + doesItPass + user + 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.'+ doesItPass +' '+ user +' '+ plan,
            duration:'5000',
            key: 'info_alt',
            type: 'error',
            mode: 'pester' });
        toastEvent.fire();
		}   
			   
            }
            else {
                console.log("Failed with state: " + state);
             }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
     
        
    }

 
Luke Higgins 22Luke Higgins 22
Hey Raj, 

The code you gave now doesn't display the toast message on button click. 
Raj VakatiRaj Vakati
try this 
<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="User"/>
    <aura:attribute name="plan" type="jstcl__TG_Commission_Plan__c"/>
    
    <aura:attribute name="doesItPass" type="String" />
    <aura:attribute name="parentId" type="String"/>
    
	<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}" onsubmit="{!c.onsubmittxt}" > 
        <div class="slds-grid slds-gutters slds-p-horizontal_small">
       1.     
            <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 type="submit" variant="brand"
			iconName="utility:add" label="Add User" title="Add User" onclick="{! c.greyOut }" /></div>
        </div>
    </lightning:recordEditForm>

</aura:component>
 
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 action = component.get("c.plcGetCPA");
        action.setParams({"userId" : user,
                          "planId" : plan                         
                         });
       
        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
               component.set("v.doesItPass", response.getReturnValue());
            }
            else {
                console.log("Failed with state: " + state);
             }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
       
        
        var doesItPass = component.get("v.doesItPass");

   //     recUpdate.fire();
        component.set("v.isOpen", !component.get("v.isOpen"));
      if(doesItPass = 'yes'){
          toastEvent.setParams({
            title : 'Success',
              message:'Commission Plan does not match the user.' + doesItPass + user + 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.'+ doesItPass +' '+ user +' '+ plan,
            duration:'5000',
            key: 'info_alt',
            type: 'error',
            mode: 'pester' });
        toastEvent.fire();}   
        
    }

 
Luke Higgins 22Luke Higgins 22
Thank you, that worked for the most part. The only thing missing is that my apex class is returning a boolean of false when I want it to return a string of 'yes' or 'no'. The query seems to be pulling the right data but the if statement afterwards must be incorrect.
 
public class getPTM {
@AuraEnabled
    public static String plcGetCPA(string userId, String planId){
        String doesItPass;
        List<jstcl__TG_Commission_Plan_Assignment__c> cpaList= new List<jstcl__TG_Commission_Plan_Assignment__c>();
        cpaList = [SELECT Id, jstcl__Commission_Plan__c, jstcl__User__c FROM jstcl__TG_Commission_Plan_Assignment__c WHERE jstcl__User__r.Id = :userId AND jstcl__Commission_Plan__r.Id = :planId];
        if(!cpaList.isEmpty() == true){
            doesItPass = 'no';
        }else{doesItPass = 'yes';}
        return doesItPass;
}
}

 
Raj VakatiRaj Vakati
I dnt get u ..  below code looks good to me 
 
public class getPTM {
@AuraEnabled
    public static String plcGetCPA(string userId, String planId){
        String doesItPass;
        List<jstcl__TG_Commission_Plan_Assignment__c> cpaList= new List<jstcl__TG_Commission_Plan_Assignment__c>();
        cpaList = [SELECT Id, jstcl__Commission_Plan__c, jstcl__User__c FROM jstcl__TG_Commission_Plan_Assignment__c WHERE jstcl__User__r.Id = :userId AND jstcl__Commission_Plan__r.Id = :planId];
        if(!cpaList.isEmpty() == true){
            doesItPass = 'no';
        }else{doesItPass = 'yes';}
        return doesItPass;
}
}

Or you can try like this 
 
public class getPTM {
@AuraEnabled
    public static boolean  plcGetCPA(string userId, String planId){
        boolean doesItPass;
        List<jstcl__TG_Commission_Plan_Assignment__c> cpaList= new List<jstcl__TG_Commission_Plan_Assignment__c>();
        cpaList = [SELECT Id, jstcl__Commission_Plan__c, jstcl__User__c FROM jstcl__TG_Commission_Plan_Assignment__c WHERE jstcl__User__r.Id = :userId AND jstcl__Commission_Plan__r.Id = :planId];
        if(!cpaList.isEmpty() == true){
            doesItPass = false;
        }else{doesItPass = true;}
        return doesItPass;
}
}

 
Raj VakatiRaj Vakati
working ?
Luke Higgins 22Luke Higgins 22
It is not unfortunately. The doesItPass variable keeps returning the value of false even if I change up the if statement within plcGetCPA