Menu Bar

Tuesday 28 August 2012

USING PROPERTIES IN APEX

Using Properties in Apex

Problem

You want to create a page that captures input from users, and that input spans multiple sObjects.

Solution

Use a Visualforce page with a custom Apex controller, and give it properties to represent the input fields from Accounts and Contacts.
  1. Create a custom controller with a simple getName method.
    1. Click Setup | Develop | Apex Classes.
    2. Click New.
    3. In the editor, add the following content:
      /*
       * This class is the controller for the 
       * NewCustomer VisualForce page.
       * It uses properties to hold values entered 
       * by the user.  These values
       * will used to construct multiple SObjects.
       */  
          
      public class Customer {
      
      // Add properties here  
          
      
          /* Required method in a VisualForce controller */  
          
          public String getName() {
              return 'Customer';
          }
      // Add methods here  
          
      // Add queries here  
          
      }
    4. Click Quick Save.
  2. Update the controller by replacing the line // Add properties here with the following Apex properties:
        public String companyName {get; set;}
        public Integer numEmployees {get; set;}
        public String streetAddress {get; set;}
        public String cityAddress {get; set;}
        public String stateAddress {get; set;}
        public String postalCodeAddress {get; set;}
        public String countryAddress {get; set;}
        public String department {get; set;}
        public String email {get; set;}
        public String phone {get; set;}
        public String firstName {get; set;}
        public String lastName {get; set;}
        public String title {get; set;}
    
  3. Click Quick Save.
  4. Update the controller by replacing the line // Add methods here with the following method for saving the property values to a new pair of Account and Contact objects:
        /*
         * Takes the values entered by the user in the VisualForce 
         * page and constructs Account and Contact sObjects.
         */  
        
        public void save() {
            Account a = new Account(
                Name = companyName,
                NumberOfEmployees = numEmployees,
                ShippingStreet = streetAddress,
                ShippingCity = cityAddress,
                ShippingState = stateAddress,
                ShippingPostalCode = postalCodeAddress,
                ShippingCountry = countryAddress);
    
            insert a;
    
            Contact c = new Contact(
                FirstName = firstName,
                LastName = lastName,
                Account = a,
                Department = department,
                Email = email,
                Phone = phone,
                Title = title,
                MailingStreet = streetAddress,
                MailingCity = cityAddress,
                MailingState = stateAddress,
                MailingPostalCode = postalCodeAddress,
                MailingCountry = countryAddress);
    
            insert c;
        }
    
    
  5. Click Quick Save.
  6. Update the controller by replacing the line // Add queries here with queries for displaying Accounts and Contacts related lists:
        /* Used for the Account list at the end of the 
           VisualForce page 
        */  
        
        public List<Account> getAccountList() {
            return [select name, numberofemployees from account];
        }
    
        /* Used for the Contact list at the end of the 
           VisualForce page 
        */  
        
        public List<Contact> getContactList() {
            return [select name, title, department, email, phone 
                    from contact];
        }
    
  7. Click Save.
  8. Click SetupDevelop | Pages.
  9. Click New.
  10. In the name field, enter newCustomerEntry.
  11. Optionally enter a label and description.
  12. In the editor, enter the following markup:
    <apex:page controller="Customer">
        <apex:form >
            <apex:pageBlock title="New Customer Entry">
                <p>First Name: 
                   <apex:inputText value="{!firstName}"/></p>
                <p>Last Name: 
                   <apex:inputText value="{!lastName}"/></p>
                <p>Company Name: 
                   <apex:inputText value="{!companyName}"/></p>
                <p># Employees: 
                   <apex:inputText value="{!numEmployees}"/></p>
                <p>Department: 
                   <apex:inputText value="{!department}"/></p>
                <p>Email: 
                   <apex:inputText value="{!email}"/></p>
                <p>Phone: 
                   <apex:inputText value="{!phone}"/></p>
                <p>Title: 
                   <apex:inputText value="{!title}"/></p>
                <p>Address</p>
                <p>Street: 
                   <apex:inputText value="{!streetAddress}"/></p>
                <p>City: 
                   <apex:inputText value="{!cityAddress}"/></p>
                <p>State: 
                   <apex:inputText value="{!stateAddress}"/></p>
                <p>Zip: 
                   <apex:inputText 
                    value="{!postalCodeAddress}"/></p>
                <p>Country: 
                   <apex:inputText value="{!countryAddress}"/></p>
    
                <p><apex:commandButton action="{!save}" 
                   value="Save New Customer"/></p>
            </apex:pageBlock>
        </apex:form>
       <!-- Add related lists here -->  
        
    </apex:page>
    
    
  13. Click Quick Save.
  14. Update the page by replacing <!-- Add related lists here --> with the following markup to displays the related lists from the queries:
        <apex:pageBlock title="Accounts">
            <apex:pageBlockTable value="{!accountList}" var="acct">
                <apex:column value="{!acct.Name}"/>
                <apex:column value="{!acct.NumberOfEmployees}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:pageBlock title="Contacts">
            <apex:pageBlockTable value="{!contactList}" var="item">
                <apex:column value="{!item.Name}"/>
                <apex:column value="{!item.Phone}"/>
                <apex:column value="{!item.Title}"/>
                <apex:column value="{!item.Department}"/>
                <apex:column value="{!item.Email}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    
  15. Click Save.
  16. Call the page by using the following URL: https://salesforce_instance/apex/newCustomerEntry.

Discussion

You want to ask the user to enter information about a new customer. The fields are used to create both a new account and a new contact associated with that account. Using a Visualforce page lets you present whatever user interface you want, using HTML and Visualforce markup; however, each standard controller inVisualforce corresponds to a single sObject type, such as Account or Contact. To work with more than on sObject type, you need to use a custom controller.
When using a Apex custom controller, the easiest way to do to access data on exposed by the controller is to use Apex properties. The syntax for Apexproperties is similar to C# properties. Java-style bean properties (with getters and setters that you create for each property) also work; however, the property syntax used above is much more readable, and makes it easier to distinguish the controller's properties from its actions.
Queries in a custom controller can be used to present data to the user. In this example, queries are used to create two tables that mimic related lists.
Since the form is simple HTML, you can modify it to your style either using HTML or Visualforce components.
Note that when you add a new customer and click Save, the account and contact information is displayed in the related lists on the page.

42 comments:

Unknown said...

Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!

Salesforce Training in Chennai

Unknown said...

Hi Chaitanya,

very useful article on cloud technology. cloud computing training in chennai

Unknown said...

Thanks for your informative post. Placement training is excellent option for students willing to enter software development industry with lucrative salary package.

Unknown said...

Hadoop Training Chennai

Hi, I am Jack lives in Chennai. I am technology freak. I did Hadoop Training in Chennai at FITA which offers best Big Data Training in Chennai. This is useful for me to make a bright career in IT field.

Big Data Training Chennai


Unknown said...

Thanks for sharing these niche piece of coding to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
Best Informatica Training In Chennai|Informatica training in chennai

Unknown said...

The information you have given here is truly helpful to me. CCNA- It’s a certification program based on routing & switching for starting level network engineers that helps improve your investment in knowledge of networking & increase the value of employer’s network, if you want to take ccna course in Chennai get into FITA, thanks for sharing…
ccna training in Chennai | ccna training institute in Chennai

Unknown said...


Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.Nice article i was really impressed by seeing this article, it was very interesting and it is very useful. CCNA training in chennai | CCNA training chennai | CCNA course in chennai | CCNA course chennai


sss said...

very useful article on cloud technology.
Salesforce training course is recommended in this cutting-edge competition. It is ideal for system administrators managing the configuration and maintaining the salesforce application in an organization.For more detailssalesforce online training in hyderabad

Anonymous said...

you just copied and pasted everything from sfdc documentation.

Unknown said...

Here we provide Hadoop online training and placementfor you..

Unknown said...

Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
python training in omr

python training in annanagar | python training in chennai

python training in marathahalli | python training in btm layout

python training in rajaji nagar | python training in jayanagar

simbu said...

From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
java training in chennai | java training in bangalore

java online training | java training in pune

java training in chennai | java training in bangalore

java training in tambaram | java training in velachery

Saro said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.


rpa training in Chennai | rpa training in velachery

rpa training in tambaram | rpa training in sholinganallur

rpa training in Chennai | rpa training in pune

rpa online training | rpa training in bangalore

sai said...

Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
python training in annanagar
python training in chennai
python training in chennai
python training in Bangalore

shethal said...

Really you have done great job,There are may person searching about that now they will find enough resources by your post
Devops training in sholinganallur
Devops training in velachery

Ram Ramky said...

Thank you for sharing such valuable information and tips. This can give insights and inspirations for us; very helpful and informative! Would love to see more updates from you in the future.
Selenium Training in adyar
Selenium Training in Chennai
Loadrunner Training in Velachery
Loadrunner Training in Adyar
Automation testing training in chennai
Qtp training in Velachery

pavithra dass said...

I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
PHP Training in Chennai
PHP Course in Chennai
PHP Training Institute in Chennai
PHP course
PHP Training

Hemapriya said...
This comment has been removed by the author.
mercyroy said...

Innovative thinking of you in this blog makes me very useful to learn.
i need more info to learn so kindly update it.
Cloud computing Training Bangalore
Best Cloud Computing Training Institute in Anna nagar
Cloud Computing Training in Guindy

Vicky Ram said...

Thank you sharing this kind of noteworthy information. Nice Post.

Guest posting sites
Education

Arunaram said...

Very impressive blog! i liked it and was very helpful for me.Thanks for sharing. Do share more ideas regularly.
Big Data Hadoop Training in Tnagar
Big Data Hadoop Training in Nungambakkam
Big Data Hadoop Training in Saidapet
Big Data Hadoop Training in sholinganallur
Big Data Hadoop Training in navalur
Big Data Hadoop Training in kelambakkam

Unknown said...

Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting.

Photoshop Classes in Chennai
Photoshop Course in Chennai
Photoshop Training in Chennai
Photo Editing Courses in Chennai
Photoshop Training Institute in Chennai
Best Place to Learn Photoshop in Chennai

Unknown said...

Thank you for sharing this useful information with us. Nice work

Ionic Training in Chennai | Ionic 2 Course | Ionic Training Course | Ionic Training in Adyar | Ionic Training in Velachery | Ionic Training in Tambaram

Anbarasan14 said...

I believe that your blog will surely help the readers who are really in need of this vital piece of information. Waiting for your updates.

Spoken English Classes in Anna Nagar
Spoken English Classes in Perambur
Spoken English Classes in Ayanavaram
Spoken English Classes in Anna Nagar West
Spoken English Class in Mogappair
Spoken English Anna Nagar Chennai
Spoken English Class Anna Nagar
Spoken English Classes near me

Arunaram said...

I have to thank for sharing this blog, it is really helpful and I learned a lot from your blog.
Data Science Training in Velachery
Data Science Training in Chennai Velachery
Data Science Training in Tnagar
Data Science Training in Nungambakkam
Data Science Training in Saidapet
Data Science Training in Aminjikarai

LindaJasmine said...

Thanks for sharing such a information. It shows your in-depth knowledge. Pls keep updating.
Hadoop Admin Training in Chennai
Hadoop Administration Training in Chennai
Hadoop Administration Course in Chennai
Hadoop Administration Training
Big Data Administrator Training
Hadoop Administration Course
Hadoop Admin Training Institutes in Chennai

VenuBharath2010@gmail.com said...

Amazing Post. I am very much impressed with your choice of words. The content showcases your in-depth knowledge in this subject. Thanks for sharing.
Social Media Certification
Social Media Classes
Social Media Marketing Certification
Social Media Marketing Classes
Social Media Marketing Training Courses
Social Marketing Training
Social Media Online Training

Anjali Siva said...

Useful content, I have bookmarked this page for my future reference.
UiPath Training in Chennai
UiPath Training near me
ccna course in Chennai
AWS course in Chennai
RPA Training in Chennai
DevOps Training in Chennai
Angularjs Training in Chennai

vijaykumar said...




the blog is nicely maintain.when i read this blog i got lot of ideas and information.thanks to improve my knowledge.
RPA Training Institute in Chennai
RPA course in Chennai

luckys said...

indian whatsapp group links

priya said...

You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.
Microsoft Azure online training
Selenium online training
Java online training
uipath online training
Python online training


Dhanraj said...

Do you know how to save tree quotes

Nick569 said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
CRS Info Solutions Salesforce Admin Training 

lavanya said...

Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.

Java training in Chennai

Java Online training in Chennai

Java Course in Chennai

Best JAVA Training Institutes in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Training

Java Online Training

ram said...

Worth reading! Our experts also have given detailed inputs about these trainings & courses! Presenting here for your reference. Do checkout
oracle training in chennai & enjoy learning more about it.

Devi said...

Chennai's best training institute, Infycle Technologies, offers the No.1 Hadoop training in Chennai for tech professionals & students along with other courses such as Python, Oracle, Selenium, Java, Hadoop, iOS, and Android development with 100% hands-on training. Once the completion of training, the students will be sent for placement interviews in the core MNC's. Call 7504633633 to get more info and a free demo.
Top Hadoop Training in Chennai | Infycle Technologies

UNIQUE ACADEMY said...

UNIQUE ACADEMY FOR COMMERCE, an institute where students enter to learn and leave as invincible professionals is highly known for the coaching for CA and CS aspirants.

cs executive
freecseetvideolectures/
UNIQUE ACADEMY

Steven Cohen said...

Meta Fx Global. Now You Can Login Your Meta Fx Global E-Account Through Your Meta Fx Global Account Login.

Maria Yang said...

Login Your exness login Account To Read The Latest News About The Platform.s

Maridev said...

Grab the Digital Marketing Training in Chennai from Infycle Technologies, the best software training institute, and Placement center in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Cyber Security, Big Data, Java, Hadoop, Selenium, Android, and iOS Development, DevOps, Oracle etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

akhil said...

Nice article and very good information. For Salesforce Dumps (Admin, Developer, Service Cloud, Marketing Cloud etc…) Click Salesforce Dumps

VIPTHANOSS said...

GAME BAAZIGAR