Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.6 kB
3
Indexable
Never
var AccountForm = window.AccountForm || {};
var formContext = {};

(function () {
    // ... (previous code remains unchanged)

    this.checkCodiceFiscale = function () {
        debugger;
        var codiceFiscale = formContext.getAttribute("yod_codicefiscale").getValue();

        // Check if codiceFiscale is not empty before proceeding with checks
        if (codiceFiscale) {
            try {
                codiceFiscale = codiceFiscale.toUpperCase();
                if (codiceFiscale.length !== 16)
                    throw new Error("Il codice fiscale non è valido. Deve avere 16 caratteri alfanumerici.");
                else {
                    if (codiceFiscale.substring(15, 16) !== this.calcolaCodiceControllo(codiceFiscale.substring(0, 15)))
                        throw new Error("Il codice fiscale non è valido.");
                }
            } catch (error) {
                // Display field notification error
                formContext.getControl("yod_codicefiscale").setNotification(error.message, "error");
            }
        }
    };

    this.validateAndCorrectPhoneNumber = function () {
        debugger;
        var phoneNumber = formContext.getAttribute("telephone1").getValue();

        // Check if phoneNumber is not empty before proceeding with checks
        if (phoneNumber) {
            try {
                const cleanedPhoneNumber = this.validateItalianPhoneNumber(phoneNumber);

                if (!/^\d{14}$/.test(cleanedPhoneNumber)) {
                    throw new Error('Numero di telefono non valido. Inserisci tutte le cifre.');
                }
                formContext.getAttribute("telephone1").setValue(cleanedPhoneNumber);
            } catch (error) {
                // Display field notification error
                formContext.getControl("telephone1").setNotification(error.message, "error");
            }
        }
    };

    // ... (rest of the code remains unchanged)

}).call(AccountForm);



this.checkCodiceFiscale = function () {
    debugger;
    var codiceFiscale = formContext.getAttribute("yod_codicefiscale").getValue();

    // Check if codiceFiscale is not empty before proceeding with checks
    if (codiceFiscale) {
        try {
            codiceFiscale = codiceFiscale.toUpperCase();

            if (codiceFiscale.length !== 16) {
                // Display field notification for invalid length
                formContext.getControl("yod_codicefiscale").setNotification("Il codice fiscale deve avere 16 caratteri alfanumerici.", "error");
            } else {
                if (codiceFiscale.substring(15, 16) !== this.calcolaCodiceControllo(codiceFiscale.substring(0, 15))) {
                    // Display field notification for invalid control code
                    formContext.getControl("yod_codicefiscale").setNotification("Il codice fiscale non è valido.", "error");
                } else {
                    // Clear any existing field notifications
                    formContext.getControl("yod_codicefiscale").clearNotification();
                }
            }
        } catch (error) {
            // Display field notification for other errors
            formContext.getControl("yod_codicefiscale").setNotification(error.message, "error");
        }
    }
};




this.onSave = function (executionContext) {
    debugger;
    formContext = executionContext.getFormContext();

    this.checkCodiceFiscale();
    this.validateAndCorrectPhoneNumber();

    // Get event arguments
    var eventArgs = executionContext.getEventArgs();

    // Check if there are any field notifications
    if (formContext.ui.getFormNotifications().length > 0) {
        // Prevent the save operation if there are field notifications
        eventArgs.preventDefault();
    }
};





this.onLoad = function (executionContext) {
    debugger;
    formContext = executionContext.getFormContext();

    formContext.getAttribute("emailaddress1").addOnChange(this.onEmailChange);
    formContext.getAttribute("yod_creacontatto").addOnChange(this.onCreaContattoFieldChange);

    this.visibilitaCreaContatto();

    // Check form type
    formType = formContext.ui.getFormType();
    if (formType === CrmFormType.update) {
        // Only attach onChange events for existing records (update mode)
        formContext.getAttribute("yod_codicefiscale").addOnChange(this.checkCodiceFiscale);
        formContext.getAttribute("telephone1").addOnChange(this.validateAndCorrectPhoneNumber);
    }
};
Leave a Comment