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
RstrunkRstrunk 

Apex class for VF page

Hello all, 

 

     I am trying to do what I think would be very simple but since I have no experience with APEX my attempts have been unsuccessful.  

 

I need an apex class that will act as an extension to a standard controller on a VF page.  

 

  I need the apex class to:

 

         Query the contact object to find a CONTACT RECORD that has the same first name, last name and email address as
                     the 
CURRENT LOGGED IN USER.  

         Save the contact record ID in a variable that can be accessed in the VF page. 

 

 

It seems very easy but I am a noob with apex.  

 

Someone please help!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
public with sharing class MyExtension {
  public Contact theContact { get; set; } // You can use theContact on your page.

  public MyExtension(ApexPages.StandardController controller) {
    Contact[] c = [SELECT Id FROM Contact WHERE FirstName = :UserInfo.getFirstName() AND LastName = :UserInfo.getLastName() and Email = :UserInfo.getUserEmail()];
    // Make sure you select all the fields you need.
    if(!c.isEmpty()) {
      theContact = c[0];
    }
  }
}

 

All Answers

sfdcfoxsfdcfox
Contact[] c = [SELECT Id FROM Contact WHERE FirstName = :UserInfo.getFirstName() AND LastName = :UserInfo.getLastName() and Email = :UserInfo.getUserEmail()];
if(!c.isEmpty()) {
  // c[0] is the contact you're looking for.
}

 

RstrunkRstrunk

Thanks for the fast response.  

 

    I'm not sure how to use that code.  

sfdcfoxsfdcfox
public with sharing class MyExtension {
  public Contact theContact { get; set; } // You can use theContact on your page.

  public MyExtension(ApexPages.StandardController controller) {
    Contact[] c = [SELECT Id FROM Contact WHERE FirstName = :UserInfo.getFirstName() AND LastName = :UserInfo.getLastName() and Email = :UserInfo.getUserEmail()];
    // Make sure you select all the fields you need.
    if(!c.isEmpty()) {
      theContact = c[0];
    }
  }
}

 

This was selected as the best answer
RstrunkRstrunk

Thanks!!