Using beforeSave()

Hello,

The thing I want to achieve is before the form submission I want to refresh the values of SPDataTable1 check if the control SPDataTable1 has a certain text value in a field and based on it do 2 things if the value is equal to "Text" close the form and display alert else save the form and redirect to URL.

Here is the code that I came up with:

spBeforeSave(function() {



fd.control('SPDataTable1').refresh();

var preluat =  fd.control('SPDataTable1').filter =
"<Eq><FieldRef Name='Eveniment'/><Value Type='Text'>Cererea a fost preluată</Value></Eq>";




if (preluat == "Cererea a fost preluată") {
    //  Close form and display Alert 
    fd.spClosed(function() {
        alert("Some text");
    });
} else {
    // Save form and redirect to URL
    fd.spSaved(function(result) {
        var itemId = result.Id;
        result.RedirectUrl =
            "http://intranet.maib.local/sites/help/Workflows/preluareElement/preluareElement.aspx?List={7a9edd38-3f18-4bef-99f6-062067196fa2}&ID=" + itemId + "72&ItemGuid={BE628B7D-57E1-44C0-8377-A389786A17E7}&TemplateID={ed452887-7ecf-4a7b-9d9f-de7b1b8eb19e}&Source=http%3A%2F%2Fintranet%2Emaib%2Elocal%2Fsites%2Fhelp%2FLists%2FCerere%2520de%2520suport%2FAllItems%2Easpx%3Fweb%3D1";
    });        
    
}

});

Can someone please tell me what am I doing wrong ?

Thank you !

Hello @Vasilii_Burca,

The spBeforeSave event occurs when the process of saving the form starts. Once the process of saving has started, you cannot abort saving and just close the form.

You can create a custom button to achieve what you need.

Add a button to the form and use the below code in button's properties >> General >> Click
image

 //filter list or library control    
fd.control('SPDataTable1').filter = "<Eq><FieldRef Name='Eveniment'/><Value Type='Text'>Cererea a fost preluata</Value></Eq>";
fd.control('SPDataTable1').refresh();


//close the form if there are records in the list or library control
if (fd.control('SPDataTable1').widget.dataItems().length > 0) {
    //  Close form and display Alert 
    fd.spClosed(function() {
        alert("Some text");
    });

    return fd.close();

} else {
    // Save form and redirect to URL
    fd.spSaved(function(result) {
        var itemId = result.Id;
        result.RedirectUrl =
            "http://intranet.maib.local/sites/help/Workflows/preluareElement/preluareElement.aspx?List={7a9edd38-3f18-4bef-99f6-062067196fa2}&ID=" + itemId + "72&ItemGuid={BE628B7D-57E1-44C0-8377-A389786A17E7}&TemplateID={ed452887-7ecf-4a7b-9d9f-de7b1b8eb19e}&Source=http%3A%2F%2Fintranet%2Emaib%2Elocal%2Fsites%2Fhelp%2FLists%2FCerere%2520de%2520suport%2FAllItems%2Easpx%3Fweb%3D1";
    });        
    return fd.save();
}

Hello @mnikitina,

Thank you for your outstanding help and support.

The thing is that I need to compare the output of fd.control('SPDataTable1').filter =, with a predefined value and based on that execute the steps.

Now it works only if the fd.control.('SPDataTable1') has values or not.

How can I make it compare if fd.control('SPDataTable1').filter.value = "Cererea a fost preluata" fd.close(), else fd.save() ?

Thank you.

Hello @Vasilii_Burca,

Once you filter the view, List or Library control will display only records with Eveniment field equals 'Cererea a fost preluata'.

So if the control has at list one record, the form will be closed, and if the control has no records, the form will be saved.

Hello @mnikitina,

Here is my code:

        //filter list or library control   

    fd.control('SPDataTable1').filter = "<Eq><FieldRef Name='Eveniment'/><Value Type='Text'>Please do not close</Value></Eq>";
    fd.control('SPDataTable1').refresh();


//close the form if there are records in the list or library control
if (fd.control('SPDataTable1').widget.dataItems().length > 0) {
    //  Close form and display Alert 
    fd.spClosed(function() {
        alert("Some text");
    });

    return fd.close();

} else {
    // Save form and redirect to URL
    fd.spSaved(function(result) {
        var itemId = result.Id;
        result.RedirectUrl =
            "http://intranet.maib.local/sites/help/Workflows/preluareElement/preluareElement.aspx?List={7a9edd38-3f18-4bef-99f6-062067196fa2}&ID=" + itemId + "72&ItemGuid={BE628B7D-57E1-44C0-8377-A389786A17E7}&TemplateID={ed452887-7ecf-4a7b-9d9f-de7b1b8eb19e}&Source=http%3A%2F%2Fintranet%2Emaib%2Elocal%2Fsites%2Fhelp%2FLists%2FCerere%2520de%2520suport%2FAllItems%2Easpx%3Fweb%3D1";
    
        });        
    

    return fd.save();
}

and here is a screen shot of the form where you can see that the table control does not have the value

But everytime the first condition is met and it gives me an alert.
I even tryied to put a invented value in the filter still only fd.close().

What am i doing wrong, can you please tell me ?
Thank you !

@Vasilii_Burca,

I've updated the code, please try it out.

//filter list or library control   

fd.control('SPDataTable1').filter = "<Eq><FieldRef Name='Eveniment'/><Value Type='Text'>Please do not close</Value></Eq>";
fd.control('SPDataTable1').refresh();

function SaveOrClose() {
//close the form if there are records in the list or library control
if (fd.control('SPDataTable1').widget.dataItems().length > 0) {
    //  Close form and display Alert 
    fd.spClosed(function() {
        alert("Some text");
    });

    return fd.close();

} else {
    // Save form and redirect to URL
    fd.spSaved(function(result) {
        var itemId = result.Id;
        result.RedirectUrl =
            "http://intranet.maib.local/sites/help/Workflows/preluareElement/preluareElement.aspx?List={7a9edd38-3f18-4bef-99f6-062067196fa2}&ID=" + itemId + "72&ItemGuid={BE628B7D-57E1-44C0-8377-A389786A17E7}&TemplateID={ed452887-7ecf-4a7b-9d9f-de7b1b8eb19e}&Source=http%3A%2F%2Fintranet%2Emaib%2Elocal%2Fsites%2Fhelp%2FLists%2FCerere%2520de%2520suport%2FAllItems%2Easpx%3Fweb%3D1";
    
        });        
    

    return fd.save();
}
}
setTimeout(SaveOrClose, 1000);
1 Like

Hello @mnikitina,

It actually adds values to the list, but it does not respect the lookup filtering that is done in the fd.control('SPDataTable1') at Settings > Data Source Editor. Do I need to add another lookup filtering in the Button OnClick code?

If yes can you please give me a sample because I am very inexperienced at this.

Here is a screenshot of DataTable Settings
Screenshot_2
Thank you!

Hello @Vasilii_Burca,

Ok, to keep lookup filtering you need to add your filtering conditions to the existing ones. Please find more information in this article.

I've updated the code, please see below.

var filter = "<And>"
//add existing filter value
filter += fd.control('SPDataTable1').filter;
//add your own filtering conditions
filter +=  "<Eq><FieldRef Name='Eveniment'/><Value Type='Text'>Please do not close</Value></Eq>";
filter += "</And>"
//filter list or library control   

fd.control('SPDataTable1').filter = filter;
fd.control('SPDataTable1').refresh();

function SaveOrClose() {
//close the form if there are records in the list or library control
if (fd.control('SPDataTable1').widget.dataItems().length > 0) {
    //  Close form and display Alert 
    fd.spClosed(function() {
        alert("Some text");
    });

    return fd.close();

} else {
    // Save form and redirect to URL
    fd.spSaved(function(result) {
        var itemId = result.Id;
        result.RedirectUrl =
            "http://intranet.maib.local/sites/help/Workflows/preluareElement/preluareElement.aspx?List={7a9edd38-3f18-4bef-99f6-062067196fa2}&ID=" + itemId + "72&ItemGuid={BE628B7D-57E1-44C0-8377-A389786A17E7}&TemplateID={ed452887-7ecf-4a7b-9d9f-de7b1b8eb19e}&Source=http%3A%2F%2Fintranet%2Emaib%2Elocal%2Fsites%2Fhelp%2FLists%2FCerere%2520de%2520suport%2FAllItems%2Easpx%3Fweb%3D1";
    
        });        
    

    return fd.save();
}
}
setTimeout(SaveOrClose, 1000);
1 Like

Thank you very much it worked as a charm !!!

1 Like