In this blog I will share the complete code of a workaround through which you will be able to get API parameters in Lightning Component.
It's just 3 simple steps :
1. Create Visualforce Page and print API parameters with some prefix like start and end text in it.
2. Create a Utility class to get the text from VF page and separate the value based on the start and end prefix.
3. Create the lightning component and get the API value by passing the VF page name in utility class.
Visualforce Page
1 2 3 4 | <apex:page > Start_of_SessionId{!$Api.Session_ID}End_of_SessionId Start_of_URL{!$Api.Partner_Server_URL_160}End_of_URL </apex:page> |
Lightning Component
1 2 3 4 5 6 7 | <aura:component controller="SendAPIParametersCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" > <aura:attribute name="sessionId" type="String" /> <aura:attribute name="partnerUrl" type="String" /> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> Session ID : {!v.sessionId} URL : {!v.partnerUrl} </aura:component> |
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ({ doInit : function(component, event, helper) { console.log('###I am in component'); var action = component.get("c.fetchAPIVar"); action.setCallback(this,function(response){ var state = response.getState(); if(state == "SUCCESS"){ var result=response.getReturnValue(); component.set("v.sessionId",result[0]); component.set("v.partnerUrl",result[1]); console.log('###SessionID : '+result[0]); console.log('###URL : '+result[1]); } }); $A.enqueueAction(action); } }) |
Apex Controller
1 2 3 4 5 6 7 8 | public class SendAPIParametersCtrl { @AuraEnabled public static List<String> fetchAPIVar(){ PageReference apiPage = Page.SendAPIParametersVF; List<String> apiList = Utility.getAPIVarFromVF(apiPage); return apiList; } } |
Utility Class
1 2 3 4 5 6 7 8 9 10 11 12 13 | global class Utility { global static List<String> getAPIVarFromVF(PageReference vfPage){ List<String> apiList = new List<String>(); String content=vfPage.getContent().toString(); Integer s=content.indexOf('Start_of_SessionId')+'Start_of_SessionId'.length(), e=content.indexOf('End_of_SessionId'); apiList.add(content.substring(s,e)); s=content.indexOf('Start_of_URL')+'Start_of_URL'.length(); e=content.indexOf('End_of_URL'); apiList.add(content.substring(s,e)); return apiList; } } |
Checkout complete tutorial below :
If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !
Thanks
Keep Coding
6 Comments
Used the above code, and calling a lightning comp on button click and I have to fetch the session id. However getting error as Ending position out of bounds: -1
ReplyDeletePlease advise how I can resolve.
Hi Manpreet, have you copied complete code ? The reason behind this issue could be that the string is not available at the position you are trying to find it.
DeleteHi Kapil,
DeleteI have copied the same exact code and I am still getting the error
Manpreet,
DeleteCould you please put some console log in your code to debug it and let me know on which line you are getting the error ?
Bascially I am getting exception from apex-
DeleteSystem.StringException: Ending position out of bounds: -1
Could you please share your utility class and vf page code here so I could check.
Delete