public static List<Account> parentWithChildren() { List<Account> accounts = [SELECT Name, AccountSource, (SELECT Name FROM Contacts) FROM Account]; for(Account nextAcct :accounts) { for(Contact nextContact :nextAcct.Contacts) { System.Debug(nextContact); } } return accounts; } // To access value from a lookup inside the inner query List<Case> cases = [SELECT Id, CaseNumber, Subclassificacao__c, Status, (SELECT Id, Produto__r.Name FROM ProdutosDoCaso__r) FROM Case WHERE NTrackwise__c = '' and CreatedDate >= 2023-11-01T00:00:00.000+0000 and Status = 'MTO']; for (Case c : cases) { System.debug('Case Number: ' + c.CaseNumber); System.debug('Subclassificacao: ' + c.Subclassificacao__c); System.debug('Status: ' + c.Status); // Iterate over related Produto__r records for (ProdutoDoCaso__c produto : c.ProdutosDoCaso__r) { System.debug('Produto Name: ' + produto.Produto__r.Name); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter