I need to know how to be able to display the following warining message in salesforce page:

There is an existing Merchant: corresponding account.Id for this Lead: corresponding objLead.Id_

in order to achieve that I use the following code:

public with sharing class LeadToMerchConvertController {
    Lead objLead;
    public static Account account;
    // String objLeadId;   
    public LeadToMerchConvertController(ApexPages.StandardController controller) {
        objLead = (Lead)controller.getRecord();
        // System.debug('objLead ' + objLead.Id);
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING,'There is an existing Merchant: ' + buildAccountTemplate() +  ' for this Lead: ' + buildLeadTemplate());

public static Account setAccount(Account accountToSet) {
        System.debug('accountToSet ' + accountToSet);
        System.debug('account17 ' + account);
        account = accountToSet;
        System.debug('account171 ' + account);
        return account;


Here is what is displayed in the debug log:

10:39:56.690 (690070892)|USER_DEBUG|[16]|DEBUG|account171 Account:
{Name=emerchantpay,Id=00156000002zGVsAAM, Status__c=Initial comments sent, CurrencyIsoCode=USD}

public String buildLeadTemplate() {     
        String urlAddress;
        if(objLead != null) {
            urlAddress = URL.getSalesforceBaseUrl().toExternalForm() + '/' + objLead.Id;            
        } else {
            urlAddress = 'Lead does not exist';
        return urlAddress;

    public String buildAccountTemplate() {
        String urlAddress;
        System.debug('account ' + account);
        if(account != null) {
            urlAddress = URL.getSalesforceBaseUrl().toExternalForm() + '/' + account.Id;
        } else {
            urlAddress = 'Account does not exist';
        System.debug('urlAddress ' + urlAddress);
        return urlAddress;

The problem is that in the buildAccountTemplate mehtod the account field is initilize with null and the 'Account does not exist' message inside the else is displayed.

If you take a detail look at my code I use the setAccount method in order to initialize the account filed. And the setAccount method is actually invoked from another controller outside the LeadToMerchConvertController.

It is exactly in the other controller where I set the account field. I don't need account field to be initialized in the LeadToMerchConvertController.

The problem is that the account field is actually initilized as you will be able to see it in the log but for some unknown to me reason in method buildAccountTemplate the account field is null.

Could you please advise how can I get the account filed initialized in order to be able to get the account.Id?

  • Here is the code of my visual force page <apex:page standardController="Lead" extensions="LeadToMerchConvertController"> <apex:form > <apex:pageBlock > <apex:pageMessages > </apex:pageMessages> </apex:pageBlock> </apex:form> </apex:page> – Dylan D Nov 30 '15 at 11:56
  • From where you are calling your setAccount method? Your account will always be null. – Santanu Halder Nov 30 '15 at 11:58
  • Okay, I see you mentioned you initialize the account from another controller. See In SFDC, static variables also gets re-initialize when your class gets loaded. So initializing it from different controller wont work. – Santanu Halder Nov 30 '15 at 12:01
  • Hi, thank you for your comment. I woud like to know how to initialize the account filed provided I need my account field to be initialized in another controller. Can you suggest and advise how can I initialize my account field? – Dylan D Nov 30 '15 at 12:31
  • I have added an answer. May be that will help – Santanu Halder Nov 30 '15 at 13:02

Initializing your static variable from another controller will not keep the value intact when current controller loads.

One thing you can do is to pass the accountId value through apex page parameters or in worse case, custom settings.

Suppose your previous page controller was PrevPageController. There in the last method, lets say goToNextPage(),

public PageReference goToNextPage()
PageReference pageRef = Page.MyNextPage;
pageRef.getParameters().put('AccId', accountId);
return pageRef;

Then you fetch the value in your Extension controller

String AccountId = ApexPages.currentPage().getParameters().get('AccId');
  • If you use Custom Settings, two near simultaneous transactions will smash each other. I'd stick with page parameters. – cropredy Nov 30 '15 at 16:58
  • Sorry, I think I am missing something? which 2 simultaneous transactions? – Santanu Halder Dec 1 '15 at 7:04
  • Hi, I can get the account id the way you described 11:32:00.289 (289076493)|USER_DEBUG|[260]|DEBUG|pageRef System.PageReference[/apex/newconvtomerch?AccId=00156000002zGVsAAM] but when I tried to fetch the value by using your code in my Extension controller String AccountId = ApexPages.currentPage().getParameters().get('AccId'); the AccountId is still null. – Dylan D Dec 1 '15 at 9:44
  • 11:37:01.047 (47009523)|USER_DEBUG|[9]|DEBUG|System.PageReference[/apex/newConvToMerch] 11:37:01.047 (47212823)|VARIABLE_ASSIGNMENT|[10]|LeadToMerchConvertController.accountId|null Could you please advise is there a possibility at all to pass and fetch the accoutId value I need? – Dylan D Dec 1 '15 at 9:53
  • Can you update the question with your current code? – Santanu Halder Dec 1 '15 at 12:15

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.