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
morganeCRMCLOUDERmorganeCRMCLOUDER 

problem with remove method for list

Hello,

 

I have a list of custom objects. 

I want to update an element of this list so I remove it and then add it (it's the only thing I've found). 

I refered to the list methods and the example here : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_list.htm

 

But when I refresh the page, there is an error saying the elemnt is duplicate. I checked the log with some system.debug and it is indeed not removed from the list. Can someone explain why ? 

 

I tried both : 

myList.remove(2)       and 

whateverObject__c removedObj = myList.remove(2) 

 

Here is the code of the controller : 

 

public List<Production__c> production { get; set; }
  
    
    public List<Production__c> getName() {
        if(production== null) 
            production= [SELECT ID_Prod__c, name, Tech_name__c, S1__c
                         FROM Production__c 
                         WHERE ID_Prod__c = 1 OR ID_Prod__c = 2 OR ID_Prod__c = 3
                         ORDER BY ID_Prod__c ];
      
        
       Production__c prodAvai = new Production__c(Name='Available', 
                                                   Tech_name__c='AVASTO0', 
                                                   S1__c= 44,
                                                 ID_Prod__c=3);
       
        
      
        production.remove(2);  
        /* or as the doc says : 
        Production__c prodRemoved = production.remove(2);
        */

        Integer nb = production.size();
        System.debug('nb element after rm ' + nb);
        production.add(prodAvai);    // won't work because production[2] is still here 
      
       
        // loop to check the list
        for(Integer i = 0; i < production.size(); i++)
            System.debug('élément ' + i + ' = ' + production[i]);
         

        return production;
    }

 

 

Ashish_SFDCAshish_SFDC

Hi Morgane, 

 

The issue is not with remove(), it is actually removing the row. 

When a new record is inserted it goes into the list at the Bottom of the Array. 

"List" type actually allows duplicates, so no question of duplicate being identified as issue. 

We can explore it further if we identify the exact error and what is it reffering to "elemnt is duplicate". 

 

Regards,

Ashish