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.
ApexPages.addMessage(errorMessage);
inside your catch statement?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.