Populate Field values from source SO/Journal to Revenue Recognition journal

Revenue recognition journal will be created by bulk. So we can use map reduce script to populate the sublist value from source SO to revenue recognition journal.For the bulk processing, there may be chance for usage limit exceed issue and will throw an error “SSS_USAGE_LIMIT_EXCEEDED” and so you have to use the schedule script (scheduled/map reduce) for the same. So you can avoid the such issues.

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(['N/record','N/search','N/runtime'],
    
    (record,search,runtime) => {
            const savedSearch = {
                    // The saved search for finding all the active customer records
                    journalentrySearch() {
                            var journalentrySearchObj = search.create({
                                    type: "journalentry",
                                    filters:


                                            [
                                                [["type","anyof","Journal"],"AND",["custbody_cl_rev_type_flag","is","F"],"AND",[["memo","contains","Rev Rec Destination"],"OR",["memo","contains","Rev Rec Source"]],"AND",["datecreated","on","today"]]
                                            ],
                                              
                                    columns:
                                        [
                                            search.createColumn({
                                                name: "internalid",
                                                summary: "GROUP",
                                                //sort: search.Sort.DESC,
                                                label: "Internal ID"
                                            })
                                        ]
                            });
                            var srchArr = [];
                            var searchResultCount = journalentrySearchObj.runPaged().count;
                            log.debug("journalentrySearchObj result count",searchResultCount);
                            journalentrySearchObj.run().each(function(result){
                                    // .run().each has a limit of 4,000 results
                                var srchObj = {};
                                srchObj.journalid = result.getValue({
                                    name: "internalid",
                                    summary: "GROUP",
                                    //sort: search.Sort.DESC,
                                    label: "Internal ID"
                                });
                                srchArr.push(srchObj);
                                    return true;
                            });
                            return srchArr;


                    }
            }


            const getInputData = (inputContext) => {
                try{
                log.debug('inside getinput', 'hiii');
                var journalResultArray = savedSearch.journalentrySearch();
                return journalResultArray;
                }
                catch (err) {
                log.debug("error@getInputData", err)
            }


        }

        function revenueArrangementSearch(id){
            try {
                log.debug('revenueArrangementSearchfunctionnnn', id);
                var revenueplanSearchObj = search.create({
                    type: "revenueplan",
                    filters:
                        [
                            ["journal.internalid","anyof",id]
                            // "AND",
                            // ["recordnumber","is","2417"]
                        ],
                    columns:
                        [
                            search.createColumn({
                                name: "recordnumber",
                                sort: search.Sort.ASC,
                                label: "Number"
                            }),
                            search.createColumn({
                                name: "custcol5",
                                join: "appliedToTransaction",
                                label: "Revenue Type"
                            })
                        ]
                });
                var searchResultCount = revenueplanSearchObj.runPaged().count;
                //var revenuetype;
                var arry1=[];
                log.debug("revenueplanSearchObj result count",searchResultCount);
                revenueplanSearchObj.run().each(function(result){
                    // .run().each has a limit of 4,000 results

                    // var scriptObj = runtime.getCurrentScript();
                    // log.debug({
                    //         title: "Remaining usage : ",
                    //         details: scriptObj.getRemainingUsage()
                    // });

                    // .run().each has a limit of 4,000 results

                    var obj={};
                    obj.revenueId = result.getValue({
                        name: "recordnumber",
                        sort: search.Sort.ASC,
                        label: "Number"
                    });

                    obj.revenuetype = result.getValue({
                        name: "custcol5",
                        join: "appliedToTransaction",
                        label: "Revenue Type"
                    });
                    arry1.push(obj);
                    return true;

                });
                log.debug("revenuetype result return",arry1);
                return arry1;
            }
            catch (e) {
                log.debug("revenueArrangementSearch error",e);
            }

        }


        // const map = (mapContext) => {
        //
        // }


        const reduce = (reduceContext) => {
            log.debug("inside reduce function","true");
            var dataObj = JSON.parse(reduceContext.values[0]);
            log.debug("dataObj",dataObj);
            var id = dataObj.journalid;
            log.debug("reduce Jounal id",id);


            var objRecord = record.load({
                type: record.Type.JOURNAL_ENTRY,
                id: id,
                isDynamic: true
            });
            var revRecvalue = objRecord.getValue({
                fieldId: 'isfromrevrec'
            });
            log.debug("revRecvalue", revRecvalue);

            var flag = objRecord.getValue({
                fieldId: 'custbody_cl_rev_type_flag'
            });
            log.debug("flag", flag);

            var lineItemCount = objRecord.getLineCount({
                sublistId: 'line'
            });
            log.debug("lineItemCount", lineItemCount);
            if(revRecvalue == 'T') {
                log.debug("inside ifrev rec", true);
                var revenueSearchRes = revenueArrangementSearch(id);
                log.debug("revenue search", revenueSearchRes);
                var recNum;
                var revenueType;

                // log.debug("recNum", recNum);
                // log.debug("recNum", recNum);
                var sourceid;
                var entityName;
                for(var j=0; j<revenueSearchRes.length;j++){
                    recNum = revenueSearchRes[j].revenueId;
                    revenueType = revenueSearchRes[j].revenuetype;
                    for (var i = 0; i < lineItemCount; i++) {
                        log.debug('index in for loop', i);
                        sourceid = objRecord.getSublistValue({
                            sublistId: 'line',
                            fieldId: 'sourcerevenueplan',
                            line: i
                        });

                        log.debug('sourceid', sourceid);
                        entityName =objRecord.getSublistValue({
                            sublistId: 'line',
                            fieldId: 'entity',
                            line: i
                        });

                        log.debug('entityName', entityName);
                        if(sourceid == recNum) {
                             log.debug("sourceid == recNum", recNum);
                            log.debug("QQQQQQQQQQQrevenueType", revenueType);

                            objRecord.selectLine({
                                sublistId: 'line',
                                line: i
                            })
                            objRecord.setCurrentSublistValue({
                                sublistId: 'line',
                                fieldId: 'custcol5',
                                value: revenueType
                            })
                            objRecord.setCurrentSublistValue({
                                sublistId: 'line',
                                fieldId: 'custcol_customervendor',
                                value: entityName
                            })


                            objRecord.commitLine({
                                sublistId: 'line'
                            });
                        }
                        else{
                            continue;
                        }

                    }

                }


                objRecord.setValue({
                    fieldId: 'custbody_cl_rev_type_flag',
                    value: true
                })

                objRecord.save();

            }

        }


        const summarize = (summaryContext) => {
            var scriptObj = runtime.getCurrentScript();
            log.debug({
                title: "Remaining usage units: ",
                details: scriptObj.getRemainingUsage()
            });

        }

        return {getInputData, reduce, summarize}

    });

Leave a comment

Your email address will not be published. Required fields are marked *