The code on SPBeforesave function is not finishing the work before closing the form

I have problem that my code is not completing the work on spBeforesave function before closing the form.
I am sure that there is no problem in my code, unless i am missing something!
The goal of this code is to create and set a unique custom id

I think the problem is that it needs more time in order to complete the task and set the values
before contiue to the main list.

what i should do in this case i tried the set timeout function but did not resolve my issue :
setTimeout(xxxxxxxxxxxxxxxx, 500);

this is my code below:

fd.spBeforeSave(function () {
        fd.field("BaptismRecordParishNameIdFk").ready().then(function () {
            return fd.field("BaptismRecordTypeIdFk").ready();
        }).then(function () {
            // Filter by Parish id and year number
            var fdParishCurrentID = fd.field('BaptismRecordParishNameIdFk').value.LookupId ? fd.field('BaptismRecordParishNameIdFk').value.LookupId : fd.field('BaptismRecordParishNameIdFk').value;
            var fdRecordYear = fd.field('BaptismRecordYear').value;
            var fdRecordCode = fd.field("BaptismRecordTypeIdFk").value.TransactionCode;
            var filter = "BaptismRecordParishNameIdFk/Id eq " + fdParishCurrentID + " and BaptismRecordYear eq " + fdRecordYear;
            var newNumber = 0;

            pnp.sp.web.lists.getByTitle("BaptismList")"Id", "Title", "BaptismIDYearlyNumber").filter(filter).top(1).orderBy("BaptismIDYearlyNumber", false).get().then((items) => {
                var lastNumber = 0;
                if (items.length > 0) {
                    lastNumber = items[0].BaptismIDYearlyNumber;
                } else {
                    lastNumber = 0;
                //Get New yearly number
                newNumber = lastNumber + 1;
                //Set Yearly Number
                fd.field('BaptismIDYearlyNumber').value = newNumber;
                //Set Code
                fd.field('BaptismRecordCode').value = fdRecordCode;
                //Set Baptism New Sys ID
                fd.field('BaptismRecordParishFullID').value = fdRecordCode + " " + fdRecordYear + "/" + newNumber;

Hello @gkhadra,

Please try out the approach described in this post:

1 Like

Dear @mnikitina,
Thank you again and again for your support and for this helpful topic.

I still have a problem, i need to get multiple values from multiple controls already used on the form filled data by the end user.
It is necessary to:

  • Set the PNP Query filter
  • To concatenate multiple values and the PNPresult.
    How to get in the approach you mentioned above these values not only the: item ID (result.Id)
    i have lookup id values, text and number field values.
    Thank you


you can call PnP js function to filter the list values by the specific parameter and to get multiple values.