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
Dilyan DimitrovDilyan Dimitrov 

Display warning message

Hello,


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:





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
ManojjenaManojjena
Hi Dilyan,
You can add belwo code in your page ,if you want any condition you can modify teh if condition accordingly .
Also modify error message accordigly as per your requirment .
<apex:panelGroup rendered="{!IF(Profile.Name,'System Administrator',TRUE,FALSE)}">
      <apex:pageMessage id="msg" escape="0" summary="Error message needs to add " severity="warning" strength="3"/>
  </apex:panelGroup>
Let us know if it hels !!
Thanks
Manoj


 
Dilyan DimitrovDilyan Dimitrov

Hi,

Thank you for your comment. 

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>
 

ManojjenaManojjena
Hi Dilyon ,
Try with belwo code !!
<apex:page standardController="Lead" extensions="LeadToMerchConvertController"> 
	  <apex:form >
		  <apex:pageBlock > 
			  <apex:pageMessage id="msg" escape="0" summary="Please do what u want to do" severity="warning" strength="3"/>
		  </apex:pageBlock> 
	  </apex:form> 
  </apex:page>


 
Dilyan DimitrovDilyan Dimitrov

Hi,
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() {     
        // Example: https://cs42.salesforce.com/a0r5600000001tu
        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?