Insert the data into a field in the object using Visualforce page and Apex

Steps to follow :

1. Creating of a custom object
2. Creating of custom fields
3. Creating an Apex class with a method to insert the data received from the Visualforce page
4. Creating 2 Visualforce pages
    a. Page to insert the data
    b. Page to redirect after the action has taken place


Request - Radical team has received a request from it's client to collect data of their customers.(Name,City,Country and Phone number) No validation is required. And the client wants some blue color sprinkled. "They like blue color"

Understanding the situation -  Collecting the data of the customer needs to be stored somewhere and this can be achieved in Salesforce. Yes, in custom object. Now the situation arrives where to get the color from ? Someone from the back hinted, "let's customize it for the client by creating a Page".
1. A VF page where the data of the customer is entered and some "blue color" on it.
2. An Apex class to handle this data and a custom object to store all this.

Technical team Analysis-  Using custom controller,Apex:inputtext and apex:command Button for inserting records into custom object.


Visualforce Page

<apex:page sidebar="false" controller="DataLoadTestingClass">
 <apex:form >
     <div style="border:1px solid; width:200px;">
    <div style="height:30px;width:150px;margin-top:20px;margin-left:20px;
                     font-size:15px;color:blue;">
             DATA INSERT
         </div>
     <table>
         <tr>
             <td>
                 Name
             </td>
             <td>
                 <apex:inputtext value="{!nameVal}"/>
             </td>
             </tr>
             <tr>
             <td>
                 City
             </td>
             <td>
                 <apex:inputtext value="{!cityVal}" />
             </td>
             </tr>
             <tr>
             <td>
                 Country
             </td>
             <td>
                 <apex:inputtext value="{!countryVal}" />
             </td>
             </tr>
            
             <tr>
             <td>
                 Phone
             </td>
             <td>
                 <apex:inputtext value="{!phoneVal}"/>
             </td>
         </tr>
         <tr >
             <td colspan="2" align="center">
                 <apex:commandButton value="INSERT" style="color:red;" 
                 action="{!doInsert}" />
             </td>
         </tr>
     </table>
     </div>
    
    
    
 </apex:form>

</apex:page>


VisualforcePage - messsagePage

<apex:page>
  Data has been successfully inserted.
</apex:page>


Apex Class

public with sharing class DataLoadTestingClass {

    public PageReference doInsert() {
        DataloadTest__c objdlt = new DataLoadTest__c();
        objdlt.name=nameVal;
        objdlt.city__c=cityVal;
        objdlt.country__c=countryVal;
        objdlt.phone__c=phoneVal;
        insert objdlt;
       
        pagereference ref = new pagereference('/apex/messsagePage');
        ref.setredirect(true);
        return ref;
    }



    public String phoneVal { get; set; }

    public String countryVal { get; set; }

    public String cityVal { get; set; }

    public String nameVal { get; set; }
   
 
}
We need your support,Please share/Like us on Facebook ⛅
Insert the data into a field in the object using Visualforce page and Apex Insert the data into a field in the object using Visualforce page and Apex Reviewed by dasfrogpractice on 04:51 Rating: 5

13 comments:

  1. what is dataloadtest_c in page reference method above??

    ReplyDelete
    Replies
    1. Hello Mohit,

      DataLoadTest__c is the object we need to create in Salesforce before we create this Page and Class.
      And then create City, Country and Phone under the same object. After the above ,create this page and class to add data to your object.

      Delete
    2. hi sir
      i m devender singh and i have 4 year experice in asp.net and now i m working in salesforce.so this will be good for my carrier ? please let me Know sir

      Delete
  2. Error: Compile Error: Illegal assignment from String to Decimal at line 6 column 9

    in apex code

    ReplyDelete
    Replies
    1. Hello @beena anand,

      Try to have a look what 'type' are your fields.
      In my code here, i consider creating all my fields as 'text' type, so assigning a String type to text works well.

      Phone = Text(String)
      country = Text(String)
      city = Text(String)
      name = Text(String)

      Since you are getting the error 'String to Decimal'. The type should match. If you have provided a field as number, then your apex should contain Integer type.
      If checkbox, then type should be Boolean(True,False)

      I hope you are able to understand what i meant.
      Thanks,
      Krishna

      Delete
    2. Thank you. Wonderful stuff with great explanation. Its working fine. Although I am a beginner.

      Delete
  3. Hi ,

    How can we do the same with reference datatype and using in my VF page?

    ReplyDelete
  4. hi sir
    i m devender singh and i have 4 year experice in asp.net and now i m working in salesforce.so this will be good for my carrier ? please let me Know sir

    ReplyDelete
  5. Thanks for posting such a great article.you done a great job salesforce Online Training

    ReplyDelete
  6. Every thing is working fine.
    But i am not able to insert Date field by the above Method

    ReplyDelete
    Replies



    1. var j$ = jQuery.noConflict();
      function openDatePicker(){
      var dtpckr= document.getElementById('{!$Component.datePicker}');
      j$(dtpckr).datepicker();
      }


      This code would work for your request, understand the script tag code as you can find date picker to pick the required date info. Let me know if it helps

      Delete

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe