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
Todd KadasTodd Kadas 

visualforce page displays correct result but export button generates duplicates

Thanks for taking the time to read this.  I've got an apex wrapper class that compiles a list of opportunities that are pending approval.  I've then created two visualforce pages, one that displays the desired results and includes an export to excel button and the other that creates the excel table.  
In preview mode, the one that creates the excel table generates the desired results in Excel.  However, the one that contains the export button, on click, creates an excel file with each record displayed 3 times.  Here is the apex and the visualforce pages.  Any help would be greatly appreciated.

apex
public class getWrapDataexport{

    public PageReference exportToExcel() {
        PageReference retURLExcel = new PageReference('/apex/wraptestexport');
        return retURLExcel;
    }

 
    List<wrapperclass> wrapList = new List<wrapperclass>();
    public List<wrapperclass> getWrapList() {
 
        for (ProcessInstance pi: [SELECT Id, Status, TargetObjectId, TargetObject.Name, 
                (SELECT Id, ActorId, Actor.Name, OriginalActorId, OriginalActor.Name, StepStatus, Comments, ProcessNode.Name, CreatedDate FROM StepsAndWorkitems WHERE StepStatus =: 'Pending') 
                FROM ProcessInstance WHERE Status =: 'Pending' AND ProcessDefinitionId IN ('04a170000008pXpAAI','04a1J000000GqbkQAC') ORDER BY ProcessDefinitionId] ) {
 
                   for (ProcessInstanceHistory pih : pi.StepsAndWorkItems) {
                       wrapperClass pendingApprovalWrap = new wrapperClass();
                       pendingApprovalWrap.Status = pih.StepStatus;
                       pendingApprovalWrap.RecordName = String.valueOf(pi.TargetObject.Name);
                       pendingApprovalWrap.RecordId = String.valueOf(pi.TargetObjectId);
                       pendingApprovalWrap.RecordObject = String.valueOf(pi.TargetObjectId.getSObjectType()).split('__')[0];
                       pendingApprovalWrap.AssignedToName = String.valueOf(pih.OriginalActor.Name);
                       pendingApprovalWrap.ApproverName = pih.Actor.Name;
                       pendingApprovalWrap.CreatedDate = String.valueOf(pih.CreatedDate);
 
                       wrapList.add(pendingApprovalWrap);
                       }          
                   }
 
                   return wrapList;
    }
 
    public class wrapperClass{
        public String Status {get; set;}
        public String RecordName {get; set;}
        public String RecordId {get; set;}
        public String RecordObject {get; set;}
        public String AssignedToName {get; set;}
        public String ApproverName {get; set;}
        public String CreatedDate {get; set;}

}        
  
    }
visualforce page 1
<apex:page controller="getWrapDataexport" contentType="application/vnd.ms-excel#SalesForceExport.xls" cache="true">
  <head>
   <meta http-equiv="Content-Type" content="text/HTML;charset=UTF-8" />
  </head>
    <apex:pageblock title="Pending Approvals">
        <apex:pageblocktable value="{!wrapList}" var="wrap">
 
            <apex:column >
                <apex:facet name="header">Status</apex:facet>
                <apex:outputtext value="{!wrap.Status}"></apex:outputtext>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Object</apex:facet>
                <apex:outputtext value="{!wrap.RecordObject}"></apex:outputtext>
            </apex:column>
 
            <apex:column headervalue="Name">
                <apex:facet name="header">Opportunity Name</apex:facet>
                <apex:outputlink value="/{!wrap.RecordId}">{!wrap.RecordName}</apex:outputlink>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Assigned To</apex:facet>
                <apex:outputtext value="{!wrap.AssignedToName}"></apex:outputtext>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Approver Name</apex:facet>
                <apex:outputtext value="{!wrap.ApproverName}"></apex:outputtext>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Date</apex:facet>
                <apex:outputtext value="{!wrap.CreatedDate}"></apex:outputtext>
            </apex:column>
 
        </apex:pageblocktable>
    </apex:pageblock>
</apex:page>
visualforce page 2
<apex:page controller="getWrapDataexport" >

    <apex:pageblock title="Pending Approvals">
        <apex:pageblocktable value="{!wrapList}" var="wrap">
 
            <apex:column >
                <apex:facet name="header">Status</apex:facet>
                <apex:outputtext value="{!wrap.Status}"></apex:outputtext>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Object</apex:facet>
                <apex:outputtext value="{!wrap.RecordObject}"></apex:outputtext>
            </apex:column>
 
            <apex:column headervalue="Name">
                <apex:facet name="header">Opportunity Name</apex:facet>
                <apex:outputlink value="/{!wrap.RecordId}">{!wrap.RecordName}</apex:outputlink>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Assigned To</apex:facet>
                <apex:outputtext value="{!wrap.AssignedToName}"></apex:outputtext>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Approver Name</apex:facet>
                <apex:outputtext value="{!wrap.ApproverName}"></apex:outputtext>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">Date</apex:facet>
                <apex:outputtext value="{!wrap.CreatedDate}"></apex:outputtext>
            </apex:column>
        </apex:pageblocktable>
    </apex:pageblock>
    <hr/>
    <apex:form >
    <apex:commandButton action="{!exportToExcel}" value="Export to Excel" id="ExportExcel"/>
    </apex:form>
</apex:page>