Change Values in the Revenue Recognition Plan when values changed in the Subscription Line

  • User will create subscription records in NS for Service items for Sale. Eg: Dakota Studio. In these Service item’s records, Hold Revenue Recognition checkbox will be checked.
  • After creating a subscription record, User will run the bulk processing page- Update Revenue Arrangements and Revenue Plans to create Revenue arrangement and Revenue plan records.
  • In the subscription record, under the related records subtab, under Revenue elements section, we can see the revenue arrangements created.
  • Within the revenue arrangement record, we can view revenue plans created for each subscription line item.
  • The revenue plan record created for the Service items with Hold Revenue Recognition checkbox checked will have the Hold Revenue Recognition checkbox checked in the Revenue plan also.

Requirement

  • In the subscription record, on the item line level, there will be a custom field called- Completion date.
  • Required to check if it is possible to uncheck the Hold Revenue Recognition checkbox in the related revenue plan, once a date in entered in the Completion date field on the subscription line.
  • Also, simultaneously, two fields- Rev Rec start date and Rev Rec End Date in the revenue plan should be updated with the date we entered in the Completion date. field.
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/search','N/record'],
    
    (search,record) => {

            /**
             * function created for checking whether the checkbox is checked in the item record.
             */
            function itemFieldSearch(itemId){
                     try{
                             var itemRecord = search.lookupFields({
                                     type: search.Type.ITEM,
                                     id: itemId,
                                     columns: ['deferrevrec']
                             })
                             log.debug("itemRecord",itemRecord)
                             var checkbox = itemRecord.deferrevrec
                             log.debug("checkbox",checkbox)
                             return checkbox;
                     }catch(er){
                             log.debug("Error@itemRecordCheckBox",er)
                     }
            }

            /**
             * function is created for checking the revenue plans of subscription record.
             */
            function revenueRecognitionPlanSearch(subscriptionId){
                    var revenueelementSearchObj = search.create({
                            type: "revenueelement",
                            filters:
                                [
                                        ["sourcesubscription.subscription","anyof",subscriptionId]
                                ],
                            columns:
                                [
                                        search.createColumn({
                                                name: "revenueplantype",
                                                join: "revenuePlan",
                                                label: "Revenue Plan Type"
                                        }),
                                        search.createColumn({
                                                name: "internalid",
                                                join: "revenuePlan",
                                                label: "Internal ID"
                                        })
                                ]
                    });
                    var searchResultCount = revenueelementSearchObj.runPaged().count;
                    log.debug("revenueelementSearchObj result count",searchResultCount);
                    var array = [];
                    revenueelementSearchObj.run().each(function(result){
                            var obj = {};
                            obj.internalId = result.getValue({
                                    name: "internalid",
                                    join: "revenuePlan",
                                    label: "Internal ID"
                            })
                            obj.revenueType = result.getValue({
                                    name: "revenueplantype",
                                    join: "revenuePlan",
                                    label: "Revenue Plan Type"
                            })
                            array.push(obj);
                            return true;
                    });
                    return array;
            }
        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
                try {
                        if (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.UserEventType.EDIT) {
                                log.debug("ScriptContext", scriptContext.type)
                                var subscriptionLineRecord = scriptContext.newRecord
                                var subscriptionLineId = scriptContext.newRecord.id
                                log.debug("subscriptionLineId", subscriptionLineId)
                                var completionDate = subscriptionLineRecord.getValue({
                                        fieldId: "custrecord_subscription_line_field"
                                })
                                log.debug("completionDate",completionDate)
                                if(completionDate){
                                        var itemId = subscriptionLineRecord.getValue({
                                                fieldId: "item"
                                        })
                                        log.debug("itemId",itemId)
                                        var check = itemFieldSearch(itemId);
                                        if(check === true){
                                                var subscriptionId = subscriptionLineRecord.getValue({
                                                        fieldId: "subscription"
                                                })
                                                var data = revenueRecognitionPlanSearch(subscriptionId)
                                                log.debug("data",data)
                                                for(var i = 0;i< data.length;i++){
                                                        if(data[i].revenueType === "ACTUAL"){
                                                               var  revenueTypeId = data[i].internalId
                                                        }
                                                }
                                                log.debug("revenueTypeId",revenueTypeId)
                                                 var revenuePlanRecord = record.load({
                                                         type: record.Type.REVENUE_PLAN,
                                                         id: revenueTypeId,
                                                         isDynamic: true
                                                 })
                                                 log.debug("revenueRecordPlan",revenuePlanRecord)
                                                 var revenuePlanType = revenuePlanRecord.getValue({
                                                        fieldId: "revenueplantype"
                                                })
                                                log.debug("revenuelanType",revenuePlanType)
                                                if(revenuePlanType == "ACTUAL"){
                                                        var setHoldCheckbox = revenuePlanRecord.setValue({
                                                                fieldId: "holdrevenuerecognition",
                                                                value: false
                                                        })
                                                        log.debug("setHoldCheckbox",setHoldCheckbox)
                                                        var revRecordStartDate = revenuePlanRecord.setValue({
                                                                fieldId: "revrecstartdate",
                                                                value: completionDate
                                                        })
                                                        log.debug("revRecordStartDate",revRecordStartDate)
                                                        var revRecordEndDate = revenuePlanRecord.setValue({
                                                                fieldId: "revrecenddate",
                                                                value: completionDate
                                                        })
                                                        log.debug("revRecordEndDate",revRecordEndDate)
                                                }
                                                revenuePlanRecord.save();
                                          }

                                 }
                        }
                }catch(error){
                        log.debug("Error@beforeSubmit",error)
                        // log.error("error@beforeSubmit",error)
                }
        }
        return { afterSubmit}
    });

Leave a comment

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