Removing Subsidiaries in Customer-Subsidiary Relationship
Recently, I was tasked with removing X subsidiary from all of our existing customers, and I was unable to find anyway to do this efficiently. I tried using CSV import and using the “overwrite sublists” feature, however I don’t think it works, because in the customer-subsidiary sublist it cannot remove all the subsidiaries, therefore it cannot replace the sublist with one I put into the CSV.
I was wondering if there was anyway to automate or possibly do this without having to manually go through all of our existing customers and removing the subsidiary from their customer-subsidiary relationship. Any help would be greatly appreciated!
Just adding onto this, I have tried to come up with a solution using SuiteScript to enter in and automatically remove the subsidiaries that I designate after I hit “edit” on a customers file, however I cannot seem to get it to work, this is the script so far.
/**
* @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: 'subsidiaries'
});
// 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: 'subsidiaries',
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: 'subsidiaries',
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
};
}
);
I would appreciate any help/insight, thanks again
Updating this again! I ended up being able to solve this error, I wrote a working script (noticed my last one was just riddled with errors) and did a CSV import comprising of all of our existing customer base to run the script that I wrote that would trigger and delete the extra subsidiaries that I designated. I had to run getSublists() and .getSublistValues to access the “Subsidiaries” sublist tab under customers and then ran for-loops to iterate through and delete lines I did not want. Hope this methodology can help future people to fix this issue.



