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
uat_live1.3903047664538198E12uat_live1.3903047664538198E12 

Get the ID of a Lookup Field when Using SObject List

Hi All,

I Use The Below Code to Give a Lookup
List<SObject> InstanceType = New List<SObject>();
for(Sobject temp:InstanceType){
ViewPIP__c ViewPIPInstance = New ViewPIP__c();
String EvntId = String.ValueOf(temp.get('SitEduTest__Event_Id__r.ID'));
ViewPIPInstance.ForEvent__c = EvntId;

Where SitEduTest__Event_Id__c is Lookup in the Object(SitEduTest__Internal_Exam__c) in the SObject List. I get 
"System.SObjectException: Invalid field SitEduTest__Event_Id__r.ID for SitEduTest__Internal_Exam__c"

Could Some One Please Guide me in this

Greatly appreciate your Help.

Regards,
Christwin

Where
Best Answer chosen by uat_live1.3903047664538198E12
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@uat_live1.3903047664538198E12:


It will be something like this:

List<SObject> InstanceType = New List<SObject>();
Sobject SitEduTestEvent;
for(Sobject temp:InstanceType){
SitEduTestEvent=new Sobject();
ViewPIP__c ViewPIPInstance = New ViewPIP__c();
//String EvntId = String.ValueOf(temp.get('SitEduTest__Event_Id__r.ID'));
SitEduTestEvent=temp.getSObject('SitEduTest__Event_Id__c'); // Use getSobject method to retreive the parent info
//Access rest of the fields from SitEduTestEvent instance.
ViewPIPInstance.ForEvent__c = SitEduTestEvent.get('id');


For reference, take a look at below links:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_dml.htm - Last section in it
http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_sobject.htm

Hope it helps:

Thanks,
Balaji

All Answers

Balaji Chowdary GarapatiBalaji Chowdary Garapati
@uat_live1.3903047664538198E12:


It will be something like this:

List<SObject> InstanceType = New List<SObject>();
Sobject SitEduTestEvent;
for(Sobject temp:InstanceType){
SitEduTestEvent=new Sobject();
ViewPIP__c ViewPIPInstance = New ViewPIP__c();
//String EvntId = String.ValueOf(temp.get('SitEduTest__Event_Id__r.ID'));
SitEduTestEvent=temp.getSObject('SitEduTest__Event_Id__c'); // Use getSobject method to retreive the parent info
//Access rest of the fields from SitEduTestEvent instance.
ViewPIPInstance.ForEvent__c = SitEduTestEvent.get('id');


For reference, take a look at below links:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_dml.htm - Last section in it
http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_sobject.htm

Hope it helps:

Thanks,
Balaji
This was selected as the best answer
uat_live1.3903047664538198E12uat_live1.3903047664538198E12
Thank you, It Was Really helpful.

I only made the following changes.
When I gave 
SitEduTestEvent=new Sobject(); I Got "Constructor not defined: [SObject].<Constructor>() "
I gave Like 
Sobject SitEduTestEvent = temp.getSObject('SitEduTest__Event_Id__c'); I Got Invalid RelationShip SitEduTest__Event_Id__c .
So I gave the Following and it worked

Sobject SitEduTestEvent = temp.getSObject('SitEduTest__Event_Id__r');
ViewPIPInstance.ForEvent__c = String.valueof(SitEduTestEvent.get('id'));

Is it code fine to Use.

Could you please also let us know the Difference Between an Object and an Sobject .I read SObject is a General Object but could not understand the Declaration used for Object Like
Object Test = ;

Thank You Again,

Regards,
Christwin