# Save Knack Record ID

![The Knack Record ID short text field for the parent object](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-JpXxootHDhgyQJ9F%2F-Ma-Ni6sEJ3cjZN6Whn9%2Fimage.png?alt=media\&token=ece9976b-33e3-46d0-bfa0-20181471e242)

![The Knack Record ID text formula field for the child object](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-JpXxootHDhgyQJ9F%2F-Ma-OlWHVgKqPBkQcuoF%2Fimage.png?alt=media\&token=983218d1-f879-4b1c-936a-154e8382a955)

![The Save Record ID Page separate from the rest of the app](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-JpXxootHDhgyQJ9F%2F-Ma-Nx-B2TUe8jQuJG10%2Fimage.png?alt=media\&token=8bb5b1b9-58ee-48c6-b7bf-e58c2077a3c1)

### The JS

In order to the save the Knack Record ID, we use a [View Based PUT with the Knack API](https://docs.knack.com/docs/view-based-put).

```
/******************************/
/**** Save Knack Record ID ****/
/******************************/
// Function to Save Record ID
function saveRecordId(recordId) {
  $.ajax({
  // Scene/View of Second Form
    url: "https://api.knack.com/v1/pages/scene_269/views/view_740/records/" + recordId,
    type: "PUT", 
    headers: {
      "X-Knack-Application-Id": Knack.application_id,
      "X-Knack-REST-API-Key": `knack`,
      "Authorization": Knack.getUserToken(),
      "ContentType": "application/json"
    },
    data: {
    // Store Record ID in Knack Record ID field on TIA Request object
      field_824 : recordId,
    },
    tryCount: 0,
    retryLimit: 3,
    success: function(response) {
    // Success Message in Console
      console.log("Captured Record ID");
      Knack.hideSpinner();
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
      this.tryCount++;
      let tryCount = this.tryCount, retryLimit = this.retryLimit, seconds; 
      if (tryCount <= retryLimit) { //try again
        switch(tryCount) {
          case 1:
          case 2: seconds = 5; break;
          case 3: seconds = 10; break; }
        let timeout = seconds * 1000;
        console.log("Error: " + XMLHttpRequest.status + " " + XMLHttpRequest.statusText + "\nRetry Count: " + tryCount + "\nRetrying in " + seconds + " seconds")
        let ajaxObject = this;
        window.setTimeout(function(){
          $.ajax(ajaxObject);
        }, timeout);
      } else {
      // Failure Message in Console
        console.log("Failed to Capture Record ID");
      }
    }
  });
}

// Listen for record creation (Application Submit / First Form)
$(document).on('knack-record-create.view_393', function(event, view, record) {
  const recordId = record.id;
  console.log('CREATE')
  saveRecordId(recordId);
});

// This is the Save Record ID / Second Form.
// This removes the view from HTML upon rendering to prevent data manipulation.
$(document).on('knack-view-render.view_740', function (event, view, record) {
  $('#' + view.key).remove(); 
});
```

### The CSS

None needed 😎

### How to Implement

{% tabs %}
{% tab title="1️⃣" %}
Create a short text field for the parent object to capture the Knack Record ID

![](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-QuDWYbWUF4ZuOTOT%2F-Ma-U0osT-fvCrHfH76t%2Fimage.png?alt=media\&token=9535db20-e2f2-4b30-973d-c18d7dc159c6)
{% endtab %}

{% tab title="2️⃣" %}
Create a text formula field on the child object that is set to the Knack Record ID field on the parent object

![](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-QuDWYbWUF4ZuOTOT%2F-Ma-UcAZhI1xc-ZSQ1rC%2Fimage.png?alt=media\&token=4eaccc40-66dd-4ca7-8840-f0066ec4acc5)
{% endtab %}

{% tab title="3️⃣" %}
Create a new page to Save the Record ID

![](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-QuDWYbWUF4ZuOTOT%2F-Ma-XdbXXQ-yuhN72RAm%2Fimage.png?alt=media\&token=5873b8da-0f93-49c5-a84a-c4dc9bcc3c32)

![](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-QuDWYbWUF4ZuOTOT%2F-Ma-VZHPCEyPdvFWqty4%2Fimage.png?alt=media\&token=7991e8f5-226e-4e54-9937-4544a32b981f)

Make sure the page settings do NOT have the page included in the page menu

![](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-QuDWYbWUF4ZuOTOT%2F-Ma-W4qxc88Cm-Yod0HD%2Fimage.png?alt=media\&token=4a49645f-75a2-4046-abf6-fcd4c6353c26)

Add a parent object form view to the page that will only show the Knack Record ID field. No additional form rules are needed.

![](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-Ma-QuDWYbWUF4ZuOTOT%2F-Ma-XPs1dSPqLalK8dxj%2Fimage.png?alt=media\&token=69c013cc-7f1f-4546-b40d-956ec9a3a34c)
{% endtab %}

{% tab title="4️⃣" %}
Establish a form submit page or multi-page application where the parent object is created if there is not one already. For example, in the TDS TIA Module there is a parent object multi-page application. The TIA Applicant Information is the first page in this application where the parent object is created. This is the form view that will save the Knack Record ID.

![](https://3087753818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzNSSXajDFpzOv2IhIv%2F-MfYgb6rzrCymw6KF3Jd%2F-MfYhFhySSGvKSCkGjm9%2Fimage.png?alt=media\&token=b1e1d73e-87f4-45f4-86ba-11aee60f4b26)
{% endtab %}

{% tab title="5️⃣" %}
Update the JS for data section where the Record ID is stored with the correct Field ID

```
data: {
    // Store Record ID in Knack Record ID field on TIA Request object
      field_824 : recordId,
    },
```

{% endtab %}

{% tab title="6️⃣" %}
Update the JS handlers for both the first and second forms with the correct View IDs

```
// Listen for record creation (Application Submit / First Form)
$(document).on('knack-record-create.view_393', function(event, view, record) {
  const recordId = record.id;
  console.log('CREATE')
  saveRecordId(recordId);
});
```

```
// This is the Save Record ID / Second Form
// This removes the view from HTML upon rendering to prevent data manipulation.
$(document).on('knack-view-render.view_740', function (event, view, record) {
  $('#' + view.key).remove(); 
});
```

{% endtab %}

{% tab title="7️⃣" %}
Lastly, the url in the function will need to be updated with the Scene and View IDs of the Save Record ID page

```
url: "https://api.knack.com/v1/pages/scene_269/views/view_740/records/" + recordId,
```

{% endtab %}
{% endtabs %}
