Syntax for getUserAttributes() Assignment

Answered

Comments

5 comments

  • Official comment
    Avatar
    Laura Theis

    Just a quick syntax correction here - you'll want use Knack.getUserAttributes().email instead.

  • Avatar
    Tyler Shewmake

    Thanks, Laura!

  • Avatar
    Tyler Shewmake

    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

  • Avatar
    Justin

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

    });

  • Avatar
    Marc Dykstra

    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)

Please sign in to leave a comment.