I am using SOQL to query Salesforce via the Mobile SDK SFResRequest method. I want to know is it possible to create an efficient query that will return a set of parent records and their associated children records in the JSON such that the JSON can be assigned to an array and then to iterate through the NSArray for each object (a NSDictionary, the parent) to iterate through each sub-object (the child) and assign the key/values to another dictionary. See below for illustrative purpose what I can currently achieve, which is very much the inverse: Compile the query and send the request (during the ViewDidLoad method):

NSString *stringUser = [SFAccountManager sharedInstance].idData.username;
_labelCurrentUser.text = stringUser;
NSMutableString *stringFilteredRESTRequest = [[NSMutableString alloc]init];
NSString *stringPart1 = [[NSString alloc]init];
NSString *stringPart2 = [[NSString alloc]init];
NSString *stringPart3 = [[NSString alloc]init];
stringPart1 = @"Select id, subject, activitydate, time_slot__c, task.account.name, task.account.id, task.account.omnis_account_short_name__c, task.account.omnis_customer_account_number__c, task.account.Buyer_email__c, task.account.Buyer_Name__c, task.account.Phone, task.account.Full_Address__c  from task where OwnerId = '";
stringPart2 = [GlobalVariables sharedInstance].stringUserId;
stringPart3 = @"' AND subject = 'customer visit' and activitydate !=NULL and activitydate > yesterday order by activitydate asc, time_slot__c asc limit 200";
[stringFilteredRESTRequest appendString:stringPart1];
[stringFilteredRESTRequest appendString:stringPart2];
[stringFilteredRESTRequest appendString:stringPart3];
SFRestRequest *request = [[SFRestAPI sharedInstance] requestForQuery:stringFilteredRESTRequest];
[[SFRestAPI sharedInstance] send:request delegate:self];

enter preformatted text here

Assigned the returned JSON to an array

    - (void)request:(SFRestRequest *)request didLoadResponse:(id)jsonResponse {
    NSArray *records = [jsonResponse objectForKey:@"records"];
    self.arrayCustomerAndContacts = records;

Iterate through the dictionaries (during the cellForRowAtIndexPath method):

NSDictionary *dictionaryVisitDetails = [self.arrayCustomerVisits objectAtIndex:indexPath.row];
NSDictionary *dictionaryCustomerDetails = [dictionaryVisitDetails objectForKey:@"Account"];
self.labelAccountCode.text = [dictionaryCustomerDetails objectForKey:@"Account_Number__c"];
if ([dictionaryCustomerDetails objectForKey:@"Buyer_email__c"] == (id)[NSNull null]){self.labelBuyerEmail.text = stringEmptyField;}else {self.labelBuyerEmail.text = [dictionaryCustomerDetails objectForKey:@"Buyer_email__c"];
self.labelCompanyAddress.text = [dictionaryCustomerDetails objectForKey:@"Full_Address__c"];

As you can see the JSON is returning the children records along with the parent record attributes, but I now want the parent records with the associated children records which is more efficient.


I'm not quite sure about the Objective-C side of things, but you can construct a query similar to the following if you want children along with the parents (Making the assumption here that these are Accounts and Contacts):

SELECT Id, Name, Type, AccountNumber, (SELECT Id, FirstName, LastName, Email FROM Contacts) FROM Account

As a note, the "Contacts" in the subquery is actually the name of the relationship to the Account (which, in the case of custom objects, isn't always the same name as the child object).

By structuring the query like this, you'll get an array of JSON results inside each Account.

JSON Results

Try it out in the workbench, where you can test out the REST response to the query similar to:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.