1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Generic SObject update pattern

Discussion in 'Programming/Internet' started by Martin Borthiry, Oct 8, 2018.

  1. Question


    I know that it is possible to update a generic of List<SObject>, But what is the best approach to generate that list without knowing the SObjectType, only the ids ?

    Sample:


    List<SObject> l = new List<SObject>();

    SObject o1 = new SObject(id='xxxxxx'); //this Id could be an Account
    o1.Name = 'Test';
    l.add(o1);


    SObject o2 = new SObject(id='xxxxxx'); //this Id could be a Custom
    o2.Name = 'Test'
    l.add(o2);

    //this is possible
    update l;


    Unfortunately, SObject constructor doesn't allow id as parameter.

    Backgound:


    I'm trying to make a generic WS which will receive a list of objects to update( or upsert). The received object could by any type.

    WS Request sample:

    [
    {id:'xxxxx',Name:'Test'},
    {id:'yyyyy',Name:'Test 2'},
    {id:'zzzzz',Name:'Test 3'}
    ]


    The id attribute cloud be from any SObject.( I know the extra attr to update always exist).

    Thanks in advance for your time.

    Login To add answer/comment
     

Share This Page