Bind Other list data while saving the form

Hello, community!
I am trying to fill other list while saving the form,
some how below code is completely work in spRender method, but not in spBeforeSave,

fd.spBeforeSave(function () {

  //

  //Check for myTUMid in calculation list, if it is there than we hve to update record, or else save as new record

  pnp.sp.web.lists.getByTitle("Calculation").items.filter(`MyTUMid eq '${fd.field('Mytumlogin').value}'`).get().then(function (val) {

    console.log(val, "val")

    if (val.length > 0) {

      alert("Updated item ");

      alert(val[0].Id);

      var updatedItem = pnp.sp.web.lists.getByTitle("Calculation").items.getById(val[0].Id).update({

        Title: "Updated Title2"

      }).then(function (result) {

        alert('update done');

      });

    }

    else {

      calculationListData();

    }

  });

  function calculationListData() {

    alert("New item will be added");

    var list = pnp.sp.web.lists.getByTitle("Calculation");

    list.items.add({

      workExternalHoursTotal: fd.field('TotalHoursActiveContracts').value,

      MyTUMid: fd.field('Mytumlogin').value

    }).then(function () {

      console.log("Created!");

    });

  }

});

Is there any problem in there?, but it is work perfectly in render method,
when i debug the code, it also flow in mysterious way.
Additionally how can i use async and await in JS?

Thanks.

Hello @harshp924,

You can use setTimeout() method like this:

function update(id) {
        pnp.sp.web.lists.getByTitle("Calculation").items.getById(id).update({

        Title: "Updated Title2"

      }).then(function (result) {

        alert('update done');

      });

}

function calculationListData() {

    alert("New item will be added");

    var list = pnp.sp.web.lists.getByTitle("Calculation");

    list.items.add({

      workExternalHoursTotal: fd.field('TotalHoursActiveContracts').value,

      MyTUMid: fd.field('Mytumlogin').value

    }).then(function () {

      console.log("Created!");

    });

  }

fd.spBeforeSave(function () {

  //

  //Check for myTUMid in calculation list, if it is there than we hve to update record, or else save as new record

  pnp.sp.web.lists.getByTitle("Calculation").items.filter(`MyTUMid eq '${fd.field('Mytumlogin').value}'`).get().then(function (val) {

    console.log(val, "val")
    var id = (val[0].Id);

    if (id) {

      alert("Updated item ");

      alert(id);

      setTimeout(update(id), 1000);
    }

    else {

      setTimeout(calculationListData(), 3000);

    }

  });
});

Hello, @mnikitina,
I try above method, but it is not working in my case :worried:

Any other way i can use the above?
Thanks

Hello, @mnikitina
Can you tell me how can use async and await in JS code.
While i am trying to use, it shows syntax error.

Thanks

Hello, @mnikitina
I am trying to update data in SharePoint online list with fetch call,
For update into list item, i need to get Request-digest,
my problem is when i try to get request digest, function is not waiting for request coming back, so post operation is not successfully execute, is there any other way i can get request digest and update in list item..?

Thanks.

Hello @harshp924,

Please try to add return statement and change the filter query "MyTUMid eq '" + fd.field('Mytumlogin').value + "'" like this:

function update(id) {
        pnp.sp.web.lists.getByTitle("Calculation").items.getById(id).update({

        Title: "Updated Title2"

      }).then(function (result) {

        alert('update done');

      });

}

function calculationListData() {

    alert("New item will be added");

    var list = pnp.sp.web.lists.getByTitle("Calculation");

    list.items.add({

      workExternalHoursTotal: fd.field('TotalHoursActiveContracts').value,

      MyTUMid: fd.field('Mytumlogin').value

    }).then(function () {

      console.log("Created!");

    });

  }

fd.spBeforeSave(function () {

  //

  //Check for myTUMid in calculation list, if it is there than we hve to update record, or else save as new record

  return pnp.sp.web.lists.getByTitle("Calculation").items.filter("MyTUMid eq '" + fd.field('Mytumlogin').value + "'").get().then(function (val) {

    console.log(val, "val")
    var id = (val[0].Id);

    if (id) {

      alert("Updated item ");

      alert(id);

      setTimeout(update(id), 1000);
    }

    else {

      setTimeout(calculationListData(), 1000);

    }

  });
});

If that doesn't work, please debug the code outside of spBeforeSave event.