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
Ramin MohammadiRamin Mohammadi 

Loop through subquery

Guys,

I need to loop through a subquery, but am getting an "Invalid Type: Product_Releases__r" Error.

Query:

IPList = [select Name,(select Account__c, Product_Family__c from Product_Releases_del__r ORDER BY Product_Family__c ASC) from Account a where ParentID =: AccountID];


Loop Code:

    For(Account acc: IPList){
        For(Product_Releases_del__r p: acc.Product_Releases_del__r){
            if( p.Product_Family__c == FTemp){
                acc.remove(p);
            }
            else {
                FTemp = acc.Product_Family__c;
            }   
        }


Thanks
 
Equipo Salesforce Sura GEquipo Salesforce Sura G
Go to your child object definition(Product_Releases_del) and check the name set for  "Child Relationship Name". THats the one you should use.
Krishna SambarajuKrishna Sambaraju
Change the code in your for loop as below.
For(Account acc: IPList){
        For(Product_Release_del__c p: acc.Product_Releases_del__r){
            if( p.Product_Family__c == FTemp){
                acc.remove(p);
            }
            else {
                FTemp = acc.Product_Family__c;
            }   
        }
}

Hope this helps.
Amit Chaudhary 8Amit Chaudhary 8
If child relationship name is right then you need to use below code

For(Account acc: IPList)
{
    //acc.Product_Releases_del__r will return array of acc.Product_Releases_del__c
    For(Product_Releases_del__c p: acc.Product_Releases_del__r)
    {
        
        if( p.Product_Family__c == FTemp){
                acc.remove(p);
            }
            else {
                FTemp = acc.Product_Family__c;
            }   
       }
}       

Please let us know if this will help u

THanks
Amit Chaudhary
smriti sharan19smriti sharan19
Below is the code:

for ( Account IPList : [ select Name,(select Account__c, Product_Family__c from Product_Releases_del__r ORDER BY Product_Family__c ASC) from Account a where ParentID =: AccountID] ) {
     for ( Product_Releases_del__c p: acc.Product_Releases_del__r) {
      if( p.Product_Family__c == FTemp){
                acc.remove(p);
            }
            else {
                FTemp = acc.Product_Family__c;
            }   
      }

Hope it helps
Thanks
Smriti Sharan