K,
First foray into the Community - looking for simple syntax to move "email" attribute from getUserAttributes() into a field when I create a record.
Here's my code that doesn't work :):
$(document).on('knack-record-create.view_155', function(event, view, record) {
$('input#field_188').attr('value', Knack.getUserAttributes.email)
});
I have to do this because I can't use the normal way of using a connected value (for audit trail purposes).
Thanks!
Justin. You have a couple of scripts that I would like to include in my app. Are you available for hire?
Thank you! (marc@progroupmail.com)
You'll need to send an ajax request.
Something like this:
$(document).on('knack-record-create.view_155', function(event, view, record) {
var email = $('input#field_188').attr('value', Knack.getUserAttributes().email);
var newRecord = record.id;
var data = {field_xxx: email}; // change to the field you want updated
$.ajax({
url: 'https://api.knack.com/v1/pages/scene_XXX/views/view_XXX/records/' + newRecord,
//Change 'url' to an input form on the relevant object
type: 'PUT',
headers: {
'Authorization': Knack.getUserToken(),
'X-Knack-Application-Id': Knack.application_id,
'Content-Type': 'application/json'
},
data: JSON.stringify(data),
success: function(response) {
console.log('Record Updated');
},
error: function(jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
console.log(msg);
}
});
});
So - almost set!
I can now pull the email value - but cannot get it to write to the record during the knack-record-create action. Tried every syntax in line (2) that I can think of... Any suggestions out there?
$(document).on('knack-record-create.view_155', function(event, view, record) {
$('input#field_188').attr('value', Knack.getUserAttributes().email)
});
Thanks - Ty
Just a quick syntax correction here - you'll want use Knack.getUserAttributes().email instead.