0

I am using an import CSV functionality using Visual Force Page and Apex through Quick Action Buttons.

I would like to show two messages.

First is INFO Message "CSV Imported, if any errors are present, those rows are not imported. Check them in the error log"

Second is Error Messages

But whenever I run the code, it first shows error messages and below then it shows the INFO message.

How to overcome this.

Below is my Apex Code sample

public class importDataFromCSVController {
    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    public List<Asset__c> assets{get;set;}

    private static final Integer Comments = 25;
    public importDataFromCSVController(){
        csvFileLines = new String[]{};
            }
    List<ApexPages.Message> errormessages=new List<ApexPages.Message>();
    public void importCSVFile(){

        try
        {
            csvAsString = csvFileBody.toString();
            csvAsString = csvAsString.replace('"','');
            csvAsString = String.escapeSingleQuotes(csvAsString);
            csvFileLines = csvAsString.split('\n');
           /*
           Coding part
           */
                }
                catch(Exception e)
                {
                    ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR, 'Import of Record Failed.'); 
                    errormessages.add(errorMessage);               
                }
                ApexPages.Message infoMessage = new ApexPages.Message(ApexPages.severity.INFO,'Assets are Imported into the Application. If any invalid entries are present, they are not imported. Please check them in the error log .');
            ApexPages.addMessage(infoMessage);
            for(ApexPages.Message error:errormessages)
            {
            ApexPages.addMessage(error);    
            }

            }

}

Below is my VisualForce Page Code

<apex:page controller="importDataFromCSVController" showQuickActionVfHeader="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection columns="24">
                  <apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
                  <apex:commandButton value="Import" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
                <apex:pagemessages />
   </apex:form>
</apex:page>

This is the output I am getting, I want to reverse it.

enter image description here

4
  • Have you tried removing the ApexPages.addMessage(errorMessage); inside your catch statement? May 11, 2020 at 9:53
  • ApexPages.addMessage(errorMessage) not there in catch, edited the code part in the question, please check it. Kept in by mistake while trimming the code. But always it first shows error and info message then
    – Aryama I
    May 11, 2020 at 10:00
  • This is nothing to do with your code (in terms of message ordering) but in fact standard behaviour from the platform when you use Severity. Here is the order: Error, Warning, Info, Success, Confirm. You will need custom logic to divide your list of messages and possible some CSS to "clone" the styling of the Visualforce <apex:pagemessages /> i.e to make use of the icons and coloring.
    – TSmith
    May 11, 2020 at 10:26
  • Ok, got it, thank you @TSmith
    – Aryama I
    May 11, 2020 at 10:44

3 Answers 3

2

Salesforce orders the messages based on severity.

A work-around would be to show multiple times the <apex:pageMessages /> and hide the corresponding parts.

If you know that you will only have Info and Error, this could give you an idea

...
    <style>
        .hideError .errorM3 {display: none;}
        .hideInfo .infoM3 {display: none;}
    </style>
...

... 
    <span class="hideError">
        <apex:pagemessages />
    </span>
    <span class="hideInfo">
        <apex:pageMessages />
    </span>
...

As a second work-around, as mentioned by Hemant Jain is to add a fixed info message that you can control from the controller

<apex:pageMessage summary="Assets are Imported into the Application. If any invalid entries are present, they are not imported. Please check them in the error log." severity="info" strength="3" rendered = "{!showInfoMessage}"/>
<apex:pageMessages />

A third work-around is to have a list of String (or wrappers / messages) per severity and iterate through them, something like ...

<apex:repeat value="{!infoMessages}" var="infoMessage">
    <apex:pageMessage summary="{!infoMessage}" severity="info" strength="3" />
</apex:repeat>
<!-- Do the same for the errors with errorMessages -->
2
  • 1
    Thank You Sergio Alcocer, that did the trick
    – Aryama I
    May 11, 2020 at 10:42
  • Keep in mind the other alternatives. May 11, 2020 at 10:56
0

You cannot change the order in which different types of messages are created in a VF page. You will always get messages sorted by severity, so that the most severe, and therefore probably most important, messages are shown first.

The order is

  1. FATAL
  2. ERROR
  3. WARNING
  4. INFO
  5. CONFIRM

You might be able - through CSS magic - to change the order in which the div of each type is presented, but in the DOM, the order is fixed.

1
  • Ok, got it, thank you
    – Aryama I
    May 11, 2020 at 10:43
0

You cannot control the order of messages . The order is defined on the basis of severity.

Controlling this behavior using CSS is one option. Another option you can try is to have a static info message on visualforce page and order the apex:pagemessage and apex:pagemessages tag as per your requirement.

<apex:pageMessage summary="Assets are Imported into the Application. If any invalid entries are present, they are not imported. Please check them in the error log." severity="info" strength="3" rendered = "{!showInfoMessage}"/>
<apex:pageMessages />

Next, in the apex class just update the flag showInfoMessage instead of adding info message, so that information message can be displayed when you want it to show up.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .