How to stop Process Management using JS Customization?

Hello everyone,

Question / Problem

I am trying to block Process Management from being executed when a specified condition is met by using Process Management API.

If you have any idea about the case, please help!

Current Situation

So the scenario is to get the data from an App and check it with the other App before running PUT API, but for now, I can only block PUT API.

The status is still changing after the condition is met. It seems that I can pop up an error or return an error(false) to the event of process management.

Code / Attempts

(function () {
  "use strict";

  kintone.events.on("app.record.detail.process.proceed", (eventLeave) => {
    const record = eventLeave.record;
    const status = record.Status.value;
    const newStatus = eventLeave.nextStatus.value;

    if (status !== "Approved" && newStatus === "Approved") {
      const record = eventLeave.record;
      const User = "Text_Copy";
      const Req_Date = record.ReqDate.value;
      const Leave = "Leave_Type";
      const Used = record.Total_Used.value;
      const Note = record.Note_Leave.value;

      const query = 'Text_Copy = "' + record[User].value + '" order by $id';
      const fields = "&fields[0]=$id&fields[1]=Text_Copy";

      const A = kintone.api(
        kintone.api.url("/k/v1/records", true) +
          "?app=10&query=" +
          query +
          fields,
        "GET",
        {},
        function (resp) {
          // success
          const recid = resp.records[0].$id.value;
          const TableName = resp.records[0].Text_Copy.value;

          if (record[Leave].value === "Sick") {
            const body = {
              value: {
                Date_Sick: {
                  type: "DATE",
                  value: Req_Date,
                },
                Notes_Sick: {
                  type: "SINGLE_LINE_TEXT",
                  value: Note,
                },
                Number_Leave_Sick: {
                  type: "NUMBER",
                  value: "-" + Used,
                },
              },
            };

            kintone.api(
              kintone.api.url("/k/v1/record", true) + "?app=10&id=" + recid,
              "GET",
              {},
              function (resp) {
                // success
                const record2 = resp.record;
                const sickTableValue = record2.Sick_Table.value;
                sickTableValue.push(body);
                const AvailSick = record2.Current_Avail_Sick.value;

                if (AvailSick - Used < 0) {
                  eventLeave.error = "Insufficient Leave";
                  return eventLeave.error;
                } else {
                  const updateData = {
                    app: 10,
                    id: record2.$id.value,
                    record: {
                      Sick_Table: {
                        value: sickTableValue,
                      },
                    },
                  };

                  kintone.api(
                    kintone.api.url("/k/v1/record", true),
                    "PUT",
                    updateData,
                    function (resp) {
                      console.log("Update Sick Time");
                    }
                  );
                }
              }
            );
          } else if (record[Leave].value === "Personal") {
            const body = {
              value: {
                Date_Person: {
                  type: "DATE",
                  value: Req_Date,
                },
                Notes_Person: {
                  type: "SINGLE_LINE_TEXT",
                  value: Note,
                },
                Number_Leave_Person: {
                  type: "NUMBER",
                  value: "-" + Used,
                },
              },
            };
            kintone.api(
              kintone.api.url("/k/v1/record", true) + "?app=10&id=" + recid,
              "GET",
              {},
              function (resp) {
                // success
                const record2 = resp.record;
                const personTableValue = record2.Person_Table.value;
                const AvailPerson = record2.Current_Avail_Person.value;
                personTableValue.push(body);

                if (AvailPerson - Used < 0) {
                  eventLeave.error = "Insufficient Leave";
                  return eventLeave;
                } else {
                  const updateData = {
                    app: 10,
                    id: record2.$id.value,
                    record: {
                      Person_Table: {
                        value: personTableValue,
                      },
                    },
                  };
                  kintone.api(
                    kintone.api.url("/k/v1/record", true),
                    "PUT",
                    updateData,
                    function (resp) {
                      console.log("Update Person Time");
                    }
                  );
                }
              }
            );
          } else if (record[Leave].value === "Vacation") {
            const body = {
              value: {
                Date_Vac: {
                  type: "DATE",
                  value: Req_Date,
                },
                Notes_Vac: {
                  type: "SINGLE_LINE_TEXT",
                  value: Note,
                },
                Number_Leave_Vac: {
                  type: "NUMBER",
                  value: "-" + Used,
                },
              },
            };
            kintone.api(
              kintone.api.url("/k/v1/record", true) + "?app=10&id=" + recid,
              "GET",
              {},
              function (resp) {
                // success
                const record2 = resp.record;
                const vacTableValue = record2.Vac_Table.value;
                const AvailVac = record2.Current_Avail_Vac.value;
                vacTableValue.push(body);

                if (AvailVac - Used < 0) {
                  eventLeave.error = "Insufficient Leave";
                  return eventLeave;
                } else {
                  const updateData = {
                    app: 10,
                    id: record2.$id.value,
                    record: {
                      Vac_Table: {
                        value: vacTableValue,
                      },
                    },
                  };
                  kintone.api(
                    kintone.api.url("/k/v1/record", true),
                    "PUT",
                    updateData,
                    function (resp) {
                      console.log("Update Vac Time");
                    }
                  );
                }
              }
            );
          }
          return eventLeave;
        }
      );
    }
  });
})();

Error Message

Screenshots are helpful

Desired Outcome / Expected Behavior

Screenshots are helpful

Referenced Resources

List the tutorials, API docs, & help article you read

Hi @Teerayook

In the event app.record.detail.process.proceed when executing an action in process management, you can cancel the action by returning false and displaying an error at the top of the screen.

I was unclear when you would like to block the action, but the code may need to be modified significantly.

For more information, refer to the Proceed Process Event - app.record.detail.process.proceed article’s “Notes” section.

https://kintone.dev/en/docs/kintone/js-api/events/record-details-event/#proceed-process-event

Could you check if it is possible to achieve the desired operation by adding a process that returns false and displaying an error message at the top of the screen?

Also, the kintone.api function is executed asynchronously, so it seems that it would be difficult to implement a process such as returning false in the code you provided.

Using async/await makes it easier to write asynchronous processing.

With the following process, I could cancel the action if a record that met the condition existed.

Example Script

(function () {

  "use strict";

  kintone.events.on("app.record.detail.process.proceed", async (eventLeave) => {
    const record = eventLeave.record;
    const status = record.Status.value;
    const newStatus = eventLeave.nextStatus.value;

    if (status !== "Approved" && newStatus === "Approved") {
      const query = 'Text_Copy = "' + record["User"].value + '" order by $id';
      const fields = "&fields[0]=$id&fields[1]=Text_Copy";
      let resp = await kintone.api(
        kintone.api.url("/k/v1/records", true) + "?app=5897&query=" + query + fields, 'GET', {}
      );

      console.log(resp);

      if (resp.records.length) {
        eventLeave.error = "Canceled because there is a record that meets the condition."
      }
      return eventLeave;
    }
  });
})();

I hope you find this information helpful in modifying the process.

1 Like

Hello @Sean_Tcbn

Thank you for you time.
For the question that you ask, I will try to answer and explain each of them as much as I can.

  1. When to block the action => I would like to block at

So after checking the available days with the day used, if available is not enough then return false and cancel the process.proceed (Do not update or change the status).

  1. Achieve the error with false => Yes, I can but only for a another test code and not this one. So for now I will try to using async/await as you suggested. Because for me the coding is very new and I have a lot to learn.

Anyways, Thanks you for the help. I will try to change the code and test in more detail.

Hello all,

My code is now working but I’m still not sure how’s error occur and what change I did for it to working. But here’s the code that working. (In case it could help others later).

(function() {
  'use strict';
  
   kintone.events.on('app.record.detail.process.proceed', async (eventLeave) => {
    const record = eventLeave.record;
    const status = record.Status.value;
    const newStatus = eventLeave.nextStatus.value;
    const User = 'Text_Copy';
    const Req_Date = record.ReqDate.value;
    const Leave = 'Leave_Type';
    const Used = record.Total_Used.value;
    const Note = record.Note_Leave.value;
    

    if (status !== 'Approved' && newStatus === 'Approved') {
    
    
    const query = 'Text_Copy = "' + record[User].value +'" order by $id';
    const fields = '&fields[0]=$id&fields[1]=Text_Copy';
    

    let resp = await kintone.api(
      kintone.api.url("/k/v1/records", true) + "?app=10&query=" + query + fields, 'GET', {}
    );
    // success
      const recid = resp.records[0].$id.value;
      const TableName = resp.records[0].Text_Copy.value;
      
        
      if (record[Leave].value === 'Sick') { 
        const body = {
      value: {
        "Date_Sick": {
          type: "DATE",
          value: Req_Date
        },
        "Notes_Sick": {
          type: "SINGLE_LINE_TEXT",
          value: Note
        },
        "Number_Leave_Sick": {
          type: "NUMBER",
          value: '-' + Used
        }
      }
    };
    
    let resp = await kintone.api(
      kintone.api.url('/k/v1/record', true) + '?app=10&id='+recid, 'GET', {}
    );
      // success
      const record2 = resp.record;
      const sickTableValue = record2.Sick_Table.value;
      sickTableValue.push(body);
      const AvailSick = record2.Current_Avail_Sick.value;
      
      if (AvailSick-Used < 0) {
        eventLeave.error = 'Insufficient Leave';
        return eventLeave;
      }
      
      else{
           const updateData = {
        "app": 10,
        "id": record2.$id.value,
        "record": {
          "Sick_Table": {
            "value": sickTableValue
          }
        }
      };
     
      kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', updateData, function(resp) {
        console.log('Update Sick Time');
      });
    }
  }
    
    
    
    
    
    else  if (record[Leave].value === 'Personal') { 
        const body = {
      value: {
        "Date_Person": {
          type: "DATE",
          value: Req_Date
        },
        "Notes_Person": {
          type: "SINGLE_LINE_TEXT",
          value: Note
        },
        "Number_Leave_Person": {
          type: "NUMBER",
          value: '-' + Used
        }
      }
    };
    let resp = await kintone.api(
      kintone.api.url('/k/v1/record', true) + '?app=10&id='+recid, 'GET', {}
    );// success
      const record2 = resp.record;
      const personTableValue = record2.Person_Table.value;
      const AvailPerson = record2.Current_Avail_Person.value;
      personTableValue.push(body);
      
      if (AvailPerson-Used < 0) {
        eventLeave.error = 'Insufficient Leave';
        return eventLeave;
      }
      
      else{
      const updateData = {
        "app": 10,
        "id": record2.$id.value,
        "record": {
          "Person_Table": {
            "value": personTableValue
          }
        }
      };
      kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', updateData, function(resp) {
        console.log('Update Person Time');
      });
    }
  }

    else  if (record[Leave].value === 'Vacation') { 
      const body = {
    value: {
      "Date_Vac": {
        type: "DATE",
        value: Req_Date
      },
      "Notes_Vac": {
        type: "SINGLE_LINE_TEXT",
        value: Note
      },
      "Number_Leave_Vac": {
        type: "NUMBER",
        value: '-' + Used
      }
    }
  };
  let resp = await kintone.api(
      kintone.api.url('/k/v1/record', true) + '?app=10&id='+recid, 'GET', {}
    );// success
    const record2 = resp.record;
    const vacTableValue = record2.Vac_Table.value;
    const AvailVac = record2.Current_Avail_Vac.value;
    vacTableValue.push(body);
    
    if (AvailVac-Used < 0) {
        eventLeave.error = 'Insufficient Leave';
        return eventLeave;
      }
      
      else{
    
    const updateData = {
      "app": 10,
      "id": record2.$id.value,
      "record": {
        "Vac_Table": {
          "value": vacTableValue
        }
      }
    };
    kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', updateData, function(resp) {
      console.log('Update Vac Time');
    });
    }
  }
  }
   });
})();




This code will pop-up the error if Avail…-Used is less than 0, so the status will not updated and PUT API will not executed.

If anyone still want to ask about this issue or knows anything about the error, please explain and let me know. I would be appreciated.

Thank you.