Prevent fields from clearing whats filled in when saving and another problem

Hello,

is there a way how to disable clearing fields values when saving? Thank you.

when saving it throws errors of value not being available.

Also all forms throws this error when opening
image

and im not enabling any field
// Code

fd.spRendered(function() {
try {
    fd.field('Castka').disabled = true;
    fd.field('Oblast').disabled = true;
    fd.field('_Status').disabled = true;
    graph.me.select("companyName").get().then(response => {
        try {
            pnp.sp.web.lists.getByTitle("Seznam společností MAPO Group").items.select("Id").filter("substringof('" + response.companyName + "',Title)").getAll().then(function(allItems) {
                if (allItems.length > 0) {
                    fd.field('Spolecnost').value = allItems[0].ID;
                    fd.field('Spolecnost').reloadValue();
                }
            });
        } catch (error) {
            console.error("Error in graph.me response processing:", error);
            logErrorToHelpdesk("Error v graph.me response processing", error.message);
            errorMsg=error.message
        }
    }).catch(error => {
        console.error("Error fetching user profile:", error);
        logErrorToHelpdesk("Error fetching user profile", error.message);
        errorMsg=error.message
    });

    pnp.sp.web.lists.getByTitle("HelpdeskDefaultNastaveni").items.select("Resitel/Id","Resitel/Name","Skupina/Title","Skupina/Name","SkupinaRead/Name","Title","Oblast/ID","Oblast/Title").expand("Resitel","Skupina","SkupinaRead","Oblast").filter("Title eq '" + fd.field('ContentType').value + "'").getAll().then(function(allItems) {
        try {
            fd.field('AssignedTo').value = allItems[0].Resitel.Name;
            for (var i = 0; i < allItems[0].Skupina.length; i++) {
                Skupina += allItems[0].Skupina[i].Name + ',';
            }
            for (var a = 0; a < allItems[0].SkupinaRead.length; a++) {
                SkupinaRead += allItems[0].SkupinaRead[a].Name + ',';
            }
            fd.field('Oblast').value = allItems[0].Oblast.ID;
            Oblast = allItems[0].Oblast.Title;
        } catch (error) {
            console.error("Error processing HelpdeskDefaultNastaveni items:", error);
            logErrorToHelpdesk("Error processing HelpdeskDefaultNastaveni items", error.message);
            errorMsg=error.message
        }
    });

    graph.me().then(function(props) {
        try {
            IDzakladatel = props.userPrincipalName;
        } catch (error) {
            console.error("Error fetching user principal name:", error);
            logErrorToHelpdesk("Error fetching user principal name", error.message);
            errorMsg=error.message
        }
    });
} catch (error) {
    console.error("Error v fd.spRendered:", error);
    logErrorToHelpdesk("Error in fd.spRendered", error.message);
    hasRenderErrors = true;
    errorMsg=error.message
}

});

Sometimes it fails to fetch i dont know why it happens

//Code

fd.spSaved(function(result) {
result.RedirectUrl = null;
try {
    var listId = fd.spFormCtx.ListAttributes.Id;
    var itemId = result.Id;
    fd.toolbar.buttons = fd.toolbar.buttons.filter(button => button.id !== "SAVE");
    document.getElementsByClassName('fd-grid container-fluid')[0].style.display = "none";
    var linkEdit = "https://xxxx.xxxxxxxxx.com/sites/Helpdesk/_layouts/15/listform.aspx?PageType=6&ListId=" + listId + "&ID=" + itemId;
    var url = 'link';
    url += '&Skupina=' + Skupina + 
           '&Id=' + result.Id + 
           '&IDzakladatel=' + IDzakladatel + 
           '&SkupinaRead=' + SkupinaRead;

    // Přidání do seznamu Oblast
    pnp.sp.web.lists.getByTitle(Oblast).items.add({
        Title: result.Id.toString(),
    }).then(function(r) {
        if (!r || !r.data || !r.data.ID) {
            throw new Error("❌ Přidání do seznamu '" + Oblast + "' selhalo – žádné ID ve výsledku.");
        }

        // Aktualizace v seznamu Helpdesk
        return pnp.sp.web.lists.getByTitle("Helpdesk").items.getById(result.Id).update({
            HelpDeskId: "HTP-" + pad(r.data.ID, 6)
        });
    }).then(function(updateResult) {
        if (!updateResult || !updateResult.data) {
            throw new Error("❌ Aktualizace položky v seznamu 'Helpdesk' selhala.");
        }

        // Volání API
        return fetch(url);
    }).then(function(response) {
        if (!response.ok) {
            throw new Error('API request failed. Status Code: ' + response.status);
        }

        // Redirect po úspěchu
        fd.close()
    }).catch(function(error) {
        logErrorToHelpdesk("Chyba v post-save operacích", error.toString());
        throw error;
    });
} catch (error) {
    logErrorToHelpdesk("Chyba v save handleru", error.toString());
    throw error;
}

});

Hello @Patrik,

What SharePoint version are you using? Online or On-Premises?

When do fields clear? When you are saving a new item, or when editing?
Please try to comment out all the custom code and check if the issue persists.

Hello Margo,

SP Online, fields clear when saving.

And after commenting out line where i disabled field that is lookup it didnt return error, but i need to keep this field not editable for user and based on content type add value to this field.

//Code

fd.field('Oblast').disabled=true;//Lookup Field

@Patrik,

When you comment out all the code, does it fix the issue with clearing fields?

Lookup is a complex field that requires time to load. Wrap the code inside the ready event like so to avoid errors:

fd.field('Oblast').ready().then(function(field) {
    field.disabled = true;
});

yep i have done it already just and it works, just didnt let u know, so now is there a way how to keep values in fields when saving new form ?

@Patrik,

Could you please describe your case?

Should the user be able to create a new item with the same values again? If so, saving form data to local storage and populating the form after submission will work:

If the user should be able to save and then edit the same item, you should use a redirect:

i meant if im able to disable function that clear all fields when user press save if not its ok we can close this ticket.

@Patrik,

Yes, this is by designed and can't be disabled. But there are workarounds I shared that might work for your case.