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
saykosayko 

How do I make a SOQL query that allows me to Add/Remove/Edit values in Custom Object Fields?

I have created a custom object, students__c, and several related fields e.g. Name__c, Year__c, etc. I now wish to create a VF page and a form which will allow me to add new entries for this custom object. How should I go about it? Do I have to have a custom class here? I would also like to display a list of already enrolled students beneath the form.
Best Answer chosen by sayko
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sayko,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="InsertEditDeleteC">
    <apex:pageMessages/>
    <apex:form>
        <apex:pageBlock title="Enter Details" >
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!saveIt}" />
                <apex:commandButton value="Cancel" action="{!cancelIt}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!student.Name}" />
                <apex:inputField value="{!student.Course__c}" />
                <apex:inputField value="{!student.Mobile_Number__c}" />
                <apex:inputField value="{!student.Email__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock >
        <br/>
        <apex:pageBlock title="Details" >
            <apex:inlineEditSupport showOnEdit="update, cancelButton" 
                                    hideOnEdit="editButton" event="ondblclick" 
                                    changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
            <apex:pageBlockTable value="{!sub}" var="st">
                <apex:column >
                    <apex:inputCheckbox value="{!st.check_box}" />
                </apex:column>
                <apex:column headerValue="Name">
                    <apex:outputfield value="{!st.cs.Name}"/>
                </apex:column>
                <apex:column headerValue="Course">
                    <apex:outputfield value="{!st.cs.Course__c}"/>
                </apex:column>
                <apex:column headerValue="Mobile Number">
                    <apex:outputfield value="{!st.cs.Mobile_Number__c}"/>
                </apex:column>
                <apex:column headerValue="Email">
                    <apex:outputfield value="{!st.cs.Email__c}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons>
                <apex:commandButton id="update" value="Update" action="{!quickUpdate}" />
                <apex:commandButton id="deleteit" value="Delete Selected" action="{!delete_now}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class InsertEditDeleteC {
    
    public Student__c student {get;set;}
    public String message {get;set;}
    public List<WrapperClass> sub {get;set;}
    public Boolean check_box {get;set;}
    public List<Student__c> del {get;set;}
    
    public InsertEditDeleteC() {
        student = new Student__c();
        sub = new List<WrapperClass>();
        for(Student__c cr : [SELECT Id, Name, Course__c, Mobile_Number__c, Email__c FROM Student__c]){
            sub.add(new WrapperClass (cr, false));
        }
    } 
    
    public pageReference saveIt(){
        try{
            INSERT student;
        }
        catch (Exception e) {
            ApexPages.addMessages (e);
            return null;
        }
        PageReference pr = new PageReference(System.currentPageReference().getURL());
        pr.setRedirect(true);
        return pr;
    }
    
    public pageReference cancelIt(){
        PageReference pr = new PageReference(System.currentPageReference().getURL());
        pr.setRedirect(true);
        return pr;
    }
    
    public PageReference quickUpdate(){
        try{
            List<Student__c> accts = new List<Student__c>();            
            for(WrapperClass w : sub){
                accts.add(w.cs);
            }
            UPDATE accts;
            return ApexPages.CurrentPage();
        }catch(Exception e){
            message='Data Base error during saving...';
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, message));
            return null;
        }
    }
    
    public PageReference delete_now(){
        del=new list<Student__c>();
        for(WrapperClass cc: sub){
            if(cc.check_box){
                del.add(cc.cs);
            }
        }
        delete del;   
        PageReference pr = new PageReference(System.currentPageReference().getURL());
        pr.setRedirect(true);
        return pr;
    }
    
    public class WrapperClass {
        public Student__c cs {get; set;}
        public Boolean check_box {get; set;}
        
        public WrapperClass(Student__c cs, Boolean check_box){
            this.cs = cs;
            this.check_box = check_box;
        }
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas​

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sayko,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="InsertEditDeleteC">
    <apex:pageMessages/>
    <apex:form>
        <apex:pageBlock title="Enter Details" >
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!saveIt}" />
                <apex:commandButton value="Cancel" action="{!cancelIt}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!student.Name}" />
                <apex:inputField value="{!student.Course__c}" />
                <apex:inputField value="{!student.Mobile_Number__c}" />
                <apex:inputField value="{!student.Email__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock >
        <br/>
        <apex:pageBlock title="Details" >
            <apex:inlineEditSupport showOnEdit="update, cancelButton" 
                                    hideOnEdit="editButton" event="ondblclick" 
                                    changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
            <apex:pageBlockTable value="{!sub}" var="st">
                <apex:column >
                    <apex:inputCheckbox value="{!st.check_box}" />
                </apex:column>
                <apex:column headerValue="Name">
                    <apex:outputfield value="{!st.cs.Name}"/>
                </apex:column>
                <apex:column headerValue="Course">
                    <apex:outputfield value="{!st.cs.Course__c}"/>
                </apex:column>
                <apex:column headerValue="Mobile Number">
                    <apex:outputfield value="{!st.cs.Mobile_Number__c}"/>
                </apex:column>
                <apex:column headerValue="Email">
                    <apex:outputfield value="{!st.cs.Email__c}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons>
                <apex:commandButton id="update" value="Update" action="{!quickUpdate}" />
                <apex:commandButton id="deleteit" value="Delete Selected" action="{!delete_now}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class InsertEditDeleteC {
    
    public Student__c student {get;set;}
    public String message {get;set;}
    public List<WrapperClass> sub {get;set;}
    public Boolean check_box {get;set;}
    public List<Student__c> del {get;set;}
    
    public InsertEditDeleteC() {
        student = new Student__c();
        sub = new List<WrapperClass>();
        for(Student__c cr : [SELECT Id, Name, Course__c, Mobile_Number__c, Email__c FROM Student__c]){
            sub.add(new WrapperClass (cr, false));
        }
    } 
    
    public pageReference saveIt(){
        try{
            INSERT student;
        }
        catch (Exception e) {
            ApexPages.addMessages (e);
            return null;
        }
        PageReference pr = new PageReference(System.currentPageReference().getURL());
        pr.setRedirect(true);
        return pr;
    }
    
    public pageReference cancelIt(){
        PageReference pr = new PageReference(System.currentPageReference().getURL());
        pr.setRedirect(true);
        return pr;
    }
    
    public PageReference quickUpdate(){
        try{
            List<Student__c> accts = new List<Student__c>();            
            for(WrapperClass w : sub){
                accts.add(w.cs);
            }
            UPDATE accts;
            return ApexPages.CurrentPage();
        }catch(Exception e){
            message='Data Base error during saving...';
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, message));
            return null;
        }
    }
    
    public PageReference delete_now(){
        del=new list<Student__c>();
        for(WrapperClass cc: sub){
            if(cc.check_box){
                del.add(cc.cs);
            }
        }
        delete del;   
        PageReference pr = new PageReference(System.currentPageReference().getURL());
        pr.setRedirect(true);
        return pr;
    }
    
    public class WrapperClass {
        public Student__c cs {get; set;}
        public Boolean check_box {get; set;}
        
        public WrapperClass(Student__c cs, Boolean check_box){
            this.cs = cs;
            this.check_box = check_box;
        }
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas​
This was selected as the best answer
saykosayko
Thank you very much for your help. :)