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
Milan VadhiaMilan Vadhia 

How Can I improve apex controller class for visualforce page?

Hello, I am trying to improve given controller class. Can anybody tell me how can I improve given controller class for this VF page which is redirected from account using custom button. I want to improve this class code as per coding standards.

Thank you.
 
ManageContacts.vfp
<apex:page standardController="Account" extensions="ManageContactController" >
    <div style="position:absolute;top:15px;left: 50%;">
        <apex:actionStatus id="actionProcess" >
            <apex:facet name="start" >
                <apex:image url="{!$Resource.Load_white}" height="40px" />
            </apex:facet>
        </apex:actionStatus>
    </div>
    <apex:form > 
    	<apex:pageBlock title="Contacts of Account : {!account.Name}" id="contactList">
    	    <apex:pageBlockButtons >
                <apex:commandButton action="{! saveAll }" value="Save all"/>
                <apex:commandButton action="{! cancel }" value="Cancel"/>
            </apex:pageBlockButtons>
             <apex:variable value="{!0}" var="rowNum"/> 
            <!--<apex:pageBlockSection columns="5">-->
                <apex:pageBlockTable id="pb1" value="{!listContacts}" var="contact" align="center">
                    <apex:column headerValue="Action" >
                        <apex:commandButton action="{!deleteRow}" value="Delete" immediate="true" reRender="contactList" status="actionProcess">
                            <apex:param value="{!rowNum}" name="rowNumId" assignTo="{!rowNumId}"/>
                        </apex:commandButton>
                        <apex:variable var="rowNum" value="{!rowNum + 1}" />
                    </apex:column>    
      	        	<apex:column headerValue="First Name"><apex:inputField value="{!contact.FirstName}" /></apex:column>
      	    	    <apex:column headerValue="Last Name"><apex:inputField value="{!contact.LastName}"/></apex:column>
      	        	<apex:column headerValue="Phone"><apex:inputField value="{!contact.Phone}"/></apex:column>
      	    	    <apex:column headerValue="Email"><apex:inputField value="{!contact.Email}"/></apex:column>
   		        </apex:pageBlockTable>
   		        <br/><apex:commandLink value="Add Contact Row" action="{!addContactRow}" immediate="true" reRender="contactList" status="actionProcess"/>
            <!--</apex:pageBlockSection>-->
	    </apex:pageBlock>
    </apex:form>
</apex:page>

ManageContactController.apxc

public class ManageContactController {
    public List<Contact> listContacts       {   get;set;    }
    public List<Contact> deleteContacts  = new List<Contact>();
    public Set<Contact> CompareContacts = new Set<Contact>();    
    private Account objAccount;
    public ManageContactController(ApexPages.StandardController stdController){
        objAccount = (Account) stdController.getRecord();
        listContacts = getContacts(objAccount.Id);
        compareContacts.addAll(listContacts);
    }
    // To fatch contacts from database using AccountId
    private List<Contact> getContacts(String accountId){
        return [SELECT Id, 
                       FirstName, 
                       LastName, 
                       Phone, 
                       Email 
                  FROM Contact 
                 WHERE AccountId =:accountId];
    }
    // To add new row to table
    public void AddContactRow(){
        listContacts.add(new Contact(AccountId = objAccount.Id));
    }
    // To update database
    public void saveAll(){
        List<Contact> listContactsAdd = new List<Contact>();
        for(Contact objContact : listContacts ) if(objContact.LastName !=null) listContactsAdd.add(objContact);
        upsert listContactsAdd;
        if(! deleteContacts.isEmpty()) delete deleteContacts;
    }
    // To delete row from table
    public void deleteRow(){
        Integer rowId = Integer.valueOf(apexpages.currentpage().getparameters().get('rowNumId'));
            if(compareContacts.contains(listContacts.get(rowId))) deleteContacts.add(listContacts.get(rowId));
            listContacts.remove(rowId);
    }
}
Best Answer chosen by Milan Vadhia
Milan VadhiaMilan Vadhia
optimised code for    managaeContactHandler.apxc
 
public class ManageContactController {
    public List<Contact> listContacts       {   get;set;    }
    private Account objAccount;
 
    public ManageContactController(ApexPages.StandardController stdController){
        objAccount = (Account) stdController.getRecord();
        listContacts = getContacts(objAccount.Id);
    }
    
    // To fatch contacts from database using AccountId
    private List<Contact> getContacts(String accountId){
        return [SELECT Id,
                       FirstName,
                       LastName,
                       Phone,
                       Email
                  FROM Contact
                 WHERE AccountId =:accountId];
    }
    
    // To add new row to table
    public void AddContactRow(){
        listContacts.add(new Contact(AccountId = objAccount.Id));
    }
    
    // To update database
    public void saveAll(){
       upsert listContacts;
    }
    
    // To delete row from table
    public void deleteRow(){
        Integer rowId = Integer.valueOf(apexpages.currentpage().getparameters().get('rowNumId'));
        Contact objContact = listContacts.remove(rowId);
        if(String.isNotBlank(objContact.Id)) delete objContact;
    }
}

 

All Answers

Salesforce####Salesforce####
by he way is the above code working now , are you able to acheive your functionality ? if so tell me what is this vf page functionality
Milan VadhiaMilan Vadhia
Yes this code is working. and I am able to acheive functionality. This VF page functionality is to edit all the contacts details related to account and from there only we can add new contacts as well as we can delete contacts from this VF page..

For example there is one account Called 'TCS' in SObject Account. if I want to edit all the contacts related to TCS account which are in SObject Contact in one Single page then we can acheive this functionality by this VF page. where we can delete contacts and add new contacts to it as well as update exsisting contacts.

To redirect to this VF page have created custom button in Account detail page.

For this page i want to improve code standard of apex class.

Hope this can make you understand whole task. Thank you.
Milan VadhiaMilan Vadhia
optimised code for    managaeContactHandler.apxc
 
public class ManageContactController {
    public List<Contact> listContacts       {   get;set;    }
    private Account objAccount;
 
    public ManageContactController(ApexPages.StandardController stdController){
        objAccount = (Account) stdController.getRecord();
        listContacts = getContacts(objAccount.Id);
    }
    
    // To fatch contacts from database using AccountId
    private List<Contact> getContacts(String accountId){
        return [SELECT Id,
                       FirstName,
                       LastName,
                       Phone,
                       Email
                  FROM Contact
                 WHERE AccountId =:accountId];
    }
    
    // To add new row to table
    public void AddContactRow(){
        listContacts.add(new Contact(AccountId = objAccount.Id));
    }
    
    // To update database
    public void saveAll(){
       upsert listContacts;
    }
    
    // To delete row from table
    public void deleteRow(){
        Integer rowId = Integer.valueOf(apexpages.currentpage().getparameters().get('rowNumId'));
        Contact objContact = listContacts.remove(rowId);
        if(String.isNotBlank(objContact.Id)) delete objContact;
    }
}

 
This was selected as the best answer