• Rupesh Ghate
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hey, team I am facing some issue WHile Updating Accouunts Notes in My Vf page. please advice .


Visualforce Page

 

Case

ABC Firms wants a custom UI which has ability for them to view Account with account notes on one screen. They wish to create/update multiple account notes simultaneously.

Requirement Details

  • Create a custom object Account notes with Account as parent.
  • Set name field as auto-number and a custom field ‘Notes’ of text area type.
  • Create a custom VF tab, ‘Note Update’.
  • VF Page should have capability of pagination and text provided by user should maintain its state throughout pagination.
  • While pagination a loading image should be displayed for user to wait till operation completes.
  • VF Page should display – Account Number(Read Only), Account Name(Read Only), Account Notes (Notes field of related object in editable mode)
  • Against one account only one account notes will be shown (hence pull the most recent record)
  • User will have the ability to create new note if Note field is blank
  • If note field has some existing value, user will have ability to update it
  • Save button should help user in saving all the changes made by him.
  • You are free to decide on Pages Look and Feel
  • Adhere to salesforce best practices.
  • Create test class to ensure developed code can be deployed; adhere to best practices of testing framework
global class AccountWrapper {
    public String Name{get;set;}
    public String accountNumber{get;set;}
    public Account account{get;set;}
    public List<Account_Note__c> accountNote{get;set;}
    
    public AccountWrapper (String accName,String accNumber,List<Account_Note__c> Acc_Note)
    {
        this.Name = accName;
        this.accountNumber = accNumber;
        this.accountNote = Acc_Note;
        
        for(Account_Note__c accNote : accountNote)
        {
            if(accNote==null)
                accNote = new Account_Note__c();
        }
        
    }
    
    public AccountWrapper(){
        account = new Account();
    }
    public Pagereference save(){
        
        return null;
    }
}
 
==========================Controller=====================

public class AccountWrapperController{
    public List<AccountWrapper> lstWrapper{get; set;}
    public List<Account_Note__c> lstSetController{get; set;}
    public List<account> acc_Lst{get;set;}
    
    public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,AccountNumber,(SELECT Id, Notes__c FROM Account_Notes__r ORDER BY CreatedDate DESC limit 1) FROM Account])); 
                //here you can set the record size to display                
                con.setPageSize(10);       
            }
            return con;
        }
        set;
    } 
    public List<Account> getAccounts() {
        return (List<Account>) Con.getRecords();
    }
    
    /*****************Creating the constructor***********************/ 
    
    
    public AccountWrapperController() {
        lstWrapper =  new List<AccountWrapper>();
        lstSetController = new List<Account_Note__c>();
        try{ 
            for(Account acc : ([SELECT Name,AccountNumber,(SELECT Id, Notes__c FROM Account_Notes__r ORDER BY CreatedDate DESC limit 1) FROM Account])){
                lstWrapper.add(new AccountWrapper(acc.Name,acc.AccountNumber,acc.Account_Notes__r));
            }
            
        }
        catch(SObjectException se) {
            System.debug('The following exception has occurred: ' + se.getMessage());
        }
        /*  obj = new CustomIterable (lstWrapper); 
obj.setPageSize = 10;
next();   */     
    }
    
    /*********************pagination methods*********************/  
    
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }
    
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }
    
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
    
    public void first() {
        con.first();
    }
    
    public void last() {
        con.last();
    }
    
    public void previous() {
        con.previous();
    }
    
    public void next() {
        con.next();
    }
    
    public void cancel() {
        con.cancel();
    }  
    
    
    
    
    /*************************Update Method**********************/   
    public void UpdateNotes() {
        try{
            account_note__c an = new account_note__c();
            for(AccountWrapper AccWrap : lstwrapper){
                for(Account_Note__c Acnt_Note : AccWrap.AccountNote){
                    // upsert Acnt_Note;
                    lstSetController.add(Acnt_Note);
                }
                if(lstSetController.size()>0)
                {
                    upsert lstSetController;
                }
            }
        }
        
        catch(DmlException e) {
            System.debug('DmlException caught: ' + e.getMessage()); 
        }
    }
    public void processAjaxRequest(){  
        //Do some processing here  
        for(Integer i =0 ; i < 10000; i++){  
        }  
    }  
}
 
====================VF page=========================

<apex:page controller="AccountWrapperController" >
    <apex:form >
        <apex:pageBlock id="myPage" mode="edit" >
            
            <apex:pageBlockButtons location="both">
                <apex:commandButton id="update" action="{!UpdateNotes}" value="Save" reRender="myPage" status="actStatusId"/>
                <apex:actionStatus id="actStatusId" >
                    <apex:facet name="start" >
                        <img src="/img/loading.gif" />                    
                    </apex:facet>
                </apex:actionStatus>
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection columns="1">
                
                <apex:pageBlockTable value="{!Accounts}" var="acc" >
                    
                    <apex:column >
                        <apex:facet name="header">Account Name</apex:facet>
                        <apex:outputtext value="{!acc.Name}"/>
                    </apex:column>
                    
                    <apex:column >
                        <apex:facet name="header">Account Number</apex:facet>
                        <apex:outputtext value="{!acc.AccountNumber}"/>
                    </apex:column>
                    <apex:inlineEditSupport showOnEdit="update, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/> 
                    
                    <apex:column >
                        <apex:facet name="header">Account Notes</apex:facet>            
                        <apex:repeat value="{!acc.Account_Notes__r }" var="note">
                            
                            <apex:inputText value="{!note.Notes__c}"/>
                            
                        </apex:repeat>
                    </apex:column>  
                    
                </apex:pageBlockTable>
                
                
                <apex:outputPanel >
                    <apex:commandButton value="<<Previous" action="{!previous}" rendered="{!hasPrevious}" reRender="myPage" status="blockUI" />
                    
                    <apex:commandButton value="Next >>" action="{!next}" rendered="{!hasNext}" reRender="myPage"  status="NextStatusId"  />
                    <apex:actionStatus id="NextStatusId" >
                        <apex:facet name="start" >
                            <img src="/img/loading.gif" />                    
                        </apex:facet>
                    </apex:actionStatus>
                </apex:outputPanel>  
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
    
</apex:page>