# API Example: Simple record copy

**URL:** <https://forums.knack.com/t/api-example-simple-record-copy/10370>\
**Category:** API & Custom Code\
**Created:** [November 4, 2014, 11:04pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370 "2014-11-04T23:04:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://forums.knack.com/u/Nic)\
**Post date:** [November 4, 2014, 11:04pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/1 "2014-11-04T23:04:38Z")

</div>

Add the following snippet to a Details view to easily copy a record

&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

```
// CHANGE: the view key to your own view
$(document).on('knack-view-render.view_12', function (event, view, data) {

```
/* Add all of the fields you want copied and match them with detail data */

var duplicateData = {
  		
    field_57: data.field_57_raw[0].id, // If field is connection, we need to match the ID field
	field_56: data.field_56_raw[0].id,
	field_17: data.field_17,
	field_18: data.field_18,
	field_27: data.field_27,
	field_28: data.field_28,
	field_30: data.field_30,
	field_35: data.field_35,
	field_34: data.field_34,
	field_31: data.field_31_raw
	}

// Add a button to execute the action
$('#' + view.key).prepend('&lt;button id="copy-button"&gt;Duplicate Record&lt;/button&gt;');

// Add the duplicate function to the button we just added
document.getElementById('copy-button').addEventListener('click', function () {
	Knack.showSpinner();
    	$.ajax( {

    		// Change object here to match your record's object #
            // Replace XXX with your own Application ID and API Key found in your Knack Builder
    		url: 'https://api.knackhq.com/v1/objects/object_4/records/',
		    type: 'POST',
		    headers: {'X-Knack-Application-Id': 'XXX', 'X-Knack-REST-API-Key':'XXX'},
		    data: duplicateData,
		    success: function(response) {      
		      alert('Record Copied!');
		      Knack.hideSpinner();
		}
	});
});

```

});
```

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![MarkWaschk18154](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@MarkWaschk18154](https://forums.knack.com/u/MarkWaschk18154)\
**Post date:** [October 31, 2016, 1:35pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/2 "2016-10-31T13:35:47Z")

</div>

Hi Frederico,

Just follow the example given but you would have to loop through multiple time to create multiple records. If you need professional help I can do the work for you, just write me: [mark@appweaverz.com](mailto:mark@appweaverz.com).

Best

Mark

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [October 31, 2016, 1:21pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/3 "2016-10-31T13:21:46Z")

</div>

@Bob, if you can share your solution it would be much appreciated. I am happy to swap code :) &nbsp;

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [October 31, 2016, 1:20pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/4 "2016-10-31T13:20:37Z")

</div>

Hi, I am desperately looking for a similar solution to either copy multiple records or, as Bob mentioned, copy multiple records from a parent to a child object. Any ideas? Thank you

---

<div class="post-metadata">

**Author:** ![EricAlderman](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/ericalderman/32/761_2.png) [@EricAlderman](https://forums.knack.com/u/EricAlderman)\
**Post date:** [September 22, 2016, 2:59am UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/5 "2016-09-22T02:59:31Z")

</div>

Mark, Valerie: thanks!

---

<div class="post-metadata">

**Author:** ![Kat](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/kat/32/2834_2.png) [@Kat](https://forums.knack.com/u/Kat)\
**Post date:** [September 21, 2016, 9:17pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/6 "2016-09-21T21:17:16Z")

</div>

Eric,

Since it appears that your field\_72 can sometimes be blank, including the check statement that Mark has written above should resolve this error. This will add a check to make sure that there is data within your field\_72 and if so, it will be included as a field to copy.

---

<div class="post-metadata">

**Author:** ![MarkWaschk18154](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@MarkWaschk18154](https://forums.knack.com/u/MarkWaschk18154)\
**Post date:** [September 18, 2016, 7:29pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/7 "2016-09-18T19:29:38Z")

</div>

Hi,

Just do a check/set to a variable like so:

if(&nbsp;data.field\_72\_raw &&&nbsp;&nbsp;data.field\_72\_raw[0]){

&nbsp; field\_72 =&nbsp;&nbsp;data.field\_72\_raw[0].id;

}

Cheers,

Mark

---

<div class="post-metadata">

**Author:** ![Ericalderm04061](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/ericalderm04061/32/684_2.png) [@Ericalderm04061](https://forums.knack.com/u/Ericalderm04061)\
**Post date:** [September 17, 2016, 11:19pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/8 "2016-09-17T23:19:23Z")

</div>

Hi Nick,

I'm having trouble with this. &nbsp;It seems to be choking on this reference:

&nbsp; &nbsp; &nbsp; &nbsp; field\_72: data.field\_72\_raw[0].id

This is a connection field, so following the inline instructions I'm using the "\_raw[0].id" syntax. &nbsp;But I'm getting an error in the JS console:

VM2351:5 Uncaught TypeError: Cannot read property '0' of undefined

If I remove this one field reference, the button works and I get a copied record -- but of course it's not connected to the right parent record. &nbsp;

Any ideas?

---

<div class="post-metadata">

**Author:** ![MarkWaschk18154](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@MarkWaschk18154](https://forums.knack.com/u/MarkWaschk18154)\
**Post date:** [March 29, 2016, 2:45am UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/9 "2016-03-29T02:45:21Z")

</div>

OK, great, thanks!

---

<div class="post-metadata">

**Author:** ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://forums.knack.com/u/Nic)\
**Post date:** [March 28, 2016, 6:36pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/10 "2016-03-28T18:36:50Z")

</div>

field\_31 would be any complex field, like an address field or a date / time field. \_raw will give Knack all of the properties it needs to insert the right values where needed.

---

<div class="post-metadata">

**Author:** ![MarkWaschk18154](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@MarkWaschk18154](https://forums.knack.com/u/MarkWaschk18154)\
**Post date:** [March 26, 2016, 6:26pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/11 "2016-03-26T18:26:23Z")

</div>

Hi Nic,

Just a quick clarification, what kind of field is field\_31?

field\_31: data.field\_31\_raw

Thanks,

Mark

---

<div class="post-metadata">

**Author:** ![JeffZearfo11770](https://avatars.discourse-cdn.com/v4/letter/j/aca169/32.png) [@JeffZearfo11770](https://forums.knack.com/u/JeffZearfo11770)\
**Post date:** [February 5, 2015, 6:12am UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/12 "2015-02-05T06:12:59Z")

</div>

Hi Nicolas,

Sorry for asking in advance -- can I get a little more detail about how/where to insert the code?&nbsp;

Your newbie friend,

Jeff

---

<div class="post-metadata">

**Author:** ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://forums.knack.com/u/Nic)\
**Post date:** [December 11, 2014, 12:44am UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/13 "2014-12-11T00:44:52Z")

</div>

Robert,

You're free to post any findings here. If you have any specific questions I can help answer you can send an email to [support@knackhq.com](mailto:support@knackhq.com)

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [December 7, 2014, 6:28am UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/14 "2014-12-07T06:28:10Z")

</div>

Hi Robert, can I suggest sharing it here and others might be able to help as well? I’m certainly interested in how to achieve this.

---

<div class="post-metadata">

**Author:** ![BobLove38845](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@BobLove38845](https://forums.knack.com/u/BobLove38845)\
**Post date:** [December 6, 2014, 6:14pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/15 "2014-12-06T18:14:37Z")

</div>

&nbsp;Nick,

Yes I have found a way.... it is fairly complex and not as easy would hope.... at least the way I have found... Could we email each other to talk more ?

---

<div class="post-metadata">

**Author:** ![Nic](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@Nic](https://forums.knack.com/u/Nic)\
**Post date:** [November 10, 2014, 11:42pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/16 "2014-11-10T23:42:44Z")

</div>

Robert,

I unfortunately haven't had the chance to expand on this since I first posted it -- have you had any luck in your attempts?

---

<div class="post-metadata">

**Author:** ![BobLove38845](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@BobLove38845](https://forums.knack.com/u/BobLove38845)\
**Post date:** [November 5, 2014, 5:04pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/17 "2014-11-05T17:04:45Z")

</div>

Nick,

Been working on a way do copy the record from the parent vs. the detail that would duplicate all the connected records along with the duplicated parent record.

Have you tried that yet?

---

<div class="post-metadata">

**Author:** ![CathyHMcB](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/cathyhmcb/32/1305_2.png) [@CathyHMcB](https://forums.knack.com/u/CathyHMcB)\
**Post date:** [December 22, 2021, 2:08am UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/18 "2021-12-22T02:08:06Z")

</div>

I’m trying to use Nic’s example. I’m Javascript illiterate. So please bear with me.  
I changed view\_12 in the example to my view name, updated my url, ap id and api key.

I can’t get the button to even show on the page’s view.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![KnackPros](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/knackpros/32/807_2.png) [@KnackPros](https://forums.knack.com/u/KnackPros)\
**Post date:** [December 22, 2021, 7:10am UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/19 "2021-12-22T07:10:34Z")

</div>

@CathyHMcB

Did you change the fields to your fields? Everything inside that block of code called `duplicateData` needs to be changed to your fields.

Hope that helps.

Ian  
[Knack Pros](https://knackpros.com)

---

<div class="post-metadata">

**Author:** ![CathyHMcB](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/cathyhmcb/32/1305_2.png) [@CathyHMcB](https://forums.knack.com/u/CathyHMcB)\
**Post date:** [December 22, 2021, 12:11pm UTC](https://forums.knack.com/t/api-example-simple-record-copy/10370/20 "2021-12-22T12:11:22Z")

</div>

I did change all fields I want copied.

[Next page](https://forums.knack.com/t/api-example-simple-record-copy/10370.md?page=2)
