Skip to content
Snippets Groups Projects
onValidation.js 997 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 maskingHelper = new SqlMaskingUtils();
        var newCodeNumber = newSelect(maskingHelper.max("AB_ATTRIBUTE.SORTING"))
                        .from("AB_ATTRIBUTE")
                        .where("AB_ATTRIBUTE.ATTRIBUTE_PARENT_ID", parentId)
                        .cell();
                        
        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);
    }
}