1
for(integer j = 0; j < IPList.size(); j++){ 
    t = '';
    system.debug(t);
    for(Integer i = (IPList[j].Product_Releases_del__r.size()-1) ; i >= 0; i--){
        Installed_Products__c x = IPList.get(j).Product_Releases_del__r.get(i);
        system.debug(x);
        if(t == x.Product_Family__c){
            system.debug('==='+ IPList.get(j).Product_Releases_del__r.get(i));
            system.debug(i);
            IPList.get(j).Product_Releases_del__r.remove(i);
            system.debug(IPList[j].Product_Releases_del__r) ;
            system.debug('=== go');
        }
        else{
            system.debug(i);
            t = x.Product_Family__c;
            system.debug(t);
        }   
    }            
}

Need help deleting values from a list. The if/else statements work and the right value is identified, but it won't remove it from the list.

1 Answer 1

2

Product_Releases_del__r is a list of sObjects which is of subquery type returned by QueryLocator and its readonly.

So remove(index) method won't work on this list.

What you can do is create a list of Installed_Products__c and add the Installed_Products__c which you want to remove from Product_Releases_del__r and delete this list out of the loop.

List<Installed_Products__c> IPListDelete = new List<Installed_Products__c>();
for(integer j = 0; j < IPList.size(); j++){ 
    t = '';
    system.debug(t);
    for(Integer i = (IPList[j].Product_Releases_del__r.size()-1) ; i >= 0; i--){
        Installed_Products__c x = IPList.get(j).Product_Releases_del__r.get(i);
        system.debug(x);
        if(t == x.Product_Family__c){
                system.debug('==='+ IPList.get(j).Product_Releases_del__r.get(i));
                system.debug(i);
                //IPList.get(j).Product_Releases_del__r.remove(i);
                IPListDelete.add(IPList.get(j).Product_Releases_del__r.get(i));
                system.debug(IPList[j].Product_Releases_del__r) ;
                system.debug('=== go');
        }
        else{
            system.debug(i);
            t = x.Product_Family__c;
            system.debug(t);
        }
    }
}
delete IPListDelete;

Hope it helps.

2
  • 1
    The fact that "remove" doesn't work on subquery lists has to do with some behind-the-scenes magic involving QueryLocator. You can't modify the list because it's read-only, not for any other reason.
    – sfdcfox
    Commented Jul 28, 2015 at 17:54
  • I know that lists can't be modified(addition or deletion) but don't know the exact reason behind it(readonly). Thanks @sfdcfox. Commented Jul 28, 2015 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .