Forum Discussion

WilliamHunter's avatar
WilliamHunter
Qrew Trainee
10 months ago

Javascript clock in

Struggling just a big here. So I made a javascript codepage for clock in/out, but when I am trying to pass certain parameters in the clock in section of the code it will only let me pass them as a text, although, they are date/time fields or related record fields. Could someone assist me in the formatting here? 

Code Page:

function showModalMessage(message, title, url) {
    $('#modalmessage').empty();
    $('#modaltitle').empty();
    if (title === undefined || title === null) {
        title = 'Alert!';
    }
    $('#modaltitle').append(title);
    $('#modalmessage').append('<p>' + message + '</p>');
    $('#mymodal').modal('show');

    $('#closediv').click(function() {
        window.location.href = url;
    });

}

function closeModalMessage() {
    $('#mymodal').modal('hide');

}

var fieldValue = function(field) {
    return $(field[0]).text();
};

$.when(
    $.getScript('?a=dbpage&pageid=13')

).done(function() {
    $.quickbase.api('API_DoQuery', {
        dbid: dbids._DBID_TIME_ENTRIES,
        query: '{171.EX.1}AND{172.EX.1}',
        clist: '3.174',
        slist: '3',
        fmt: 'structured',
        promise: true

    }).then(function(data) {
        if ($('errcode', data).text() === '0') {
            if ($('record', data).length > 1) {
                window.location.href = URLRoot + dbids._DBID_TIME_ENTRIES + '?a=q&qid=83';

            } else if ($('record', data).length === 1) {
                let rid = fieldValue($('f[id=3]', $('record', data)[0]));
                let curTime = fieldValue($('f[id=174]', $('record', data)[0]));
        

                //console.log(rid);
                //console.log(curTime);

                $.quickbase.api('API_ImportFromCSV', {
                    dbid: dbids._DBID_TIME_ENTRIES,
                    records_csv: rid + ',' + curTime,
                    clist: '3.15',
                    promise: true

                }).then(function(data) {
                    if ($('errcode', data).text() === '0') {
                        $('#mymodal').modal('show');
                        //sorryDialog('You have successfully clocked out at ' + curTime, 'Clocked Out', URLRoot + appID);
                        showModalMessage('You have successfully clocked out at ' + curTime, 'Clocked Out', URLRoot + appID)

                    } else {
                        showModalMessage('There was an error trying to process your request: <BR>' + $('errdetail', data).text(), 'Error', URLRoot + appID);

                    }
                });


            } else {
                //sorryDialog('You have successfully clocked in', 'Clocked In', URLRoot + dbids._DBID_TIME_ENTRIES + '?a=API_AddRecord&apptoken=' + token + '&rdr=' + URLRoot + appID);
                $.quickbase.api('API_AddRecord', {
                    dbid: dbids._DBID_TIME_ENTRIES,
                    field: [
        {
            fid: 25,
            value: 'Work'
        },
    {
                fid: 16,
                value: 'In' // This will set FID 7 to the current date/time value
            },
    {
        fid: 125,
        value: '1'
    },
    {
        fid: 13,
        value: rid.toString()
    }
    ],
                    promise: true

                }).then(function(data) {
                    if ($('errcode', data).text() === '0') {
                        showModalMessage('You have successfully clocked in', 'Clocked In', URLRoot + appID)

                    } else {
                        showModalMessage('There was an error trying to process your request: <BR>' + $('errdetail', data).text(), 'Error', URLRoot + appID)

                    }
                });
                //showModalMessage('You have successfully clocked in', 'Clocked In', URLRoot + dbids._DBID_TIME_ENTRIES + '?a=API_AddRecord&apptoken=' + token + '&rdr=' + URLRoot + appID)
            }
        } else {
            console.log($('errdetail', data).text(), 'Data Retrieval Error', 'false');
        }
    });

Issue Area:

});



------------------------------
William Hunter
------------------------------

2 Replies

  • From reviewing it in a text editor, it looks like you've got a scoping issue where the else {} condition that you're utilizing rid and its causing an issue where rid isn't set and so I'm guessing you're getting an 'rid is not defined' error. I've copied how I see it below with it condensed a little: 

    In the above, you're setting the value of rid at line 44 in the elseif condition after the response of your query and you're checking to see if the # of records is = 1. In this case, rid is contained within the scope of that IF statement from lines 43 - 59. 

    In the event that you don't have any records returning from your query - your code will jump down to line 60 into the else {} condition which has a different scope from lines 60 to 92 - but inside of that scope you're not setting rid anywhere. Basically since you're not processing the else if condition - then rid is never getting set anywhere and so its not being defined. 

    In order to fix it you will need to either remove rid from your addrecord call or define it some other way within the else {} scope so that it can be used. 



    ------------------------------
    Chayce Duncan
    ------------------------------
    • WilliamHunter2's avatar
      WilliamHunter2
      Qrew Member

      Got it working thank you!



      ------------------------------
      William Hunter
      ------------------------------