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
Robert Davis 16Robert Davis 16 

Passing Value to Apex to Javascript Helper

I have a Lightning Component that has a button that I would like to click and have it open an external URL with values from the fields from the current Opportunity Record.

I can not figure out how to get the recordId from component to Apex Controller and then the fields back to the Javascript Helper to form the URL.

Javascript Component
<aura:component implements="force:lightningQuickAction,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global" 
                controller="CAFController">
    
    <aura:attribute name="recordId" type="Id" />
    
    <div id="aura-page">
        <div class="container">
			<ui:button class="btn" label="CAF Form" press="{!c.navigate}"/> 
        </div>
    </div>
</aura:component>
Javascript Controller
({
	navigate : function(component, event, helper) {
		helper.navigate(component);
	}
})
Javascript Helper
({
	navigate : function(component) {
        var action = component.get("c.getOpportunity");
        action.setParams({opportunityId: component.get("v.recordId")});
        var urlEvent=$A.get("e.force:navigateToURL");
        var partOneURL = 'https://XXX21.springcm.com/atlas/Forms/UpdateFormDoc.aspx?aid=9124&FormUid=8beXXXXX-d9b0-e611-bb8d-6c3be5XXXXXXX';
        urlEvent.setParams({"url": partOneURL})
        urlEvent.fire();
	}
})
Apex Controller
public class CAFController {

    @AuraEnabled
    public static Opportunity getOpportunity(ID opportunityId){
        if(opportunityId == null) {
            return null;
        }
        List<Opportunity> opps = [SELECT id, 
                                         Opportunity.FTEs__c,
                                         Opportunity.Account.Industry ,
                                         Opportunity.TotalNumberofSeats__c ,
                                         Opportunity.CAPEX_Required__c ,
                                         Opportunity.Amount ,
                                         Opportunity.Annualized_Revenue__c,
                                         Opportunity.ProgramStartDate__c ,
                                         Opportunity.Name
                                  FROM Opportunity
                                  WHERE Id =: opportunityId];
        if(opps.size()>0){
            return opps[0];
        }
        return null;
    }
}

I would appreciate any assistance. Thank you in advance. 

Robert


 
Best Answer chosen by Robert Davis 16
Nayana KNayana K
({
	navigate : function(component) {
        var action = component.get("c.getOpportunity");
		var idOpp = component.get("v.recordId");
		if($A.util.isEmpty(idOpp))
		{
			action.setParams({opportunityId: idOpp});
		
			// Create a callback that is executed after 
			// the server-side action returns
			action.setCallback(this, function(response) {
				var state = response.getState();
				if (state === "SUCCESS") {
					var opp = response.getReturnValue();
					/* Here you can access like opp.Id, opp.FTEs__c, opp.Account.Industry and form the URL */
					var urlEvent=$A.get("e.force:navigateToURL");
					var partOneURL = 'https://XXX21.springcm.com/atlas/Forms/UpdateFormDoc.aspx?aid=9124&FormUid=8beXXXXX-d9b0-e611-bb8d-6c3be5XXXXXXX';
					urlEvent.setParams({"url": partOneURL})
					urlEvent.fire();
				}
				else if (state === "ERROR") {
					var errors = response.getError();
					if (errors) {
						if (errors[0] && errors[0].message) {
							console.log("Error message: " + 
									 errors[0].message);
						}
					} else {
						console.log("Unknown error");
					}
				}
			});

			// optionally set storable, abortable, background flag here

			// A client-side action could cause multiple events, 
			// which could trigger other events and 
			// other server-side action calls.
			// $A.enqueueAction adds the server-side action to the queue.
			$A.enqueueAction(action);
		}
		else
		{
			console.log('===Record Id is not present====');
		}
	}
})
 
public class CAFController {

    @AuraEnabled
    public static Opportunity getOpportunity(ID opportunityId){
        
        List<Opportunity> opps = [SELECT Id, 
                                         FTEs__c,
                                         Account.Industry ,
                                         TotalNumberofSeats__c ,
                                         CAPEX_Required__c ,
                                         Amount ,
                                         Annualized_Revenue__c,
                                         ProgramStartDate__c ,
                                         Name
                                  FROM Opportunity
                                  WHERE Id =: opportunityId];
        if(!opps.isEmpty())
		{
            return opps[0];
        }
        return null;
    }
}

 

All Answers

Nayana KNayana K
({
	navigate : function(component) {
        var action = component.get("c.getOpportunity");
		var idOpp = component.get("v.recordId");
		if($A.util.isEmpty(idOpp))
		{
			action.setParams({opportunityId: idOpp});
		
			// Create a callback that is executed after 
			// the server-side action returns
			action.setCallback(this, function(response) {
				var state = response.getState();
				if (state === "SUCCESS") {
					var opp = response.getReturnValue();
					/* Here you can access like opp.Id, opp.FTEs__c, opp.Account.Industry and form the URL */
					var urlEvent=$A.get("e.force:navigateToURL");
					var partOneURL = 'https://XXX21.springcm.com/atlas/Forms/UpdateFormDoc.aspx?aid=9124&FormUid=8beXXXXX-d9b0-e611-bb8d-6c3be5XXXXXXX';
					urlEvent.setParams({"url": partOneURL})
					urlEvent.fire();
				}
				else if (state === "ERROR") {
					var errors = response.getError();
					if (errors) {
						if (errors[0] && errors[0].message) {
							console.log("Error message: " + 
									 errors[0].message);
						}
					} else {
						console.log("Unknown error");
					}
				}
			});

			// optionally set storable, abortable, background flag here

			// A client-side action could cause multiple events, 
			// which could trigger other events and 
			// other server-side action calls.
			// $A.enqueueAction adds the server-side action to the queue.
			$A.enqueueAction(action);
		}
		else
		{
			console.log('===Record Id is not present====');
		}
	}
})
 
public class CAFController {

    @AuraEnabled
    public static Opportunity getOpportunity(ID opportunityId){
        
        List<Opportunity> opps = [SELECT Id, 
                                         FTEs__c,
                                         Account.Industry ,
                                         TotalNumberofSeats__c ,
                                         CAPEX_Required__c ,
                                         Amount ,
                                         Annualized_Revenue__c,
                                         ProgramStartDate__c ,
                                         Name
                                  FROM Opportunity
                                  WHERE Id =: opportunityId];
        if(!opps.isEmpty())
		{
            return opps[0];
        }
        return null;
    }
}

 
This was selected as the best answer
Robert Davis 16Robert Davis 16
Nayana  - THANK YOU - I will start to work on completing this. You are very impressive and kind.
Nayana KNayana K
My bad, 
if($A.util.isEmpty(idOpp)) should be if(!$A.util.isEmpty(idOpp)). I mean if not empty then apex action should be called.
Nayana KNayana K
Welcome :)