Business rules Increment value if contains initial keyword

Xhare
Tera Contributor

Hi, 

 

I have a Business rules that increments the integer values if contains initial keyword, but the problem is it will increment until 8 and goes back to initial number. I have below code. 

 

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

    // Add your code here
    // Product code
    var productCode = current.variables.pmaf_product_code;

    // Country code
    var countryCode = current.variables.pmaf_country_code.iso3166_2.toString();
    gs.info('Country Code = ' + countryCode);

    // Month and Year
    var gdt = new GlideDateTime(current.opened_at);
    var monthValue = gdt.getMonth();
    if (monthValue < 10) {
        monthValue = "0" + monthValue;
    }
    var yearValue = gdt.getYear();
    var monthYear = monthValue.toString() + yearValue.toString();

    // Sequence number
    var initial = productCode + "-" + countryCode + "-";

    var gr = new GlideRecord('sc_item_option_mtom');
    gr.addQuery('sc_item_option.item_option_new.name', 'pmaf_tracking_code');
    gr.addQuery('sc_item_option.value', 'CONTAINS', initial);
    gr.orderByDesc('sc_item_option.value');
    gr.query();

    if (gr.next()) {
        var str = gr.sc_item_option.value.toString();
        var subs = str.substring(15, 18);
        var integ = parseInt(subs);
        var increment = integ + 1;
        var digits = increment.toString().length;
        if (digits == 1) {
            initial = initial + monthYear + "-" + "000" + increment;
            gs.info("digits 1");
        } else if (digits == 2) {
            initial = initial + monthYear + "-" + "00" + increment;
            gs.info("digits 2");
        } else if (digits == 3) {
            initial = initial + monthYear + "-" + "0" + increment;
            gs.info("digits 3");
        } else if (digits == 4) {
            initial = initial + monthYear + "-" + increment;
            gs.info("digits 4");
        }
    } else {
        initial = initial + monthYear + "-" + "0001";

    }

    // Set tracking code value

    current.variables.pmaf_tracking_code = initial;


})(current, previous);
 
Xhare_1-1718826280067.png

 

 
 
3 REPLIES 3

Slava Savitsky
Giga Sage

It seems that the else part of your if (gr.next()) block gets executed at that point. What table is your business rule defined for? And how exactly is it configured?

The table is sc_req_item. It will run before insert

Xhare_0-1718884948222.png

 

There is no loop in your code, how does it "increment until 8 and goes back to initial number"?