Skip to content

How can I get the ID of a record I created?

Answer

Please use "Script".


Overview

If you want to create a new record in the parent table and a linked record in the child table at the same time, and you want to get the ID of the newly created record, you need to implement "Script".
This is a sample of creating a record in the checklist table (child) linked to the record created in the customer table (parent).

Preparations

This FAQ will proceed based on the parent-child table shown below.
Diagram of the parent-child table structure used in this FAQ
The column used for the link in the checklist table (child) is ClassA. Please refer to the following page for how to set up the link.
Manage Table: Editor: Advanced Settings Column: Option: Link | Pleasanter

Operation Procedure

  1. Register the following script in script in the Manage table of the customer table (parent). The execution timing is "Create new".
  2. Create a new record in the customer table.
  3. Go to the checklist table.
  4. Confirm that a record linked to the record created in 2. has been created.

Code Samples

JavaScript
$p.events.before_set_Create = function (args) {
    console.log(args.json);
    craeteCheckListRecored(args.json[0].Value);
}
function craeteCheckListRecored(recordId) {
    $p.apiCreate({
        id: 242196,
        data: {
            Offset: 0,
            Title: 'This record was created when a record was created in the customer table.',
            ClassA: recordId
        },
        done: (data) => {
            console.log('Success!');
            console.log(data);
        },
        fail: (data) => {
            console.log(data);
        }
    })
}

Supplementary Explanation

The part below obtains the record ID when a record is created in the above script.

JavaScript

$p.events.before_set_Create = function (args) {
    console.log(args.json);
    craeteCheckListRecored(args.json[0].Value);
}

The created record information is stored in the response (args) when the "$p.events.before_set_Create" event is executed.
You can check the record ID with "args.json[0].Value".