Business rule if condition is only working for me

Community Alums
Not applicable

Hi,

I have created a business rule and the if condition which was highlighted below no other users is entering to the if even the condition is satisfying but when am trying with my profile it is working fine. Can anyone please let me know what's the issue is? 

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    current.u_sap_me_flag = false;

    //initiate variables
    var chg_start_dt = current.getDisplayValue('start_date');
    var chg_end_dt = current.getDisplayValue('end_date');

    var chg_cat = current.category;
    var chg_srv_off = current.service_offering;


    var isostartdt = timeISOConvertor(new GlideDateTime(chg_start_dt));
    var isoenddt = timeISOConvertor(new GlideDateTime(chg_end_dt));


    gsdt_start = new GlideScheduleDateTime(chg_start_dt);
    gsdt_end = new GlideScheduleDateTime(chg_end_dt);


    var offering = gs.getProperty('SAP Service Offering Sys ID');

    if ((chg_cat.indexOf("SAP") > -1) || (chg_srv_off == offering)) {

        var gr = new GlideRecord('cmn_schedule_span');
        var schedule = gs.getProperty('SAP Me Schedule');
        gr.addQuery('schedule', schedule); //SPAN ME Schedule
        gr.query();

        var recCount = gr.getRowCount();

        while (gr.next()) {

            var schedStart = gr.start_date_time;
            var schedEnd = gr.end_date_time;

            if ((isostartdt <= schedEnd)  && (isoenddt >= schedStart)) {
                current.u_sap_me_flag = true;
                break;
            }
           
        }

    }

    function timeISOConvertor(x) {
        x = x.getRaw();
        var dt = new Packages.java.text.SimpleDateFormat();
        dt.applyPattern('yyyy');
        var ye = dt.format(x).toString();
        dt.applyPattern('MM');
        var mo = dt.format(x).toString();
        dt.applyPattern('dd');
        var da = dt.format(x).toString();
        dt.applyPattern('H');
        var ho = dt.format(x).toString();
        dt.applyPattern('m');
        var mi = dt.format(x).toString();
        dt.applyPattern('ss');
        var se = dt.format(x).toString();

        var ret = ye + mo + da + 'T' + ho + mi + se;
        //reformatted the datestring values

        return ret;
    }

})(current, previous);
4 REPLIES 4

Kieran Anson
Kilo Patron

Hi,

Can you explain the functionality of what you're trying to achieve as there may be a simpler way to code this. Currently this doesn't look like it follows the intended code methodology of ServiceNow...particular with a package call

Community Alums
Not applicable

Hi Thanks for your response.

My requirement was to set a field on change to true if planned start and end date falls in each month end time period. where the month end times has been created as schedule entries records. So when category contains sap or service offering is sap it should check for planned start and end date and if this dates falls in schedule start and schedule end date in schedule entries record then the field should be set to true. For this i have created Business rule and that is only working for the users ( where in profile the date format is system(MM/dd/yyyy) for other users with different date format it is not working. On change planned dates are displayed in this 2024-11-04 11:44:12 format and on schedule entries the date field is stored as 20241008T235959 this format. And on maintenance schedule Timezone was given as GMT.

 

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here     current.u_sap_me_flag = false;
    //initiate variables     var chg_start_dt = current.getDisplayValue('start_date');     var chg_end_dt = current.getDisplayValue('end_date');
    var chg_cat = current.category;     var chg_srv_off = current.service_offering;

    var isostartdt = timeISOConvertor(new GlideDateTime(chg_start_dt));     var isoenddt = timeISOConvertor(new GlideDateTime(chg_end_dt));

    gsdt_start = new GlideScheduleDateTime(chg_start_dt);     gsdt_end = new GlideScheduleDateTime(chg_end_dt);

    var offering = gs.getProperty('SAP Service Offering Sys ID');
    if ((chg_cat.indexOf("SAP") > -1) || (chg_srv_off == offering)) {
        var gr = new GlideRecord('cmn_schedule_span');         var schedule = gs.getProperty('SAP Me Schedule');         gr.addQuery('schedule', schedule); //SPAN ME Schedule         gr.query();
        var recCount = gr.getRowCount();
        while (gr.next()) {
            var schedStart = gr.start_date_time;             var schedEnd = gr.end_date_time;
            if ((isostartdt <= schedEnd)  && (isoenddt >= schedStart)) {                 current.u_sap_me_flag = true;                 break;             }                     }
    }
    function timeISOConvertor(x) {         x = x.getRaw();         var dt = new Packages.java.text.SimpleDateFormat();         dt.applyPattern('yyyy');         var ye = dt.format(x).toString();         dt.applyPattern('MM');         var mo = dt.format(x).toString();         dt.applyPattern('dd');         var da = dt.format(x).toString();         dt.applyPattern('H');         var ho = dt.format(x).toString();         dt.applyPattern('m');         var mi = dt.format(x).toString();         dt.applyPattern('ss');         var se = dt.format(x).toString();
        var ret = ye + mo + da + 'T' + ho + mi + se;         //reformatted the datestring values
        return ret;     }
})(current, previous);

Brad Bowman
Kilo Patron
Kilo Patron

Add some logs to the script to confirm as the other users.  I'm guessing that the timeISOConvertor function is causing problems since it is directly calling a package.  This appears on Impact report / Health Scans with the recommendation to run this tool to identify occurrences, replacing each with the Glide alternative

https://docs.servicenow.com/bundle/xanadu-api-reference/page/script/general-scripting/concept/c_Pack... 

Community Alums
Not applicable

Hi Thanks for your response.

My requirement was to set a field on change to true if planned start and end date falls in each month end time period. where the month end times has been created as schedule entries records. So when category contains sap or service offering is sap it should check for planned start and end date and if this dates falls in schedule start and schedule end date in schedule entries record then the field should be set to true. For this i have created Business rule and that is only working for the users ( where in profile the date format is system(MM/dd/YYYY) for other users with different date format it is not working. On change planned dates are displayed in this 2024-11-04 11:44:12 format and on schedule entries the date field is stored as 20241008T235959 this format. And on maintenance schedule Time zone was given as GMT.

 

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here     current.u_sap_me_flag = false;
    //initiate variables     var chg_start_dt = current.getDisplayValue('start_date');     var chg_end_dt = current.getDisplayValue('end_date');
    var chg_cat = current.category;     var chg_srv_off = current.service_offering;

    var isostartdt = timeISOConvertor(new GlideDateTime(chg_start_dt));     var isoenddt = timeISOConvertor(new GlideDateTime(chg_end_dt));

    gsdt_start = new GlideScheduleDateTime(chg_start_dt);     gsdt_end = new GlideScheduleDateTime(chg_end_dt);

    var offering = gs.getProperty('SAP Service Offering Sys ID');
    if ((chg_cat.indexOf("SAP") > -1) || (chg_srv_off == offering)) {
        var gr = new GlideRecord('cmn_schedule_span');         var schedule = gs.getProperty('SAP Me Schedule');         gr.addQuery('schedule', schedule); //SPAN ME Schedule         gr.query();
        var recCount = gr.getRowCount();
        while (gr.next()) {
            var schedStart = gr.start_date_time;             var schedEnd = gr.end_date_time;
            if ((isostartdt <= schedEnd)  && (isoenddt >= schedStart)) {                 current.u_sap_me_flag = true;                 break;             }                     }
    }
    function timeISOConvertor(x) {         x = x.getRaw();         var dt = new Packages.java.text.SimpleDateFormat();         dt.applyPattern('yyyy');         var ye = dt.format(x).toString();         dt.applyPattern('MM');         var mo = dt.format(x).toString();         dt.applyPattern('dd');         var da = dt.format(x).toString();         dt.applyPattern('H');         var ho = dt.format(x).toString();         dt.applyPattern('m');         var mi = dt.format(x).toString();         dt.applyPattern('ss');         var se = dt.format(x).toString();
        var ret = ye + mo + da + 'T' + ho + mi + se;         //reformatted the datestring values
        return ret;     }
})(current, previous);