Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help for one calculation

Are Kaveri
Tera Contributor

Hi  

 

 

I have below requirement.

 

there is one table called History of employee.

 

I need to calculate current year and last year Rank and calculate the points gained.

employee numberyearrank performance points
100202431
100202342
100202253
101202442
101202342
101202242

 

 

I need to calculate the performance points for employee.

 

for this i am not understanding how to pull current and previous year and add the performance points.

 

please help me

 

6 REPLIES 6

Hello @Are Kaveri ,

Please give a try to the scrpt below and let me know how it works for you.

// Assuming you have a GlideRecord named 'history_of_employee' representing the history table
var historyGr = new GlideRecord('history_of_employee');
historyGr.addQuery('year', '>=', new Date().getFullYear() - 1); // Filter records for the current year and the previous year
historyGr.addQuery('year', '<=', new Date().getFullYear());
historyGr.orderBy('employee_number');
historyGr.orderByDesc('year');
historyGr.query();

var currentYear = new Date().getFullYear();
var lastYear = currentYear - 1;

while (historyGr.next()) {
    var employeeNumber = historyGr.employee_number.toString();
    var year = historyGr.year.toString();
    var performancePoints = parseInt(historyGr.performance_points.toString(), 10);

    if (year == currentYear || year == lastYear) {
        // Assuming you have a GlideRecord named 'request' representing the request table
        var requestGr = new GlideRecord('request');
        requestGr.addQuery('employee_number', employeeNumber);
        requestGr.addQuery('year', year);
        requestGr.query();

        if (requestGr.next()) {
            // Update the request record with the calculated performance points
            requestGr.setValue('performance_points', requestGr.performance_points + performancePoints);
            requestGr.update();
        } else {
            // Create a new request record if it doesn't exist for the employee and year
            var newRequestGr = new GlideRecord('request');
            newRequestGr.initialize();
            newRequestGr.setValue('employee_number', employeeNumber);
            newRequestGr.setValue('year', year);
            newRequestGr.setValue('performance_points', performancePoints);
            newRequestGr.insert();
        }
    }
}

Aniket Chavan
Tera Sage
Tera Sage

Hello @Are Kaveri ,

Please give a try to the scrpt below and let me know how it works for you.

// Assuming you have a GlideRecord named 'history_of_employee' representing the history table
var historyGr = new GlideRecord('history_of_employee');
historyGr.addQuery('year', '>=', new Date().getFullYear() - 1); // Filter records for the current year and the previous year
historyGr.addQuery('year', '<=', new Date().getFullYear());
historyGr.orderBy('employee_number');
historyGr.orderByDesc('year');
historyGr.query();

var currentYear = new Date().getFullYear();
var lastYear = currentYear - 1;

while (historyGr.next()) {
    var employeeNumber = historyGr.employee_number.toString();
    var year = historyGr.year.toString();
    var rank = parseInt(historyGr.rank.toString(), 10);

    // Determine performance points based on the rank
    var performancePoints = 0;
    if (rank === 3) {
        performancePoints = 1;
    } else if (rank === 4) {
        performancePoints = 2;
    } else if (rank === 5) {
        performancePoints = 3;
    }

    if (year == currentYear || year == lastYear) {
        // Assuming you have a GlideRecord named 'request' representing the request table
        var requestGr = new GlideRecord('request');
        requestGr.addQuery('employee_number', employeeNumber);
        requestGr.addQuery('year', year);
        requestGr.query();

        if (requestGr.next()) {
            // Update the request record with the calculated performance points
            requestGr.setValue('performance_points', requestGr.performance_points + performancePoints);
            requestGr.update();
        } else {
            // Create a new request record if it doesn't exist for the employee and year
            var newRequestGr = new GlideRecord('request');
            newRequestGr.initialize();
            newRequestGr.setValue('employee_number', employeeNumber);
            newRequestGr.setValue('year', year);
            newRequestGr.setValue('performance_points', performancePoints);
            newRequestGr.insert();
        }
    }
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket