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>();


List<List<SObject>> searchResults = [FIND :searchKey IN ALL FIELDS RETURNING 

//hmPhones is a set already having some values before this operation

Set<String> targetPhones = new Set<String>();
Message__c[] msgs = searchResults[0].clone();
Contact[] contacts = searchResults[1].clone();
for(Message__c msg : msgs)
for(Contact con : contacts)
hmPhones.addAll(targetPhones);//Here I am getting the FinalException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.