How to get record type of currentrecord reliably i...
# ask-stanley-ai
l
How to get record type of currentrecord reliably in client script? When I take for example vendor bill to edit mode in account 1, currentRecord.get().type returns ‘vendorbill’. Although, when I switch to another account, I get null as the result from same call. What could cause this issue?
c
AI generated answer (using internal Slack threads):
The issue you're experiencing might be due to the fact that the
currentRecord.get().type
method only works reliably in edit mode. If you're in view mode, the
currentRecord
instance will not contain any field values, only its
recordtype
and
id
. In view mode, you can use the
recordtype
and
id
values to use
N/record.load()
or
N/search.lookupFields()
to retrieve the
entity
. Here's a sample code snippet that demonstrates this:
Copy code
javascript
define([
    'N/currentRecord',
    'N/record',
    'N/search'
], function (
    currentRecord,
    record,
    search
) {
    var exports = {};

    function handleButtonClick() {
        let rec = currentRecord.get();
        let recType = rec.type;
        let recId = rec.id;

        if (recType && recId) {
            let loadedRec = record.load({
                type: recType,
                id: recId
            });

            let entity = loadedRec.getValue('entity');
            console.log(entity);
        } else {
            console.log('Record type or id is not available');
        }
    }

    exports.handleButtonClick = handleButtonClick;

    return exports;
});
In this code, we're first getting the current record and its type and id. If these values are available, we're loading the record using
N/record.load()
and then getting the value of the
entity
field. If the record type or id is not available, we're logging a message to the console. Please note that this is just a workaround and the behavior of
currentRecord.get().type
might vary depending on the context and the mode (view or edit) of the record. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.853071749
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.826163113
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.818210423
Please provide your feedback on this answer.