Muhammad Kamran Mujeeb
04/22/2026, 1:03 PMfor (var i = 0; i < standardLines.count; i++) {
const
line = standardLines.getLine({index: i});
log.debug('line', line);
log.debug('line.getSegmentValueId("csegservice")', line.getSegmentValueId('csegservice'));
log.debug('{accountId: line.accountId, entityId: line.entityId}', {accountId: line.accountId, entityId: line.entityId});
log.debug('Object.values(ICO_ACCOUNTS).includes(Number(line.accountId))', Object.values(ICO_ACCOUNTS).includes(Number(line.accountId)));
if (Object.values(ICO_ACCOUNTS).includes(Number(line.accountId)) && !line.entityId) {
const
debit = Number(line.debitAmount) || 0,
credit = Number(line.creditAmount) || 0;
// :white_check_mark: Skip empty lines
if (debit === 0 && credit === 0) continue;
let
newLine = customLines.addNewLine();
//Reversig the line first
newLine.accountId = line.accountId;
if(debit > 0) newLine.creditAmount = debit;
if(credit > 0) newLine.debitAmount = credit;
newLine.memo = line.memo;
newLine.subsidiaryId = line.subsidiaryId;
newLine.departmentId = line.departmentId;
newLine.classId = line.classId;
newLine.locationId = line.locationId;
newLine.entityId = line.entityId;
populateSegments({line, newLine})
log.debug('newLine', newLine);
newLine = customLines.addNewLine();
//Creation of a line with correct details
newLine.accountId = line.accountId;
// newLine.isTaxable = line.isTaxable;
// newLine.isPosting = true;
// newLine.taxAmount = line.taxAmount;
// newLine.taxType = line.taxType;
// newLine.id = line.id;
if(debit > 0) newLine.debitAmount = debit;
if(credit > 0) newLine.creditAmount = credit;
// newLine.taxItemId = line.taxItemId;
newLine.memo = line.memo;
newLine.subsidiaryId = line.subsidiaryId;
newLine.departmentId = line.departmentId;
newLine.classId = line.classId;
newLine.locationId = line.locationId;
// newLine.entityId = 271439;
newLine.entityId = vendorId;
// newLine.taxableAmount = line.taxableAmount;
populateSegments({line, newLine})
log.debug('newLine', newLine);
// Set entity (this is the fix)
// line.entityId = 259147;
}
}
function populateSegments({line, newLine}){
try{
// Loop through the segments you found in your log
const mySegments = ["csegservice", "csegcountries", "csegbusienss_unit"];
mySegments.forEach(function(segId) {
try {
// Only attempt if the line actually reports having this segment
const val = line.getSegmentValueId(segId);
log.debug('val', val);
if (val) {
log.debug('Found Segment', segId + ' = ' + val);
newLine.setSegmentValueId( segId, parseInt(val, 10));
}
} catch (e) {
log.error('Segment Access Error', 'ID: ' + segId + ' Error: ' + e.message);
}
});
}
catch(err){
log.debug('ERR! Found In populateSegments()', err);
}
}