Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

現在動作している環境を判定する方法

clickplace
Tera Contributor

今、動いている環境がDEVなのかSTGなのかPRODなのかを判定して処理を行いたいという場面が出てきましたのでメモします

■サーバーサイド
・呼び出し側

//環境判定
var res = new getInstanceName();
var instance_name = res[0];
if (instance_name == 'sampledev') {
    //DEV環境の処理
} else if (instance_name == 'samplestg') {
    //STG環境の処理
} else if (instance_name == 'sampleprod') {
    //PROD環境の処理
} else {
    //エラー処理
}

・呼び出される側(スクリプトインクルード)

function getInstanceName() {
    var res = [];
    var instance_name = gs.getProperty('instance_name');
    res[0] = instance_name;
    return res;
}

 

■クライアントサイド
・呼び出し側

function onLoad() {
    //環境判定
    var ajax = new GlideAjax('ajaxGetInstanceName');
    ajax.addParam('sysparm_name', 'get');
    ajax.getXMLAnswer(callback);
}
function callback(response) {
    var instance_name = response;
    if (instance_name == 'sampledev') {
        //DEV環境の処理
    } else if (instance_name == 'samplestg') {
        //STG環境の処理
    } else if (instance_name == 'sampleprod') {
        //PROD環境の処理
    } else {
        //エラー処理
    }
}

・呼び出される側(スクリプトインクルード)

var ajaxGetInstanceName = Class.create();
    ajaxGetInstanceName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
        get: function() {
        var res = gs.getProperty('instance_name');
        return res;
    },
    type: 'ajaxGetInstanceName'
});
0件の返信0