画面のボタンからテーブルレコードのフィールド値を変更

Toshiakiki
Tera Contributor

お世話になっております。画面上にある廃止ボタンを押すことで、u_question_choiceテーブルレコードのinstall_statusのフィールド値をRetiredに、有効化ボタンを押すことでinstall_statusのフィールド値をInstalledにしたいです。ポータル画面上では、変更されたように見えましたがテーブルを見ると変更ができていなかったです。

コード面において、修正すべきところや不足しているところは何ですか?

 

 

HTML

 

<div>
<h3>Installed Items</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Value</th>
<th>Question</th>
<th>Order</th>
<th>Install Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in c.data.items | filter:{install_status:'Installed'}">
<td>{{item.u_value}}</td>
<td>{{item.u_question}}</td>
<td>{{item.u_order}}</td>
<td>{{item.install_status}}</td>
<td>
<button class="btn btn-danger btn-block" ng-click="retire(item)">廃止</button>
</td>
</tr>
</tbody>
</table>

<h3>Retired Items</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Value</th>
<th>Question</th>
<th>Order</th>
<th>Install Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in c.data.items | filter:{install_status:'Retired'}">
<td>{{item.u_value}}</td>
<td>{{item.u_question}}</td>
<td>{{item.u_order}}</td>
<td>{{item.install_status}}</td>
<td>
<button class="btn btn-success btn-block" ng-click="activate(item)">有効化</button>
</td>
</tr>
</tbody>
</table>
</div>

 

Serverscript

(function() {
    var gr = new GlideRecord('u_question_choice');
    gr.query();

    var items = [];
    while (gr.next()) {
        items.push({
            sys_id: gr.getValue('sys_id'),
            u_value: gr.getValue('u_value'),
            u_question: gr.getDisplayValue('u_question'),
            u_order: parseInt(gr.getValue('u_order'), 10),
            install_status: gr.getDisplayValue('install_status')
        });
    }

    data.items = items;

    if (input) {
        var app = new GlideRecord('u_question_choice');
        if (app.get(input.target)) {
            app.setValue('install_status',  'Retired' );
            app.update();
        }
    }
})();
 
clientcontroller
 
 
api.controller = function($scope, spModal, spUtil) {
    /* widget controller */
    var c = this;

    c.data.items = c.data.items || [];

    $scope.data.op = '';

    // Function to retire an item and show a modal
    $scope.retire = function(item) {
        item.install_status = 'Retired';

        // Set the selected item to display
        $scope.selectedItem = item;

        // Open the modal
        spModal.open({
            title: 'Item Details',
            message: '<p><strong>Value:</strong> ' + item.u_value + '</p>' +
                     '<p><strong>Question:</strong> ' + item.u_question + '</p>' +
                     '<p><strong>Order:</strong> ' + item.u_order + '</p>' +
                     '<p><strong>Install Status:</strong> ' + item.install_status + '</p>',
            buttons: [
                { label: 'Close', primary: true }
            ]
        }).then(function() {
            // Send update to the server
            spUtil.update($scope, {
                op: 'retired',
                target: item.sys_id
            }).then(function() {
                $scope.data.op = 'retired';
                $scope.data.target = item.sys_id;
            });
        });
    };

    // Function to activate an item and show a modal
    $scope.activate = function(item) {
        item.install_status = 'Installed';

        // Set the selected item to display
        $scope.selectedItem = item;

        // Open the modal
        spModal.open({
            title: 'Item Details',
            message: '<p><strong>Value:</strong> ' + item.u_value + '</p>' +
                     '<p><strong>Question:</strong> ' + item.u_question + '</p>' +
                     '<p><strong>Order:</strong> ' + item.u_order + '</p>' +
                     '<p><strong>Install Status:</strong> ' + item.install_status + '</p>',
            buttons: [
                { label: 'Close', primary: true }
            ]
        }).then(function() {
            // Send update to the server
            spUtil.update($scope, {
                op: 'activated',
                target: item.sys_id
            }).then(function() {
                $scope.data.op = 'activated';
                $scope.data.target = item.sys_id;
            });
        });
    };
};
1件の返信1

iwai
Giga Sage

ClientScriptのspModal.open の引数が間違っていると思います。

Docsでは、spUtil - update(オブジェクト $scope) となっています。

spUtil - クライアント (servicenow.com)

Server side に Inputを渡したい場合、$scope.server.get( [Input Object] ) を使います。

ウィジェット API リファレンス (servicenow.com)

 

Server side Scriptでは、app.setValue('install_status',  'Retired' ); この'Retired'が間違っていると思います、標準では"7"を設定してRetiredにします。標準では install_status は 数字を設定する項目です。

 

このServer sideの処理にinput の op が activated の時の処理と、op が retired の時の処理がありません。今は固定で常にsetValueで固定値を設定しています。

op の値で 設定値を変えるべきかと思います。install_status には activated という設定はありません。Installed はあります、設定値は"1" です。

 

それと Server sideの inputの処理が、items.push より下にあるので、inputの処理で値を変更した物が、items に設定されないため、古い情報でListを表示してしまうと思います。 inputの処理を一番上に上げると良さそうです。