Hi, so I'm creating a suitelet where a user is ins...
# suitescript
l
Hi, so I'm creating a suitelet where a user is inserting values that are passed to a log list, i want to be able to load the log list records back to the suitelet form (somewhere amity the behaver in transaction item sub list) so i can preform CRUD actions inside the form and load the changes back to the log list. i created a search on the log list and im trying to insert the line to the form.... it's not working, please help 🙂 i think mt approach is totally wrong.... End result is 3 sub lists in one form all actioning the log record
Copy code
let logSearchObj = search.create({
                    type: "xxx_log",
                    filters: [],
                    columns:
                        [
                            search.createColumn({name: "", label: ""}),
                            search.createColumn({name: "", label: ""}),
                            search.createColumn({name: "", label: ""}),
                            search.createColumn({name: "", label: ""})]
                });

logSearchObj.run().each(function(result){
    for(let i=0; i<result.columns.length;i++){
        let val = result.getValue({
            name: result.columns[i].name
        });

        scriptDeployment.insertLine({
            sublistId: id,
            line: i,
        });
        scriptDeployment.setCurrentSublistValue({
            sublistId: id,
            fieldId: recSublistIdsArr[i],
            value: val
        });
        scriptDeployment.commitLine({
            sublistId: 'id'
        });


    }
b
you probably dont want to insert one line per column per search result
its much more common to do one line per search result
l
i'm getting "{"type":"error.SuiteScriptError","name":"SSS_INVALID_SUBLIST_OPERATION","message":"You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist."
b
not terribly surprising you are reusing indexes
l
why not? imtrying to match each result from the log list to the form sublist
b
you are using i as the index for the search columns, the sublist line. and field ids
l
ohhh damn 😐 you are right.... overall battk you think it is the right approach? or am I totally misleading here?
b
there are ways to force it to work, but it requires establishing a relationship between search column ids and sublist columns ids
the easiest of which is to name them the same
usually my first attempt is to just hardcode setting each individual column, which wouldnt require a for loop in the first place
l
thank you! trying the hardcode approach now, I am sure i will work it out....but I'm also interested in what's you thoughts about this way inorder to imitate the CRUD behaver of transaction line item sublist.... I'm 30% trough what i was planning to do i guess I'm trying to see ahead if I am totally wrong or am I on the right track....
b
none of the code you share does that
l
what i did by now is create an editable sublist form POST the data to a custom record/List then get the data back trough a saved search to the suitlet, so when user is coming back he will see the log data and will be able to CRUD action on it
b
really depends on how you plan on doing a create vs updat vs delete
so far the only thing shared looks to be update
you also probably want to compare this to using a parent/child relationship
you can make your child log records appear as an editable sublist on a parent record
l
ohh thanks didn't think about creating parent/child relationship, I'm going to read about it, i haven't experimented on this yet.... (what API should i look for information?)
l
Thank you very much for the assistance battk, will update ...
Ok battk its update time 🙂 I toke your advise and used the same Id's from the Saved Search on the log record with the creation of field on the submit form
Copy code
for (let c = 0; segment_rules_logSearchObj.columns.length && c < segment_rules_logSearchObj.columns.length; c++) {
       recSublistIdsArr.push(segment_rules_logSearchObj.columns[c].name);
}
let logSearchObjLength = segment_rules_logSearchObj.runPaged().count;
for (let j = 0; j < logSearchObjLength; j++) {
    // logde("searchLogData.push(val)", searchLogData)
    if (!isNullOrEmpty(searchLogData)) {
        sublistLoadValues = searchLogData.splice((searchLogData.length - recSublistIdsArr.length), recSublistIdsArr.length)
        //logde("sublistLoadValues", sublistLoadValues)
    }
    for (let i = 0; i < recSublistIdsArr.length; i++) {
        try {
            sublist.setSublistValue({
                id: recSublistIdsArr[i],
                line: j,
                value: sublistLoadValues[i]
            })
        } catch (e) {
            logde("setSublistValue", e)
        }
    }
}
than I created an 2 arrays to hold records objects, 1 from saved Search holding submitted data => old record and one from params with current record
Copy code
const searchResult = segment_rules_logSearchObj.run()
for (let c = 0; segment_rules_logSearchObj.columns.length && c < segment_rules_logSearchObj.columns.length; c++) {
    recSublistIdsArr.push(segment_rules_logSearchObj.columns[c].name);
}
searchResult.each(function (result) {
    let tempResultLoadObj = {}
    for (let i = 0; i < result.columns.length; i++) {

        let val = result.getValue({
            name: result.columns[i].name
        });
        tempResultLoadObj[result.columns[i].name] = val

    }
    oldRecObjArr.push({"id": tempResultLoadObj.internalid, "values": tempResultLoadObj})
    return true;
});

/**
 * Create array of new record Objects from POST params
 */
function zip(keys, values) {
    return Object.fromEntries(
        keys.map((key, index) => [key, values[index]])
    );
}
try {
    for (let i = 0; i < newDataRawsArr.length; i++) {
        newRecordObjArr.push({
            "id": zip(recSublistIdsArr, newDataRawsArr[i]).internalid,
            "values": zip(recSublistIdsArr, newDataRawsArr[i])
        })
    }
} catch (e) {
    logde("Developer expected Error: read details", `inserting new record object to the array has no internal id  ${e}`)
    newRecordObjArr.push({"id": "InsertedRecord", "values": zip(recSublistIdsArr, newDataRawsArr[i])})

}
After this it was an easy flight forward CRUD...
Copy code
if (JSON.stringify(newRecordObjArr) !== JSON.stringify(oldRecObjArr)) {
    insertRecord (newRecordObjArr)

    deletedRecord (newRecordObjArr,oldRecObjArr)

    updatedRecord(newRecordObjArr, oldRecObjArr)
}