0

I have a lightning component which the goal of it is to open a page if some fields on the record are not null, how to compare those fields to null? Here the component:

<aura:component controller="purchaseOrderComponentController"
    implements="force:lightningQuickAction,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="recordId" type="Id" default="a0e2000000X2I9AAAV"/>
    <aura:attribute name="purchaseOrder" type="PBSI__PBSI_Purchase_Order__c"/>   
</aura:component>

Here the JS controller:

({
    doInit : function (component, event) {
        var purchaseOrderId  = component.get("v.recordId");
        var action = component.get("c.getPurchaseOrder");
       // window.open('https://something.visual.force.com/apex/ReceivePO?id='+purchaseOrderId);
        action.setParams({
            "purchaseOrderId": purchaseOrderId
        });
        action.setCallback(this,function (response){

            component.set('v.purchaseOrder',response.getReturnValue());
            if (state === "SUCCESS") {
                // should compare the fields to null, the fields from the apex controller
                // if they are not null then open a page, otherwise show a message.
            } 
        });
        $A.enqueueAction(action);
    }
})

Here the apex controller:

public class purchaseOrderComponentController{
  @AuraEnabled
  public static  PBSI__PBSI_Purchase_Order__c getPurchaseOrder(Id purchaseOrderId) {
    return [SELECT Id, PBSI__Delivery_Location__c, Order_Confirmation_Number__c, Has_Misc_Item__c FROM PBSI__PBSI_Purchase_Order__c where Id=:purchaseOrderId limit 1];
  }
}
1

You forgot to declare the state variable and get it from the response object, which might be your main issue. Adjust your code as follows.


    action.setCallback(this,function (response){
        var state = response.getState(),
            purchaseOrder = response.getReturnValue();
        component.set('v.purchaseOrder',purchaseOrder);
        if (state === "SUCCESS") {
            if(purchaseOrder.Order_Confirmation_Number__c) { // Modify this here //
               // you would redirect here //
            } else {
              // show an error //
            }
        } 
    });

I'll leave you to adjust the appropriate parts from here.

0

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.