Question
I have an Apex class (MyCTIExtensions) that retrieves a phone number and stores it in a static variable (phoneNumber). When certain conditions are met (cases.size() > 1), the class calls a Visualforce page. The Visualforce page’s controller (FlowRedirectController) is supposed to retrieve this phoneNumber and use it to construct a URL for a Flow. However, the phone variable in the second class is not receiving any value.
Why is this happening, and how can I correctly pass the phone number between these two classes?
Answer
The issue occurs because Apex is stateless between transactions. Creating a Visualforce page via a URL and subsequently a Flow via another URL results in a new transaction, meaning that static variables do not persist across requests.
To correctly pass the phoneNumber from MyCTIExtensions to FlowRedirectController, we need to pass it explicitly via the URL instead of relying on a static variable.
Step 1: Modify MyCTIExtensions to Pass the Phone Number in the URL
Instead of storing the phone number in a static variable, append it as a URL parameter when redirecting to the Visualforce page:
if (cases.size() > 1) {
dataToReturn.put('url', '/apex/RedirectToFlow?phone=' + EncodingUtil.urlEncode(phoneNumber, 'UTF-8'));
return JSON.serialize(dataToReturn);
}Step 2: Update FlowRedirectController to Retrieve the Phone Number from the URL
Since the phone number is now passed in the URL, retrieve it using ApexPages.currentPage().getParameters() instead of the static variable:
public class FlowRedirectController {
public PageReference redirectToFlow() {
// Retrieve phone number from URL parameter
String phone = ApexPages.currentPage().getParameters().get('phone');
// Construct Flow arguments
String flowArgs = '[{"name":"Phone","type":"String","value":"' + phone + '"}]';
String encodedFlowArgs = EncodingUtil.urlEncode(flowArgs, 'UTF-8');
// Construct the Flow URL
String flowUrl = '/lightning/page/recordActionFlow?lightning__flowDevName=CTI_ScreenFlow_GestionDeContactosMultiples&lightning__flowArgs=' + encodedFlowArgs;
PageReference pageRef = new PageReference(flowUrl);
pageRef.setRedirect(true);
return pageRef;
}
}Explanation:
- Why was the phone number lost?
Since each HTTP request in Salesforce creates a new transaction, any static variables (MyCTIExtensions.phoneNumber) do not persist across requests.
The Visualforce page and the Flow are being opened via URLs, meaning a separate execution context is created, losing the stored value. - How does passing the value in the URL fix the issue?
Instead of relying on a static variable (which does not persist across requests), we explicitly pass the phone number as a URL parameter (?phone=...).
The Visualforce page retrieves the phone number from the query parameters (ApexPages.currentPage().getParameters().get('phone')). - What is the expected outcome?
Now, the phone number is properly passed fromMyCTIExtensions→FlowRedirectController→ Flow, ensuring that data persists across the different transactions.
This approach guarantees that the necessary data is available throughout the process, solving the issue of the lost phoneNumber variable.
Real-Time Project-Based Salesforce Course to Kick Start Your Career
Our Salesforce course is carefully structured to give you a strong grasp of the Salesforce platform, equipping you with the essential skills needed to succeed in the CRM industry. Covering key modules like Salesforce Admin, Developer, and AI, the program blends theoretical learning with practical application. Through real-world projects and hands-on exercises, you’ll develop the expertise to tackle complex business challenges using Salesforce solutions. Our skilled instructors ensure you gain both technical proficiency and industry knowledge to excel in the Salesforce ecosystem.
In addition to technical training, our Top 10 Salesforce training institutes in Hyderabad provides personalized mentorship, certification support, and interview preparation to boost your career opportunities. You’ll have access to valuable study materials, practical project experience, and ongoing guidance throughout your learning journey. By the end of the course, you’ll be fully prepared for certification exams and equipped with the real-world problem-solving skills that employers seek. Take the next step in your Salesforce career—join a Free Demo today!



Leave a Reply
You must be logged in to post a comment.