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
Forcedotcom737Forcedotcom737 

Questions around Apex fundamentals: sObject, Id's, joining objects

1. Contact c = new Contact();

For the statement above is 'c' an instance of the class Contact hence making it an object of the class Contact? Is Contact (standard object) a class which salesforce refers to as sObjects?

2. t.WhatId = a.id  ('t' is an instance of the standard object task and 'a' is an instance of the standard object Account)

2.1 Could you explain whats going on in the above statement? An example would be helpful.
My guess: In the statement above the record id of created account 'a' being associated with the new task created. The id (in the task object) is a foreign key linked to the account object. 

2.2  The Id fields dont seem to be listed as object fields when viewing the fields (customize objects>fields). Whats the best way to locate the id fields in sObjects. Is it via reports? Any alternatives?

3. c.AccountId = at.Account__c; The goal of this statement is to make the account in the attendee (at) object (lookup to account) appear in the newly created contact (c). See code below.

Why would these statements not work? :
c.Account = at.Account__c
OR 
c.AccountId = at.Account__cId (linking the Id's)

*************************
Refrence code which uses the above statements:
trigger CreateContact on Attendee__c (after insert, after update) {

List <Contact> contactToInsert = new List <Contact>();
for(Attendee__c at :Trigger.new){

Contact c = new Contact();

c.Division__c = at.Division__c;

c.FirstName = at.First_Name__c;
c.LastName = at.Last_Name__c;
c.AccountId = at.Account__c;

contactToInsert.add(c);

}

insert contactToInsert;

}

-------
trigger CreateFlowupTask on Account (after insert,after update) {


// Created collection for saving new tasks
List <task> taskToInsert = new List <task> ();

//Trigger new is the list of all new accounts

for (Account a : Trigger.new) {
task t = new task ();
t.Subject = 'Call';
t.Status = 'Not Started';
t.Priority = 'Normal';

t.WhatId = a.id;

taskToInsert.add(t);

}

insert taskToInsert;

}
Best Answer chosen by Forcedotcom737
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

1 - Yes, c is an instance of sObject Contact. Every Standard or Custom object derives from the superclass sObject. The term sObject refers to any object that can be stored in the Force.com platform database.

https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SObjects.htm (https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SObjects.htm" target="_blank)

2 - Yes and Yes.

2.1 - You are correct. WhatIds are polymorphic. Polymorphic means a WhatId is equivalent to the ID of a related object. WhatId is a polymorphic Lookup Field (foreign key). By associating a Account with a Task, you will able to see the Task in the related list Task in the Account Detail Page layout.

https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_task.htm (https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_task.htm" target="_blank)

2.2 - Well if you want to query them you can use the developer console, which is a powerful online development tool, to query any sObject's field in a org. You can find on youtube tons of guides.

https://www.youtube.com/watch?v=hKNjzh9rOD0 (https://www.youtube.com/watch?v=hKNjzh9rOD0" target="_blank)

3. The field that relates the parent object with the child object in the child contains the parent id. For Contact, it's AccountId. For a custom object, it was set by the user creating the field. You can go to the child object and check. Once you know the field name, you can access it directly. For Contact:
Id parentId = myContact.AccountId;
For an object with a MasterDetail (or lookup) field called Parent__c:
 
Id parentId = myObject.Parent__c;
When retrieving data, using the suffix __c you get the Id of the target object in a relationship. 

Using the suffix __r you get the target object itself.

"[...]In addition, certain objects, including custom objects, have one or more fields of type reference that contain the ID value for a related record. These fields have names that end in the suffix “-Id”, for example, OwnerId in the account object. OwnerId contains the ID of the user who owns that object. Unlike the field named Id, reference fields are analogous to foreign keys and can be changed via the update() call. For more information, see Reference Field Type.[..]"

http://www.salesforce.com/developer/docs/api/Content/field_types.htm#i1435616 (http://www.salesforce.com/developer/docs/api/Content/field_types.htm#i1435616" target="_blank)

http://www.salesforce.com/developer/docs/api/Content/field_types.htm#i1435823 (http://www.salesforce.com/developer/docs/api/Content/field_types.htm#i1435823" target="_blank)

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.