0

I tried to get all the opp and contact from account where account name != null. But when I did debug I am not getting any result from opp and contact I am getting null result. Can any one please guide me what need to be done for fixing this issue.

public List<Contact>     contRLst = new List<Contact>();
public List<Opportunity> oppRLst  = new List<Opportunity>();
public List<Account>     accRLst  = new List<Account>();

public List<Account> accountChildInformation(){
     accLst = [SELECT Name, 
                     (SELECT Name 
                      FROM   Contacts 
                      LIMIT  100),
                      (SELECT Name
                      FROM   Opportunities 
                      LIMIT  100)
               FROM  Account
              WHERE  Name != null];

    for(Account a : accLst){
       contRLst = a.getSObjects('Contacts'); 
       oppRLst  = a.getSObjects('Opportunities');
       accRLst.add(a);
    }

Please help me to trigger out the issue.

  • Are you sure you have data ?(i.e. you're not in an empty sandbox) What are you doing with the lists? (i.e. where are you using them) There appears to be more code after this snipet. I've run this code in my org and it works as expected. – Nick Cook Aug 11 '15 at 6:51
  • As Nick suggested please check whether you have appropriate data. And i would suggest use addAll() method inside loop and check the size at the end. – San Aug 11 '15 at 7:19
  • Note that contRLst = a.getSObjects('Contacts') can be more simply written as contRLst = a.contacts; – cropredy Aug 12 '15 at 1:10
0

Change this

for(Account a : accLst){
       contRLst = a.getSObjects('Contacts'); 
       oppRLst  = a.getSObjects('Opportunities');
       accRLst.add(a);
    }

into this

for(Account a : accLst){
           for(Contact con : a.Contacts) 
                 system.debug(con);
           for(Opportunity opp : a.Opportunities) 
                 system.debug(opp);
           accRLst.add(a);
        }

Also make sure your record exist in the org(including child).

| improve this answer | |
  • This doesn't actually solve the issue though, it only helps to debug what's going on. Although the comment about checking the record exists is probably the most important. – Nick Cook Aug 11 '15 at 7:02
  • I could not see any issue with getSObjects(), it is working fine in my org aswell – San Aug 11 '15 at 7:16

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.