1

I would like to know how to be able to display a warning message in a page.

I have created my own custom visual force page:

<apex:page standardController="Lead" extensions="LeadToMerchConvertController">
  <apex:form >
   <apex:pageBlock >
      <apex:pageMessages >
      </apex:pageMessages>   
    </apex:pageBlock>
  </apex:form>  
</apex:page>

and in the LeadToMerchConvertController I added the following piece of code.

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING,'There is an existing Merchant: ' + buildAccountTemplate() +  ' for this Lead: ' + buildLeadTemplate());
        ApexPages.addMessage(myMsg);

The problem is that the above code is placed inside the constructor:

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());
        ApexPages.addMessage(myMsg);   
    }

and if I move the code outside the constructor the warning message disappear from the page.

What I would like to know is it possible to move and use the code outside the constructor and how to make the warning message visible again when the code is outside the constructor?

I will strongly appreciate if you can also, give additional examples/code of how to display warning message in salesforce.

Regards,

Dilyan

  • You're displaying the message the right way. What seems to be the problem you're having? (Or the behavior you want to see?) – Sebastian Kessel Nov 30 '15 at 8:12
  • Hi, I'll include the entire code in the controller. Basically what I need to do to display the account ID in the warning message. In order to do that I invoke the buildAccountTemplate() method. The porblem is that for some reaseon the account field is not initialize and is null althogh in the setAccount(Account accountToSet) metnod I explicitely initialize the account field and in the debug log System.debug('account171 ' + account); I see that the field is initialized. – Dylan D Nov 30 '15 at 8:54
  • 10:39:56.690 (690070892)|USER_DEBUG|[16]|DEBUG|account171 Account:{Name=emerchantpay, Id=00156000002zGVsAAM, Status__c=Initial comments sent, CurrencyIsoCode=USD} – Dylan D Nov 30 '15 at 8:54
  • Here is the code: – Dylan D Nov 30 '15 at 8:56
  • Hi @DylanD so when Account id is null then you need to so error message? – Ratan Paul Nov 30 '15 at 8:58
3

Apex code executes on the server. By putting the addMessage in the constructor you are ensuring that it is always displayed. In that constructor you could include conditional logic to only display it sometimes.

You could also add a message elsewhere in your controller e.g.

public Contact[] getContacts {
    Contact[] contacts = [select Name fro mContact where ...];
    if (contacts.size() == 0) {
        ApexPages.addMessage(...);  
    }
    return contacts;
}

and it is very common to report errors using it on an action like a save:

public PageReference save() {

    if (...) {
        // Report error
        ApexPages.addMessage(...);
        // Returning null re-displays the same page
        return null;
    }

    return Page.NextPage;

}

You could also add the message later as part of a Visualforce re-render providing you include the id of the apex:pageMessages in the set of page ID values nominated in the re-render.

1

Thank you for your comment, but what I need to know is how to be able to display the following warining message:

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());
        ApexPages.addMessage(myMsg);   
    }

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 initilized 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?

  • this is not an answer and should be moved to be an edit of the OP – cropredy Nov 30 '15 at 17:04

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.