Creating Landed Cost Detail

I’m trying to create the Landed Cost Detail record through a UE script when a custom record is getting imported against. The reason why I’m having to do this is because we have 1000’s of these and that record isn’t importable. I’m having difficulties getting this to work and the error I’m getting is ” 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”

 

Code is below, any help would be much appreciated!

 

define ([‘N/record’ , ‘N/search’ , ‘N/runtime’, ‘N/error’,’N/log’],
    function (record, search, runtime, error, log) {
        function afterSubmit (context) {
            var stLogTitle = ‘afterSubmit’;
            try {
                log.debug(stLogTitle, ‘<– SCRIPT START –>’);
                var objRuntime = runtime.getCurrentScript();
                var objRecord = context.newRecord;
                var stLCSearch = objRuntime.getParameter ({
                    name : ‘custscript_landed_cost_det_imp_search’
                });
                if (context.type == context.UserEventType.XEDIT || context.type == context.UserEventType.CREATE || context.type == context.UserEventType.EDIT) {
                    var arrLCDetail = {};
                    arrLCDetail.LCTemaplte = objRecord.getValue(‘custrecord_ns_landed_cost_template’);
                    arrLCDetail.CostCat = objRecord.getValue(‘custrecord_ns_landed_cost_category’);
                    arrLCDetail.CostAlloMeth = objRecord.getValue(‘custrecord_ns_landed_cost_allocation_met’);
                    arrLCDetail.CostFactor = objRecord.getValue(‘custrecord_ns_landed_cost_factor’);
                    log.debug(stLogTitle, JSON.stringify(arrLCDetail));
                    var objLCSearch = search.load({
                        id: stLCSearch
                    });
                    var objLCTemplateFilter = search.createFilter({
                        name: ‘internalid’,
                        operator: search.Operator.IS,
                        values: objRecord.id
                    });
                    var objLCCostCatFilter = search.createFilter({
                        name: ‘custrecord_scm_lc_dtl_costcat’,
                        join: ‘custrecord_scm_lc_dtl_profile’,
                        operator: search.Operator.ANYOF,
                        values: arrLCDetail.CostCat
                    });
                    objLCSearch.filters.push(objLCTemplateFilter, objLCCostCatFilter);
                    var arrResult = getAllResults(objLCSearch, objRecord.id);
                    if (arrResult.length > 0) {
                        record.submitFields({
                            type: ‘customrecord_ns_lc_det_import’,
                            id: objRecord.id,
                            values: {
                                ‘custrecord_ns_landed_cost_status’: 2
                            }
                        });
                        log.debug(stLogTitle, ‘Cannot create more than 1 cost category for the same template.’);
                        log.debug(stLogTitle, ‘<– SCRIPT EXIT –>’);
                        return true;
                    } else {
                        var lcTemplateDetail = createLCDetail(arrLCDetail);
                        log.debug(stLogTitle, lcTemplateDetail);
                    }
                }
            } catch (e) {
                var message = stLogTitle;
                var stError = (e.getDetails !== undefined) ? e.getCode ( ) + ‘: ‘ + e.getDetails ( ) : e.toString ( );
                var stLog = (message) ? message : ‘Error’;
                var errorObj = error.create ({
                    name : ‘9999’ ,
                    message : stLog + ‘ | Reason = ‘ + stError ,
                    notifyOff : false
                });
                record.submitFields({
                    type: ‘customrecord_ns_lc_det_import’,
                    id: objRecord.id,
                    values: {
                        ‘custrecord_ns_landed_cost_status’: 3
                    }
                });
                throw errorObj.message;
                //log error
                log.debug(stLog, stError);
            }
        }
        function isEmpty(stValue) {
            return ((stValue === ” || stValue == null || stValue == undefined)
            || (stValue.constructor === Array && stValue.length == 0)
            || (stValue.constructor === Object && (function(v){for(var k in v)return false;return true;})(stValue)));
        }
        function getAllResults(objSearch, maxResults){
            var intPageSize = 1000;
            // limit page size if the maximum is less than 1000
            if (maxResults && maxResults < 1000) {
                intPageSize = maxResults;
            }
            var objResultSet = objSearch.runPaged({
                pageSize : intPageSize
            });
            var arrReturnSearchResults = [];
            var j = objResultSet.pageRanges.length;
            // retrieve the correct number of pages. page count = maximum / 1000
            if (j && maxResults) {
                j = Math.min(Math.ceil(maxResults / intPageSize), j);
            }
            for (var i = 0; i < j; i++) {
                var objResultSlice = objResultSet.fetch({
                    index : objResultSet.pageRanges[i].index
                });
                arrReturnSearchResults = arrReturnSearchResults.concat(objResultSlice.data);
            }
            if (maxResults) {
                return arrReturnSearchResults.slice(0, maxResults);
            } else {
                return arrReturnSearchResults;
            }
        }
        function createLCDetail(arrLCDetail, LCImportId) {
            var stLogTitle = ‘createLCDetail’;
            log.debug(stLogTitle, arrLCDetail);
            log.debug(stLogTitle, arrLCDetail.LCTemaplte);
            var recLCTemplate = record.load({
                type: ‘customrecord_scm_lc_profile’,
                id: arrLCDetail.LCTemaplte,
                isDynamic: true
            });
            var lineCount = recLCTemplate.getLineCount({
                sublistId: ‘recmachcustrecord_scm_lc_profile_detail’
            });
            log.debug(stLogTitle, ‘lineCount = ‘ + lineCount);
            recLCTemplate.selectNewLine({
                sublistId: ‘recmachcustrecord_scm_lc_profile_detail’
            });
            recLCTemplate.setCurrentSublistValue({
                sublistId: ‘recmachcustrecord_scm_lc_profile_detail’,
                fieldId: ‘custrecord_scm_lc_dtl_costcat’,
                value: arrLCDetail.CostCat
            });
            recLCTemplate.setCurrentSublistValue({
                sublistId: ‘recmachcustrecord_scm_lc_profile_detail’,
                fieldId: ‘custrecord_scm_lc_dtl_method’,
                value: arrLCDetail.CostAlloMeth
            });
            recLCTemplate.setCurrentSublistValue({
                sublistId: ‘recmachcustrecord_scm_lc_profile_detail’,
                fieldId: ‘custrecord_scm_lc_dtl_factor’,
                value: arrLCDetail.CostFactor
            });
            recLCTemplate.commitLine({
                sublistId: ‘recmachcustrecord_scm_lc_profile_detail’
            });
            var lcRecId = recLCTemplate.save();
            record.submitFields({
                type: ‘customrecord_ns_lc_det_import’,
                id: LCImportId,
                values: {
                    ‘custrecord_ns_landed_cost_status’: 1,
                }
            });
            lod.debug(stLogTitle, recLCDetail);
        }
        return {
            afterSubmit : afterSubmit
        };
    });


                
Rookie Asked on May 4, 2021 in SuiteScript.
Add Comment
1 Answer(s)

I believe the error is on


createLCDetail

function,

So all of your


customrecord_scm_lc_profile

records are not updated ? or is it only occured on some records ?

Rookie Answered on May 4, 2021.

This would be for every record. The Landed Cost functionality from the Supply Chain Management has those records locked and I cannot update the record custrecord_scm_lc_profile_detail. So I’m loading the parent custom record customrecord_scm_lc_profile and accessing the sublist record custrecord_scm_lc_profile_detail from there.

on May 4, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor