1

I am trying to build one custom visualforce page from where I can Undelete Note and Attachments Both at the same time.

0

I have done the something like this in my dev Org and it working fine :

please find the below code :

Apex Class : -

public class UndeleteRecsController
{

    Public List<Note> noteList{get;set;}//List of Notes
    Public List<Attachment> attachList{get;set;}//List of Attachment
    Public List<wrapNote> wrapperlist {get;set;} // wrapper list for note to show this in the page
    Public List<wrapAttach> wrapperlistattach {get;set;}// wrapper list for Attachments to show this in the page
    public string pId;// Store the parent Id
    public String selected_option{get;set;} // store what is selected from page
    public Boolean enableNote{get;set;}//To enable section for Note
    public Boolean enableAttach{get;set;}//To enable section for Attachment

    //Controller to initialize all the List 
    public UndeleteRecsController(ApexPages.StandardController controller) {
        pId = ApexPages.currentPage().getParameters().get('Pid');
        noteList = New List<Note>();
        attachList = New List<Attachment>();
        wrapperlist  = new List<wrapNote>();   
        wrapperlistattach = new List<wrapAttach>();  

    }   

    Public UndeleteRecsController()    {

    }

    //to Show options in the Page
    public  List<SelectOption> getSearchOption()
    {        
        List<SelectOption> filter_search= new List<SelectOption>(); 
        filter_search.add(new SelectOption('Notes','Notes'));
        filter_search.add(new SelectOption('Attachments','Attachments'));        
        return filter_search; 
    } 

    //Populate the section based on the selection
    public void populate()
    {
        if(selected_option =='Notes')
        {
           System.debug('----name--------');
           enableNote = true ; 
           enableAttach = false; 
           populaterecord();           
        }    
        else if(selected_option =='Attachments')
        {
           enableNote = false; 
           enableAttach = true;  
           populaterecord();        
        }       
    }

    //populate list for both note and attachments
    Public void populaterecord()
    {
        if(selected_option =='Notes')
        {
            wrapperlist  = new List<wrapNote>();
            //Query records for Notes based on the parent Id
            noteList = [select id,Title,Body from Note where isdeleted =: true and ParentId =:pId ALL rows];
            if(noteList.size()>0)
            {
                for(Note no : noteList)
                {
                    wrapperlist.add(new wrapNote(no,false));
                }
            }
        }
        if(selected_option =='Attachments')
        {
            wrapperlistattach = new List<wrapAttach>();  
            //Query records for Attachment based on the parent Id
            attachList = [select Id,ParentId,Name,ContentType  from Attachment where isdeleted =: true and ParentId =:pId ALL rows];
            if(attachList.size()>0)
            {
                for(Attachment no : attachList)
                {
                    wrapperlistattach.add(new wrapAttach(no,false));
                }
            }            
        }

    }

    //after Selecting records un-delete functionality 
    ublic void UndeleteRecs()
    {
        if(selected_option =='Notes')
        {
            List<Note> lstselectedNote = new List<Note>();          
            for(wrapNote wrapno : wrapperlist)
             {
                 if(wrapno.selectedrecord== true)
                 {
                    //Adding Selected note which needs to be undelete
                     lstselectedNote.add(wrapno.nt);
                 }
             }
             if(lstselectedNote.size()>0)
             {
                 //Un_delete selected Attachment
                 undelete lstselectedNote ;      
                 populaterecord();        
             }
        }
        if(selected_option =='Attachments')
        {
             List<Attachment> lstselectedAttach = new List<Attachment>();  
             for(wrapAttach wrapno : wrapperlistattach)
             {
                 if(wrapno.selectedrecord== true)
                 {
                     //Adding Selected Attachment which needs to be undelete
                     lstselectedAttach.add(wrapno.att);
                 }
             }
             if(lstselectedAttach.size()>0)
             {
                 //Un_delete selected Attachment
                 undelete lstselectedAttach;      
                 populaterecord();        
             }

        }
    }

    ////preparing wrapper class for Notes
    public class wrapNote
    {
        public Note nt{get; set;}
        public Boolean selectedrecord{get; set;}      

        public wrapNote(Note n,Boolean isselecteduser)
        {               
            nt = n;
            selectedrecord = isselecteduser;
        }    
    }

    //preparing wrapper class for attachment
    public class wrapAttach
    {
        public Attachment att{get; set;}
        public Boolean selectedrecord{get; set;}      

        public wrapAttach(Attachment t,Boolean isselecteduser)
        {               
            att = t;
            selectedrecord = isselecteduser;
        }    
    }

}

Visulforce Page : -

<apex:page controller="UndeleteRecsController">

<c:Overlay style="processing"/>
<apex:form id="frm">
<script>
    function reloadPage()
    {
      var idd = "{!$CurrentPage.parameters.id}";
      window.top.location='/'+idd;
    }
</script>

    <div id="overlay"></div> 

    <apex:pageBlock id="pgdblk"> 
      <apex:actionStatus id="searchStatus"  onstart="showOverlay();" onstop="hideOverlay();"/>
      <apex:pageBlockSection collapsible="true" columns="1" title="Search Criteria" id="pgsecserch"> 
            <apex:pageBlockSectionItem id="id_sec2">
                <apex:selectRadio value="{!selected_option}" >
                    <apex:selectOptions value="{!SearchOption}"></apex:selectOptions> 
                     <apex:actionSupport event="onclick" action="{!populate}" reRender="pgdblk,avai_per,avai_att" status="searchStatus"/>
                </apex:selectRadio>
            </apex:pageBlockSectionItem>            
        </apex:pageBlockSection>
       <apex:pageBlockSection title="Available Note" id="avai_per" columns="1"  rendered="{!enableNote}">
            <apex:outputPanel id="id_available">
                <apex:pageBlockTable id="asslpgd" value="{!wrapperlist}" var="cus">
                  <apex:column headerValue="Select" style="width:40px">                                                  
                       <apex:inputCheckbox id="selectcheckbox" value="{!cus.selectedrecord}"/>                    
                  </apex:column>                       
                  <apex:column headerValue="Title">                       
                       <apex:outputText value="{!cus.nt.Title}"/>
                  </apex:column>
                  <apex:column headerValue="Body">                       
                       <apex:outputText value="{!cus.nt.Body}"/>
                  </apex:column>                      
               </apex:pageBlockTable>
            </apex:outputPanel>           
            <center><apex:commandButton value="Undelete" action="{!UndeleteRecs}" reRender="pgdblk,avai_per,asslpgd" status="searchStatus" onclick="reloadPage()"/></center>                                               
        </apex:pageBlockSection> 

        <apex:pageBlockSection title="Available Attachment" id="avai_att" columns="1" rendered="{!enableAttach}">
            <apex:outputPanel id="id_att">
                <apex:pageBlockTable id="assatt" value="{!wrapperlistattach}" var="cus">
                  <apex:column headerValue="Select" style="width:40px">                                                  
                       <apex:inputCheckbox id="selectcheckbox" value="{!cus.selectedrecord}"/>                    
                  </apex:column>                       
                  <apex:column headerValue="Title">                       
                       <apex:outputText value="{!cus.att.Name}"/>
                  </apex:column>
                  <apex:column headerValue="Content Type">                       
                       <apex:outputText value="{!cus.att.ContentType}"/>
                  </apex:column>                      
               </apex:pageBlockTable>
            </apex:outputPanel>            
            <center><apex:commandButton value="Undelete" action="{!UndeleteRecs}" reRender="pgdblk,avai_att,assatt" status="searchStatus" onclick="reloadPage()"/></center>                                               
        </apex:pageBlockSection>           
    </apex:pageBlock>  
</apex:form>
</apex:page>

It should work,but next time onward please post your requirement elaborately.Also post what you have tried so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy