Function for fetching the Quota for a Sales rep from NetSuite

/**
             * Function to fetch the quota for each sales rep or sales manager in the previous month
             * @param {number} salesRepId 
             * @param {date} startDate 
             * @param {date} endDate 
             * @returns {number} quotaMap
             */
            getMonthlyQuota(salesrep, startDate, endDate) {
                try {
                    let quotaMap = {}
                    const quotaQueryKILM = query.create({
                        type: query.Type.QUOTA,
                    });
                    const entityJoinQNIS = quotaQueryKILM.autoJoin({
                        fieldId: 'entity^entity',
                    });
                    const quotaQueryKILMConditionNAMB = quotaQueryKILM.createCondition({
                        fieldId: 'date',
                        operator: query.Operator.WITHIN,
                        values: [
                            startDate,
                            endDate,
                        ],
                    });
                    const quotaQueryKILMConditionXLMF = quotaQueryKILM.createCondition({
                        fieldId: 'ismanager',
                        operator: query.Operator.IS,
                        values: [
                            false,
                        ],
                    });
                    const quotaQueryKILMConditionAAAF = entityJoinQNIS.createCondition({
                        fieldId: 'id',
                        operator: query.Operator.ANY_OF,
                        values: salesrep,
                    });
                    quotaQueryKILM.condition = quotaQueryKILM.and(
                        quotaQueryKILMConditionAAAF,
                        quotaQueryKILMConditionXLMF,
                        quotaQueryKILMConditionNAMB,
                    );
                    const quotaQueryKILMColumnNAZN = entityJoinQNIS.createColumn({
                        fieldId: 'id',
                        groupBy: false,
                        context: {
                            name: 'RAW',
                        },
                        // label: 'Internal ID',
                        alias: 'id',
                    });


                    const quotaQueryKILMColumnGTFP = quotaQueryKILM.createColumn({
                        fieldId: 'mamount',
                        groupBy: false,
                        context: {
                            name: 'CURRENCY_CONSOLIDATED',
                        },
                        // label: 'Amount Monthly',
                        alias: 'mamount',
                    });


                    const quotaQueryKILMColumnNHCJ = quotaQueryKILM.createColumn({
                        fieldId: 'mamountquarterly',
                        groupBy: false,
                        context: {
                            name: 'CURRENCY_CONSOLIDATED',
                        },
                        // label: 'Amount Quarterly',
                        alias: 'mamountquarterly',
                    });


                    const quotaQueryKILMColumnPNPL = quotaQueryKILM.createColumn({
                        fieldId: 'date',
                        groupBy: false,
                        context: {
                            name: 'RAW',
                        },
                        // label: 'Date',
                        alias: 'date',
                    });


                    const quotaQueryKILMColumnLCFP = entityJoinQNIS.createColumn({
                        fieldId: 'entitytitle',
                        groupBy: false,
                        context: {
                            name: 'RAW',
                        },
                        // label: 'Name/ID',
                        alias: 'entitytitle',
                    });


                    const quotaQueryKILMColumnKVME = quotaQueryKILM.createColumn({
                        fieldId: 'entity',
                        groupBy: false,
                        context: {
                            name: 'DISPLAY',
                        },
                        // label: 'Rep',
                        alias: 'entity',
                    });
                    quotaQueryKILM.columns = [
                        quotaQueryKILMColumnNAZN,
                        quotaQueryKILMColumnGTFP,
                        quotaQueryKILMColumnNHCJ,
                        quotaQueryKILMColumnPNPL,
                        quotaQueryKILMColumnLCFP,
                        quotaQueryKILMColumnKVME,
                    ];
                    // Note: Query.run() is limited to 5,000 results
                    let monthlyQuota;
                    quotaQueryKILM.run().results.forEach((result) => {
                        const resultMap = result.asMap();
                        const date = resultMap.date;
                        const id = resultMap.id;
                        const mamount = resultMap.mamount;
                        const entity = resultMap.entity;
                        const dateObj = new Date(date);
                        const year = dateObj.getFullYear();
                        const month = dateObj.getMonth() + 1; // JavaScript months are zero-based
                        if (!quotaMap[id]) {
                            quotaMap[id] = {};
                        }


                        if (!quotaMap[id][year]) {
                            quotaMap[id][year] = {};
                        }
                        //  quotaMap[id][year][month] = mamount || 0;
                        monthlyQuota = mamount;
                    });
                    return monthlyQuota;
                } catch (e) {
                    log.error("Error @ getMonthlyQuota", e);
                    return {};
                }
            },

Leave a comment

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