Search code examples
jsonapirestputrestlet

REST Script for PUT Request on Multiple Records


I have a REST script that's currently working well for single-record PUT requests. I'm looking to update the script such that it can support multi-record updates.

Current Script:

function put(context) {

    // doValidation([context.recordtype, context.id], ['recordtype', 'id'], 'PUT');
    var rec = record.load({
        type: context.recordtype,
        id: context.id
    });

    var values = context.values;
    for (var fldName in values) {
        if (values.hasOwnProperty(fldName)) {
            rec.setValue({
                fieldId: fldName,
                value: values[fldName]
            });
        }
    }

    rec.save({
        ignoreMandatoryFields: true
    });
    rec = record.load({
        type: context.recordtype,
        id: context.id
    });
    return rec; // reloading includes the results of any triggered actions.
}


return {
    post: postProcess,
    put: put
};

Working Single Record JSON body payload for PUT request:

{
"recordtype": "customrecord_record",
"id": "201",
"values": {
    "custrecord_managementpriority": "3"
}

Ideally looking for the PUT script to support this payload structure:

{
"recordtype": "customrecord_record",
"id": "201",
"values": {
    "custrecord_managementpriority": "3"
}
},
{
    "recordtype": "customrecord_record",
    "id": "204",
    "values": {
        "custrecord_managementpriority": "4"
    }
}

(Wrapped in [ ] or however necessary of course).


Solution

  • Solved. Will leave this post up in case anyone else runs into this issue. This piece upfront did the trick:

    var contexts = context;
    if(!Array.isArray(contexts)) 
        contexts = [contexts];
    
    var rec;
    contexts.forEach((context) => {