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
nicloznicloz 

Select fields values

Hi, Please could you help me take one value from a field of object contact? This is my code, it is not working properly(the result is null), the ID 0033000000bOWgWAAW exist and the name of the field is correct, I want to take a value of field address and send it via url to external web service.

 

    contact cont = new contact(Id = '0033000000bOWgWAAW');
    string name2 = cont.FirstName; /// the result is null

 

All this code is inside a class or inside a trigger.

 

I thing this is a simple thing but I am stuck. (I check the book "apex code developer's guide Version 18").

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
imuinoimuino

If you want to retrieve the data for that contact you need to make an soql query

What you are doing there is just setting a new contact Object with the id given, but all other fields are null.

To do what you want you need to do the following

contact c = [Select <includes all fields separated by coma you want> From contact Where Id=: <yourid>];

Then if you access to the name for example you will have the value

All Answers

imuinoimuino

If you want to retrieve the data for that contact you need to make an soql query

What you are doing there is just setting a new contact Object with the id given, but all other fields are null.

To do what you want you need to do the following

contact c = [Select <includes all fields separated by coma you want> From contact Where Id=: <yourid>];

Then if you access to the name for example you will have the value

This was selected as the best answer
nicloznicloz

Thank you very much for your help.