Need to update location field in Asset table as per the To stockroom enter in transfer_order table.

Prasnajeet1
Giga Guru

Hi All

Need your support in the scripting for the below mentioned requirement.

For example: User has created a record in transfer order table like "From Stockroom =A" and "To Stockroom=B". Now an entry got created in alm_transfer_order_line table and also a "Ship task" got created. When user closed this ship task then Asset state gets change to "In Transit" in alm_hardware_table. Now my requirement is when Asset state is "In Transit" in alm_hardware table then I also need to update the "Location" field in the alm_hardware table as per the "To Stockroom" value from transfer order table. In this case user has enter the "to Stockroom" value as 'B' so my script should update the location field value as 'B' in the alm_hardware table when the assdet state is "In Transit".

 

Please help me in the script part and logic please.

Thank you in advanced. Please support.

7 REPLIES 7

Hi Ankur

Below is one existing BR which is updating the Asset state when the task is closed. This is a Before BR. In the first switch case it is updating the Asset state based on task closed status. I am not getting the idea to update the location on Asset form. 

 

Hope now it is will help you to understand the requirement and help on this.

 

Script:

var asset = new GlideRecord('alm_asset');
asset.addQuery('sys_id', current.asset);
asset.query();
if (asset.next()) {
    var updateAsset = true;
    var assetIsDefective = false;
    var assetIsPreallocated = false;
    if ('defective' === asset.getValue('substatus'))
        assetIsDefective = true; // Preserve existing status of 'defective'
    if ('pre_allocated' === asset.getValue('substatus'))
        assetIsPreallocated = true; // Preserve existing status of preallocated
 
    // Update asset when TOL is created (Stage = 'draft')
    if (current.stage.toString() === 'draft') {
        // install_status = 6 = In stock
        asset.install_status = '6';
        asset.substatus = 'pending_transfer';
        asset.active_to = true;
    }
 
    // For further updates on asset, check the state of task to auto update asset
    var taskGr = new GlideRecord('alm_transfer_order_line_task');
    taskGr.addQuery('transfer_order_line', current.sys_id);
    taskGr.addQuery('state', 'IN', '3,7');
    taskGr.orderByDesc('order');
    taskGr.setLimit(1);
    taskGr.query();
    if (taskGr.next()) {
        var stateOfTask = taskGr.getValue('state');
        // state = 7 = Closed Skipped
        // Do not auto update the asset if the last task was closed skipped
        if (stateOfTask !== '7') {
            //Update the asset when the last task closed was completed successfully
            switch (current.stage.toString()) {
                case 'requested':
                    // install_status = 6 = In stock
                    asset.install_status = '9';
                    asset.substatus = '';
                    asset.active_to = true;
                    // Update asset with user information
                    if (asset.sys_class_name.toString() !== 'alm_consumable') {
                        setAssetReservedFor();
                    }
                    break;
 
                case 'shipment_preparation':
                    // install_status = 6 = In stock
                    asset.install_status = '6';
                    asset.substatus = 'pending_transfer';
                    asset.active_to = true;
                    // Update asset with user information
                    if (asset.sys_class_name.toString() !== 'alm_consumable') {
                        setAssetReservedFor();
                    }
                    break;
 
                case 'in_transit':
                    // install_status = 9 = In transit
                    asset.install_status = '9';
                    asset.substatus = 'reserved';
                    asset.active_to = true;
                    // Update asset with user information
                    if (asset.sys_class_name.toString() !== 'alm_consumable') {
                        setAssetReservedFor();
                    }
                    break;
 
                case 'received':
                    // This needs an update when we touch FSM as the item has to go back to
                    // pending transfer
                    if (SNC.AssetMgmtUtil.isPluginRegistered('com.snc.work_management') && JSUtil.notNil(current.transfer_order.service_order_task)) {
                        // If this TOL is under a work order task then asset.active_to cannot
                        // be released yet,
                        // Once the service order is complete then the active_to will be
                        // released. Also since
                        // this is not the final destination of this asset, it will be in
                        // pending transfer stage
                        if (current.model.sys_class_name == 'cmdb_consumable_product_model' || current.model.asset_tracking_strategy == 'track_as_consumable') {
                            current.asset = new Consumables().split(current.asset,
                                current.quantity_received - current.quantity_returned,
                                '6', 'pending_transfer', '', asset.stockroom,
                                asset.location, asset.assigned_to);
                            updateAsset = false;
                        } else {
                            asset.install_status = '6';
                            asset.substatus = 'pending_transfer';
                            asset.stockroom = current.transfer_order.to_stockroom;
                        }
                    } else if (SNC.AssetMgmtUtil.isPluginRegistered('com.snc.field_service_management') && JSUtil.notNil(current.transfer_order.service_order)) {
                        // If this TOL is under a service order then asset.active_to cannot
                        // be released yet,
                        // Once the service order is complete then the active_to will be
                        // released. Also since
                        // this is not the final destination of this asset, it will be in
                        // pending transfer stage
                        if (current.model.sys_class_name == 'cmdb_consumable_product_model' || current.model.asset_tracking_strategy == 'track_as_consumable') {
                            current.asset = new Consumables().split(current.asset,
                                current.quantity_received - current.quantity_returned,
                                '6', 'pending_transfer', '', asset.stockroom,
                                asset.location, asset.assigned_to);
                            updateAsset = false;
                        } else {
                            asset.install_status = '6';
                            asset.substatus = 'pending_transfer';
                            asset.stockroom = current.transfer_order.to_stockroom;
                        }
                    } else {
                        // If FSM is not actiavated or TOL is not attached to an SO then
                        // release the asset and
                        // make it available in this stockroom which is its final
                        // destination
 
                        // install_status = 6 = In stock
                        asset.install_status = '6';
                        asset.stockroom = current.transfer_order.to_stockroom;
                        asset.active_to = false;
                        // Update asset with user information
                        if (asset.sys_class_name.toString() !== 'alm_consumable') {
                            setAssetReservedFor();
                        }
                        // Keep the asset substate as reserved when there's a Requested For value in the Request Line, otherwise mark it as Available
                        if (asset.substatus != 'reserved' || current.request_line.nil() || current.request_line.requested_for.nil()) {
                            asset.substatus = 'available';
                        }
                    }
                    break;
 
                case 'delivered':
// The TOL will only reach this stage if it has an SO attached to it, in
// that case the asset
// cannot be released yet, once the SO completes, it will be released.
// At this stage,
// the asset us marked 'reserved' (for work) by the FSA in his personal
// stockroom.
asset.install_status = '6';
// Marking asset as available when transfer order type is Field Stockroom request as there will be no service order task linked to part requirement
//OOB - asset.substatus =  current.transfer_order.type == 'field_stockroom_request' ? 'available' : 'reserved';
            //Modified below script as per McKesson requirements
asset.substatus =  current.transfer_order.type == 'field_stockroom_request' ? 'reserved' : 'available';
asset.active_to = false;
 
if(current.transfer_order.type == 'field_stockroom_request')
asset.stockroom = current.transfer_order.to_stockroom;
else if (SNC.AssetMgmtUtil.isPluginRegistered('com.snc.service_management.core'))
asset.stockroom = new SMStockRooms().getAgentStockroom(current.transfer_order.service_order_task.assigned_to);
else if (SNC.AssetMgmtUtil.isPluginRegistered('com.snc.field_service_management'))
asset.stockroom = new FSMUtils().getFSAStockroom(current.transfer_order.service_order.assigned_to);
//Added below else condition as per McKesson requirements
else
asset.stockroom = current.transfer_order.to_stockroom;
break;
 
                default:
                    // Nothing to do
                    updateAsset = false;
            }
 
 
        } else {
// Update active transfer order of asset even when TOL task is closed skipped 
// so that, customers can edit state/substate manually when tol is received/delivered
            if (stateOfTask === '7') {
                if (current.stage.toString() === 'received' || current.stage.toString() === 'delivered') {
                    if (asset.active_to.toString() !== 'false') {
                        asset.active_to = false;
                    }
                }
 
            }
        }
    }
// Update the asset
    updateTOLAsset(updateAsset, assetIsDefective, assetIsPreallocated, asset);
}
 
function updateTOLAsset(updateAsset, assetIsDefective, assetIsPreallocated, asset) {
    if (updateAsset) {
        if (assetIsDefective)
            asset.substatus = 'defective';
        else if (assetIsPreallocated)
            asset.substatus = 'pre_allocated';
        asset.update();
    }
}
 
function setAssetReservedFor() {
    // Update asset with user information ('reserved_for') when the TOL is created from Service Catalog REQ
    // Or when a TOL has 'request_line' (REQ-->RITM) and has been 'requested_for' (reference to sys_user) 
    if (!gs.nil(current.request_line)) {
        if (!gs.nil(current.request_line.request.requested_for)) {
            asset.reserved_for = current.request_line.request.requested_for;
        }
    }
}

Tushar
Kilo Sage
Kilo Sage

Hi @Prasnajeet1 

 

As per my understanding, you can use the following code for reference - 

 

(function() {
    var transferOrderLine = new GlideRecord('alm_transfer_order_line');
    transferOrderLine.addQuery('task', current.sys_id); // Assuming 'current' is the task record
    transferOrderLine.query();

    if (transferOrderLine.next()) {
        var transferOrder = new GlideRecord('alm_transfer_order');
        transferOrder.get(transferOrderLine.transfer_order);

        if (transferOrder.from_stockroom && transferOrder.to_stockroom) {
            var asset = new GlideRecord('alm_hardware');
            asset.get(transferOrderLine.asset);

            if (asset && asset.state == 'in_transit') {
                asset.location = transferOrder.to_stockroom;
                asset.update();
            }
        }
    }
})();

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

 

Hi Tushar

I have used on the before BR on both Insert and update operation with your mentioned script but it is not working. We need to update the location field when the asset state is "In Transit". And location value it should fetch from the alm_transfer_order table. 

 

I believe we need to write some script include and call that in a BR. Please help.