You need to sign in to do that
Don't have an account?
Luke 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
plcTMController.js
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; } }
The code you gave now doesn't display the toast message on button click.
Or you can try like this