0

I have a requirement where I need to remove Account from sObjectList as my tempSOjectList contains account. Below is my code when I try to use sObjectList.remove(s) it throws error incorrect signature. What should I do to achieve this?

Controller

public static List<String> getStandardObjects(String objectName){
    system.debug('objectName ===>>>'+objectName);
    List<string> sObjectList = new List<string>();
    List<string> tempSOjectList = new List<string>();
    tempSOjectList.add('Account');

    sObjectList.add('Account');
    sObjectList.add('Contact');
    sObjectList.add('Lead');
    sObjectList.add('Opportunity');
    sObjectList.add('Case');
    for(string s : sObjectList){
        if(tempSOjectList.contains(s)){
            sObjectList.remove(s);
        }
        update sObjectList;
    }
        return sObjectList;
}
3

You are much better off using Sets for this sort of thing.

Set<String> sObjects = new Set<String>();
Set<String> tempSOjects = new Set<String>();
tempSOjects.add('Account');

sObjects.add('Account');
sObjects.add('Contact');
sObjects.add('Lead');
sObjects.add('Opportunity');
sObjects.add('Case');

sObjects.removeAll(tempSObjects);

I note that you apply a DML operation with a list of strings, which is not a valid thing to do.

2
  • I missed out to mention that my method is @AuraEnabled and I need to return this set to my js, so after trying out your solution it says set<string> is not supported return type for auraEnabled methods. Can you suggest something to overcome this? – Indresh Bhargav Feb 6 at 15:41
  • 1
    Conversion to a list for return is as simple as using return new List<String>(sObjects); – Phil W Feb 6 at 16:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.