Hi I am writing Custom GL plugin Script that will...
# suitescript
m
Hi I am writing Custom GL plugin Script that will just see the line with specific account and if that line doesn't have name (entity) value then it will reverse the line and create a new line the script is working fine only the issue is appearing when it is reading custom segment value. all of the custom segments are on header so the custom line is taking those segments automatically but for service it is on line level and i am using according to the netsuite defined way to get the segment but it is not working below is my script
Copy code
for (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);
    }
}