Validate if Tempo Account is filled out before adding a Worklog

Work attributes

Script

var ACCOUNT_CUSTOM_FIELD_NAME = "com.tempoplugin.tempo-accounts:accounts.customfield";

function extractCustomFieldId(fieldData, customfieldName) {
    var fields = AJS.$.grep(fieldData, function (field) {
        return (field.custom && field.schema.custom === customfieldName);
    });
    return fields[0].id;
}

function getIssueKey() {
    'use strict';

    function isNullOrEmpty(value) {
        return value === undefined || typeof(value) === 'undefined' || value === null || value === '';
    }

    var funs = [
        function() {
            return AJS.$('#tempo-report-issue').val();
        },
        function() {
            return AJS.$('#tempo-issue-picker-popup').val();
        },
        function() {
            return AJS.$('.tempoIssueKey').val();
        },
        function() {
            return AJS.$('.dialog-panel-body:not(:hidden) [id^="tempo-issue-picker"]').val();
        },
        function() {
            return AJS.$('.dialog-panel-body:not(:hidden) [id^="tempo-issue-picker"] option[selected="selected"]').val();
        },
        function() {
            return AJS.$("select[name='issueKey']").val();
        },
        function() {
            try {
                return JIRA.Issue.getIssueKey();
            } catch(error) {
                console.debug("Could not call JIRA.Issue.getIssueKey, we're probably not in the context of an issue.");
            }
        }
    ]

    for (var i = 0; i < funs.length; i++) {
        var issueKey = funs[i]();

        if(!isNullOrEmpty(issueKey))
            return issueKey;
    }
}


function performBillingKeyCheck() {
    hideWorkLogBillingKeyField()
    var issueKey = getIssueKey();
    var baseUrl = AJS.params.baseURL;
    var url = baseUrl + "/rest/api/2/field";

    AJS.$.getJSON(url, function (data) {
        accountCustomFieldId = extractCustomFieldId(data, ACCOUNT_CUSTOM_FIELD_NAME);
        url = baseUrl + "/rest/api/2/issue/" + issueKey;
        AJS.$.getJSON(url, function (data) {
            account = data.fields[accountCustomFieldId];
            validAccount = (account !== undefined && account !== null);
            AJS.$("[name='_BillingKey_']").prop('checked', validAccount);
            AJS.$("#issue-add-button").removeAttr("disabled")
        });
    });
}

AJS.$(document).ready(function () {
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context, reason) {
        if (reason === JIRA.CONTENT_ADDED_REASON.dialogReady) {

            //log another resets all elements with class resetable
            AJS.$(".field-group.custom_checkboxes.resetable").removeClass("resetable");

            AJS.$("#issue-add-button").attr("disabled", true)

            performBillingKeyCheck();

            AJS.$('[id^="tempo-issue-picker"]:not([id$="-field"])').change(performBillingKeyCheck);
        }
    });

    AJS.$('[id^="tempo-issue-picker"][id$="-field"]').live('blur', performBillingKeyCheck)
});

function hideWorkLogBillingKeyField() {
    AJS.$("#popup_BillingKey_").parent().hide();
    AJS.$("[name='_BillingKey_']").parent().hide();
}

setTimeout(hideWorkLogBillingKeyField, 50);

Demo