This is the first time I am using the database.saveresult feature. How do I pass the result values back to the VF page? Do I need to create a new public list and then add each record from the FOR loop into the list or is there an easier / more direct way?

    Database.SaveResult[] srTask    = Database.update(eTasks, false);

for (Database.SaveResult sr : srTask) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully transfered tasks. Task ID: ' + sr.getId());
        showRecords = FALSE;
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Task fields that affected this error: ' + err.getFields());

Visualforce Page:

I want to put a table inside the <div id="message"> block that shows all the save results.

<apex:page controller="EmpMerge_Controller">

        ..removed for post

    <apex:form id="theForm">
        <div id="header"> 

            <apex:pageblock tabStyle="Employee_Session__c" title="Transfer Details" rendered="{!showRecords}">
                <apex:actionStatus id="tbloading">
                    <apex:facet name="start">
                        <div class="overlay">
                                <h>Transferring Records...</h>
                                <br /><br /><img src="/img/loading32.gif"/>
                                <br /><br /><h>This may take a minute or two...</h>

                <apex:pageBlockSection columns="2" id="controls" >
                    <apex:outputLabel value="Current HBS ID: {!empName}" style="font-weight:bold;"/>
                    <apex:inputField label="New HBS ID:" id="pickerHBSID" value="{!empSession.Employee__c}" required="true" />
                <apex:pageBlockSection columns="1" >
                    <apex:commandButton action="{!mergeRecords}" title="Transfer" 
                                        value="Transfer" status="tbloading" reRender="theForm" style="aling:center;"/>               
                <apex:pageMessages />

        <div id="message">
            <apex:pageBlock tabStyle="Employee_Session__c" title="Transfer Result" id="alerts" rendered="{!!showRecords}">
                <apex:outputLabel style="font-weight:bold;color:green;">

        <div id="section">
            <apex:pageblock tabStyle="Employee_Session__c" title="Records for Transfer" id="results" rendered="{!showRecords}">
                <apex:pageBlockSection title="Sessions" collapsible="false" columns="1">
                    <apex:pageBlockTable value="{!EmpSessions}" var="ES" id="Detail" >
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Session Name</apex:facet>
                            <apex:outputField value="{!ES.Name}" />
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Specialist</apex:facet>
                            <apex:outputField value="{!ES.Specialist__c}" />
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Created Date</apex:facet>
                            <apex:outputField value="{!ES.CreatedDate}" />
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Last Modified Date</apex:facet>
                            <apex:outputField value="{!ES.LastModifiedDate}" />
                <apex:pageBlockSection title="Tasks" collapsible="false" columns="1"> 
                    <apex:pageBlockTable value="{!Tasks}" var="tsk">
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Subject</apex:facet>
                            <apex:outputField value="{!tsk.Subject}" />
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">CreatedDate</apex:facet>
                            <apex:outputField value="{!tsk.CreatedDate}" />
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Record ID</apex:facet>
                            <apex:outputField value="{!tsk.Id}" />
  • Can you show us your existing visualforce page to see how you're collecting the records you're saving? Do you want to display them on the same page in an output section? I suppose the answer depends on what you want to do with any error messages. You don't really have to create a new list unless you have errors you want to display separately.
    – crmprogdev
    Commented Jun 17, 2016 at 14:17
  • I have added my page code to post. I want the save results in a table inside the div id="message" block.
    – Matt M
    Commented Jun 17, 2016 at 14:32

1 Answer 1


Adding the component Apex:pageMessages to your visualforce page, and using ApexPages.addMessage(...) should display the result on page after a rerender.

<apex:outputPanel id="pageMessages">
    <apex:pageMessages />

You can add each result by creating a ApexPages.Message with a different severity.

A failure case:

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, err.getStatusCode() + ': ' + err.getMessage()));

A success case:

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Successfully transferred tasks. Task ID: ' + sr.getId()));

If you're using a apex:commandButton, or some other method of form submission, make sure to specify the reRender.

<apex:commandButton ... reRender="pageMessages" />

You can also use the addMessages method to add messages based on a thrown exception. The ApexPages holds all the methods you should need for this.

You must log in to answer this question.

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