3

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">

    <style>
        ..removed for post
    </style> 

    <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">
                            <div>
                                <h>Transferring Records...</h>
                                <br /><br /><img src="/img/loading32.gif"/>
                                <br /><br /><h>This may take a minute or two...</h>
                            </div>
                        </div>
                    </apex:facet>
                </apex:actionStatus>

                <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>
                <apex:pageBlockSection columns="1" >
                    <apex:commandButton action="{!mergeRecords}" title="Transfer" 
                                        value="Transfer" status="tbloading" reRender="theForm" style="aling:center;"/>               
                </apex:pageBlockSection>
                <apex:pageMessages />
            </apex:pageblock>
        </div>

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

        <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>
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Specialist</apex:facet>
                            <apex:outputField value="{!ES.Specialist__c}" />
                        </apex:column>
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Created Date</apex:facet>
                            <apex:outputField value="{!ES.CreatedDate}" />
                        </apex:column>
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Last Modified Date</apex:facet>
                            <apex:outputField value="{!ES.LastModifiedDate}" />
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
                <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>
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">CreatedDate</apex:facet>
                            <apex:outputField value="{!tsk.CreatedDate}" />
                        </apex:column>
                        <apex:column headerClass="headerClass">
                            <apex:facet name="header">Record ID</apex:facet>
                            <apex:outputField value="{!tsk.Id}" />
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:pageblock>
        </div>
    </apex:form>
</apex:page>
  • 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 Jun 17 '16 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 Jun 17 '16 at 14:32
4

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 />
</apex:outputPanel>

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.

| improve this answer | |

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.