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
Adam BengtsonAdam Bengtson 

Visualforce Remoting Exception: Collection is read-only

I have a dynamic soql query that I am trying to run and then update based on what is being passed in.
 
global static Contact submitContactById_test(Map<String, String> basicInfo, Map<String, Boolean> preferences, String user_id) {
    	String s = 'SELECT ';
    	String f = ' FROM Contact';
    	String w = ' WHERE Individual_Auto_ID__c=\''+user_id+'\'';
    	Integer i = 0;
    	
    	preferences.remove('unsubscribe');
    	
    	for(String name: basicInfo.keySet()){
    		s += name + ', ';
    	}
    	
    	for(String name: preferences.keySet()){  						
			if(i != 0)
				 s += ', ';
			
			s += name;
			i++;
    	}
    	
    	contact = Database.query(s+f+w);

		for(String name: basicInfo.keySet()){
    		contact.put(name,basicInfo.get(name));
    	}
    	
    	for(String name: preferences.keySet()){
    		contact.put(name,preferences.get(name));
    	}
        Database.update(contact);

		return contact;
        
    }

I've run test on everything from top to bottom and the query and what is being updated should be exactly what I want, but the problem is I cannot get passed "Visualforce Remoting Exception: Collection is read-only" error.  I tried using .clone() which worked for some people I found when searching on this but it did not seem to change anything for me.

Another thing I find interesting is that I have this same functionality in another function I wrote and it works fine.

Thanks!
Chris  ByromChris Byrom
I think the problem is with how you are doing the Database.Query. That is meant to return List<sObject>. In this case a list of contacts. You are then trying to put stuff into this list that isn't a Contact. I am assuming you are trying to put fields into the field list of a specific contact, while the compiler thinks you are trying to add things to the list of sObjects.
Adam BengtsonAdam Bengtson
I just swapped the code out to treat it as a list and still I get the same error.

 
public static List<Contact> contacts {get; set;}
   public static Contact contact {get; set;}


global static Contact submitContactById_test(Map<String, String> basicInfo, Map<String, Boolean> preferences, String user_id) {
    	String s = 'SELECT ';
    	String f = ' FROM Contact';
    	String w = ' WHERE Individual_Auto_ID__c=\''+user_id+'\'';
    	Integer i = 0;
    	
    	preferences.remove('unsubscribe');
    	
    	for(String name: basicInfo.keySet()){
    		s += name + ', ';
    	}
    	
    	for(String name: preferences.keySet()){  						
			if(i != 0)
				 s += ', ';
			
			s += name;
			i++;
    	}
    	
    	contacts = Database.query(s+f+w);

		for(String name: basicInfo.keySet()){
    		contacts[0].put(name,basicInfo.get(name));
    	}
    	
    	for(String name: preferences.keySet()){
    		contacts[0].put(name,preferences.get(name));
    	}
        Database.update(contacts[0]);

		return contacts[0];
        
    }

 
Chris  ByromChris Byrom
Does it work if you just call it from a class, without using remoting?
Adam BengtsonAdam Bengtson
it doesn't seem to matter how its called but I can double check this again.

Here is a working function in the same class:
 
global static boolean unsubscribe(string user_id, string newsletterField) {
        
        try{
            contacts = Database.query('select ' + newsletterField + ' FROM Contact WHERE Individual_Auto_ID__c = \'' + user_id + '\'');
        } catch(Exception e){
            return false;
        }
        
        if(contacts.size() < 1) {
            return false;
        } else {
            contacts[0].put(newsletterField,false);
            Database.update(contacts[0]);
            return true;
        }
    }

this completely works and its basically the same idea but without the maps.