Skip to content
Snippets Groups Projects
onValidation.js 969 B
import("system.translate");
import("system.db");
import("system.vars");
import("system.text");
import("system.neon");
import("Sql_lib");

//TODO: this should no happen in onValidation; waiting for #1032668
if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
{
    var parentId = vars.get("$field.ATTRIBUTE_PARENT_ID");
    if (parentId)
    {
        var cond = SqlCondition.begin().andPrepare("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", parentId);
        var maskingHelper = new SqlMaskingUtils();
        var newCodeNumber = db.cell(cond.buildSql("select " + maskingHelper.max("AB_ATTRIBUTE.SORTING") + " from AB_ATTRIBUTE", "1 = 2"));
        newCodeNumber = Number(newCodeNumber);//if no number exists till no, start value will be 1 (due to: ++0)
        if (isNaN(newCodeNumber))
            throw new TypeError(translate.text("The code number is not a valid number."));
        neon.setFieldValue("$field.SORTING", ++newCodeNumber);
    }
}