0

I wrote code for an Aura Redirect Component but I'm having trouble getting it to redirect only if the CustomerId is not null. Here's my code.

Apex Controller

public with sharing class NetsuiteQuote {
    @AuraEnabled
    public static string getOpportunityUrl(String id){
        Opportunity opp = [SELECT Netsuite_Customer_ID__c, Netsuite_Employee_ID__c, X18_Character_Opp_ID__c, Netsuite_Opportunity_Stage__c, CloseDate   
                           FROM Opportunity WHERE Id=:id];
        if(opp.Netsuite_Customer_ID__c == null){
            opp.addError('Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.');
            //string error= 'Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.';
            return  'Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.';
        }

        else if(opp.Netsuite_Customer_ID__c != null){
            string NSCustID= opp.Netsuite_Customer_ID__c ;
            string NSsalesRep= opp.Netsuite_Employee_ID__c;
            string SFOpportunity= opp.X18_Character_Opp_ID__c;
            decimal NSOpportunityStatus= opp.Netsuite_Opportunity_Stage__c;
            Date SFcloseDate= opp.CloseDate;

            string sURL='https://system.netsuite.com/app/site/hosting/scriptlet.nl';
            sURL +='?script=528&deploy=1&compid=3962188_SB2';
            sURL += '&action=new';
            sURL += '&customer=' + NSCustID;
            sURL += '&salesRep=' + NSsalesRep;
            sURL += '&sfdcOpportunity=' + SFOpportunity;
            sURL += '&sfdcOpportunityStatus=' + NSOpportunityStatus;
            sURL += '&closeDate=' + SFcloseDate;
            //alert('Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.');        
            return sURL;
        }
        //acc.BillingStreet = acc.BillingStreet.replace(' ', '+');
        else return 'Opportunity not found.';
    }
}

Aura Component

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" controller="NetsuiteQuote" >
    <aura:attribute name="recordId" type="String" />
    <aura:attribute name="opp" type="Opportunity" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:if isTrue="{!v.opp== 'Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.'}">
        <p><lightning:formattedText value="{!v.opp}"/></p>
    </aura:if>
</aura:component>

Component Controller

({
    doInit : function(component, event, helper) {
        var recordId = component.get("v.recordId");
        var action = component.get("c.getOpportunityUrl");
        action.setParams({
            "id" : recordId
        });
        action.setCallback(this, function(response) {
            var state = response.getState(); 
            if(state == "SUCCESS" && component.isValid()){
                console.log("success") ;
                
                var result = response.getReturnValue();
                component.set("v.opp", result);

                console.log(result);
                if(result == 'Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.'){
                   console.error = 'Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.'; 
                }
                else{
                    var urlRedirect = result;
                    var urlEvent = $A.get("e.force:navigateToURL");
                    urlEvent.setParams({
                        "url": urlRedirect
                    });
                    urlEvent.fire();
                    
                    // Close the action panel
                    var dismissActionPanel = $A.get("e.force:closeQuickAction");
                    dismissActionPanel.fire();
                }
            }else{
                console.error("fail:" + response.getError()[0].message); 
            }
        });
        $A.enqueueAction(action);
    }
})
0

2 Answers 2

0

You are returning String from your apex method so you should check like this in Aura Controller

if(result == 'Customer is not synchronized with NetSuite yet. Please try again in 5 minutes.')

Also When checking for Empty in Apex you can do the following so that it can check for all possible scenario of field being empty

String.isBlank(opp.Netsuite_Customer_ID__c);

0
0

I had to change from if(opp.Netsuite_Customer_ID__c ==' ') to if(opp.Netsuite_Customer_ID__c == null) and I had to modify my component to display the error and the issue was fixed. Thanks for your help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .