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
FredoDevFredoDev 

How to assign an Relation Object data to a variable

Hi Devs... 

 

My soql query

soqlQuery = "select id,name, SF__Sa_Ord__r.SF__acc__r.id, SF__Sa_Ord__r.SF__acc__r.name from SF_Account__c";

 

sObject[] records = null;
qResult = SFService.query(soqlQuery);
SF_Account__c SFAccount = new SF_Account__c();
SF__Sa_Ord__c SO = new SF__Sa_Ord__c();


if (qResult.size > 0)
{
MessageBox.Show("Total Number of records Present in Salesforce:"
+ qResult.size);
records = qResult.records;


for (int i = 0; i < records.Length; i++)
{
Acc= (SF_Account__c )records[i];
SA = (SF__Sa_Ord__c)records[i];   ---- Unable 

 

var1 = Acc.Id;

var2 = Acc.Name;

var3 = SA.SF__acc__r.Id;

var4 = SA.SF__acc__r.Name;

 

}

 

Exception : 

Unable to cast object of type

'WindowsFormsApplication1.SFDC.o2bc__Order_Line__c' to type

'WindowsFormsApplication1.SFDC.o2bc__Sales_Order__c'.

 

Can anybody know the solution ?

JWykelJWykel

God I wish SF would elminate the whole underscore craze!  That stuff is so very hard to read.

/rant

 

Unfortunately, I don't know if you are posting the incorrect code for where the error is or what; there is no mention of Order_Line__c or Sales_Order__c in your code, so there is no way for us to say what could be causing the exception.

 

If you're saying the line SA = (SF__Sa_Ord__c)records[i]; is where the exception happens, I would say that you might be running into the problem of selecting  'SF_Account__c' records and are trying to convert them to 'SF__Sa_Ord__c'...they cannot be.

 

If your query is this:
SELECT Id, Name, Parent__r.Id, Parent__r.Name FROM Child__c

 

You would access the named objects like this:

Child__c child = (Child__c)result.records[i];  //The 'FROM' is what the records pulls, so convert them to that type
Parent__c parent = child.Parent__r; //The Parent is automatically filled based on the query pulling Parent__r.Id, Parent__r.Name, etc, so you can reference it directly once you have converted child