Activity Forums Salesforce® Discussions Why we cannot perform DML operation on SET type collection variable in Salesforce?

  • Adarsh

    Member
    January 12, 2018 at 8:06 am

    Hello Kapil,

    Yes, we can't perform DML operation on Set collection -

    1) List -> Ordered(Allows indexing), Allow duplicacy and Allow DML.
    List<Integer> lstArr = new List<Integer>{1, 2, 3};
    system.debug(lstArr[1]); //Valid

    1) Set-> Unordered(Does not allows indexing) and did't Allow duplicacy.
    Set<Integer> setArr= new Set<Integer>{1, 2, 3};
    system.debug(setArr[1]); //Not valid

    For DML operation - In the case of sets ,we can't use DML over sObjects. in that we need to add all Data in set to list and go for DML once again.

    Hope it helps 🙂

  • Parul

    Member
    September 18, 2018 at 6:58 pm

    Yes, we cannot perform DML operation on collection variables. But for DML operation you need to add all Data in set and then go for DML.

     

     

    Thanks

  • shariq

    Member
    September 19, 2018 at 9:31 pm

    Hi,

    One reason may be that Set<SObject> is a risky mechanism to use: equality is based on all the fields (so is expensive) and if fields are changed logic can easily break. Same problem using SObject as a Map key. So not a pattern to be encouraged.

    But perhaps just because also supporting Set<SObject> adds a bunch of extra methods that need documenting and supporting

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos