I have written a SOSL query to search on a Custom Object and a Standard Object. When I am getting the results from it, then I am trying to store the value of fields into a set, to do a SOQL query using it. But I am getting error System.FinalException : Collection is Read-Only. Below is the Code,
Note: The code works fine in Anonymous Window.
I can not show the whole code here, but I can explain the flow. I am calling a method in which there is an API callout which is giving me a list of Phone Numbers.
I am storing the Phone numbers in a set hmPhones and after that, I am doing the operation mentioned below in the code.
There is no DML and also this code is not having any connection to Triggers.
Set<String> hmPhones = new Set<String>(); hmPhones.add('19645372835'); hmPhones.add('19645372545'); hmPhones.add('19645456835'); List<List<SObject>> searchResults = [FIND :searchKey IN ALL FIELDS RETURNING Message__c(Targeted_Phone__c), Contact(E164_Phone__c)]; //hmPhones is a set already having some values before this operation Set<String> targetPhones = new Set<String>(); Message__c msgs = searchResults.clone(); Contact contacts = searchResults.clone(); for(Message__c msg : msgs) targetPhones.add(msg.Targeted_Phone__c); for(Contact con : contacts) targetPhones.add(con.E164_Phone__c); hmPhones.addAll(targetPhones);//Here I am getting the FinalException.