API Example: Simple record copy

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

              

// 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('<button id="copy-button">Duplicate Record</button>');

// 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();
		}
	});
});

});

              

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.

Best

Mark

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

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

Mark, Valerie: thanks!

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.

Hi,

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

if( data.field_72_raw &&  data.field_72_raw[0]){

  field_72 =  data.field_72_raw[0].id;

}


Cheers,


Mark

Hi Nick,

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

        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.  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.  


Any ideas?


OK, great, thanks!

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.

Hi Nic,

Just a quick clarification, what kind of field is field_31?

field_31: data.field_31_raw


Thanks,


Mark

Hi Nicolas,

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

Your newbie friend,

Jeff

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

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.

 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 ?

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?

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?

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?

@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

I did change all fields I want copied.