Hey all, I posted this question to the website, bu...
# general
j
Hey all, I posted this question to the website, but I will just repost it here in hopes of finding a solution, I basically have to remove a certain subsidiary inside of the customer-subsidiary relationship sublist under a customers form from all existing customers that we have in our database currently. I tried using a CSV import with overwrite settings to wipe the list and then import a subsidiary list that I designate, however it is unable to wipe the sublist completely, since the customers have open/past transactions with some of the subsidiaries in the current customer-subsidiary relationship. I was wondering if there was any other way to remove the subsidiary other than manually doing it for our database of customers. Any help is greatly appreciated, and thanks for including my into this community!
👀 1
I've been working on this a bit further and decided to try and write a suitescript that will automatically edit the subsidiaries sublist under a customers records, but i'm having issues testing / deploying the script, the code is
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/log', 'N/record'],
    function (log, record) {
        function beforeSubmit(context) {
            try {
                if (context.type === context.UserEventType.EDIT) {
                    // Get the customer record
                    var customerRecord = context.newRecord;

                    // Specify the subsidiary internal ID you want to remove
                    var subsidiaryToRemove = '3'; // Replace with the actual subsidiary internal ID

                    // Get the number of subsidiaries on the Subsidiaries sublist
                    var subsidiariesCount = customerRecord.getLineCount({
                        sublistId: 'subsidiary'
                    });

                    // Loop through each subsidiary on the Subsidiaries sublist
                    for (var line = subsidiariesCount - 1; line >= 0; line--) {
                        // Get the subsidiary internal ID from the sublist
                        var subsidiaryId = customerRecord.getSublistValue({
                            sublistId: 'subsidiary',
                            fieldId: 'subsidiary',
                            line: line
                        });

                        // If the current subsidiary matches the one to be removed, remove the line
                        if (subsidiaryId === subsidiaryToRemove && subsidiariesCount > 1) {
                            customerRecord.removeLine({
                                sublistId: 'subsidiary',
                                line: line
                            });

                            log.debug({
                                title: 'Subsidiary Removed',
                                details: 'Subsidiary ' + subsidiaryToRemove + ' removed from customer record.'
                            });
                        }
                    }
                }
            } catch (e) {
                log.error({
                    title: 'Error',
                    details: e.toString()
                });
            }
        }

        return {
            beforeSubmit: beforeSubmit
        };
    }
);
if anyone could point out what I am doing wrong or let me know of any problems with this id appreciate it. It ran prior as a onevent(VIEW), but it threw an error about not having permissions to edit, so I changed it to an on edit function, however now it does not seem to run at all.