Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Inject mrvs to data?

110100
Tera Contributor

Hello Community,

I would like to put the value to 'mrvs 'post_transfer_request_variable_set (sysid: d1f902b31bbc42d0f078a60ce54bcb12)'.
However, it returns a result of 'test_multi_row []'.
I am using 'test_multi_row(MultiLine)' now as a test, what improvements are needed to put the value into 'post_transfer_request_variable_set(MRVS)'?

 

HTML

<div>
	<div><label>CSV Upload</label>&nbsp;<input ng-click="c.applyVariablesAjax()" type="button" value="${Apply Data}" /></div>
</div>

ScriptInclude

var MRVUtils = Class.create();
MRVUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCSVData: function() {
        gs.info('--> MRVUtils: Entered');
        var cartID = this.getParameter('sysparm_cart_id');
        gs.info('--> MRVUtils: cart ID - ' + cartID);
		var dataSource = this.createDataSource();

        var donorTable = 'sc_cart_item';
        var recipientTable = 'sys_data_source';
        var recipientID = dataSource.getUniqueValue();
        gs.info('--> MRVUtils: recipient ID - ' + recipientID);

        GlideSysAttachment.copy(donorTable, cartID, recipientTable, recipientID);

        var loader = new GlideImportSetLoader();
        var importSetRec = loader.getImportSetGr(dataSource);
        var ranload = loader.loadImportSetTable(importSetRec, dataSource);
        importSetRec.state = 'loaded';
        importSetRec.update();


        var transArr = [];
        var i = 0;

        var importRow = new GlideRecord('x_snc_transferapp_transfer_data_excel');
        importRow.addQuery('sys_import_set', importSetRec.sys_id);
        importRow.query();

        while (importRow.next()) {
            var transObj = {};

            transObj.u_post_transcription_key = importRow.getValue('u_転記キー_テンキ');
            transObj.u_post_voucher_currency_amount = importRow.getValue('u_伝票通貨額_デンピョウ_ツウカ');
            transObj.u_post_tax_code = importRow.getValue('u_税コード_ゼイ');
            transObj.u_post_account_code = importRow.getValue('u_勘定コード_カンジョウ');
            transObj.u_post_alternate_control_account = importRow.getValue('u_代替統制勘定_ダイタイ');
            transArr[i] = transObj;

            i += 1;
        }

        var transJSONString = JSON.stringify(transArr);
        gs.info('--> MRVUtils: Result - ' + transJSONString);
        return transJSONString;

    },

    createDataSource: function() {
        var grDs = new GlideRecord('sys_data_source');
        grDs.name = 'Transfer_CSV_Data: ' + new GlideDateTime();
        grDs.import_set_table_name = 'x_snc_transferapp_transfer_data_excel';
        grDs.file_retrieval_method = 'Attachment';
        grDs.type = 'File';
        grDs.format = 'CSV';
        grDs.header_row = 1;
        grDs.sheet_number = 1;
        grDs.insert();
        gs.info('--> DataSource sysID is ' + grDs + ' and table val is ' + grDs.sys_class_name);
        return grDs;
    },



    type: 'MRVUtils'
});

ClientScript

api.controller = function ($scope) {
var c = this;

c.applyVariables = applyVariables;
c.applyVariablesAjax = applyVariablesAjax;

console.log('getCartID', getCartID());

 function applyVariables () {
 var cartID = getCartID();
 c.server.get({
 'action': 'get-CSV-data',
 'cartID': cartID,
 }).then(receiveCSVData);
 }

function applyVariablesAjax () {
var cartID = getCartID();
c.data.action = 'get-CSV-data';
 		c.server.update().then(function() {
 		c.data.action = '';
 	})

var ga = new GlideAjax('global.MRVUtils');

ga.addParam('sysparm_name', 'getCSVData');
ga.addParam('sysparm_cart_id', cartID);

ga.getXML(processResponse);
}

function getCartID () {
return getTableIdOrParentScope($scope);
}

function getTableIdOrParentScope (scope) {
return typeof scope.attachmentHandler != 'undefined' ?
scope.attachmentHandler.tableId :
typeof scope.$parent != 'undefined' ? getTableIdOrParentScope(scope.$parent) : '';
}

function processResponse (response) {
console.log('test_multi_row', response);
var answer = response.responseXML.documentElement.getAttribute('answer');
var delimiter = ',';
var quoteCharacter = '"';
//console.log('test_multi_row', response.data.test_multi_row);
$scope.page.g_form.setValue('test_multi_row', answer);

}

function receiveCSVData (response) {
console.log('test_multi_row', response.data.test_multi_row);
$scope.page.g_form.setValue('test_multi_row', response.data.test_multi_row);
}

function responseDelimiter(response){
// var testHeader = '\"Joe\",\"Smith\",\"1470 W Carmen, Chicago IL, 60640\"';
var delimiter = ',';
var quoteCharacter = '"';
var x = new sn_impex.CSVParser().parseLineToArray(response.data.test_multi_row, delimiter, quoteCharacter);
console.log('test_multi_row', x);
$scope.page.g_form.setValue('test_multi_row',x);

}
};

Server Script

(function ($sp, data, input) {
	
	/*
	if (input.action == 'get-CSV-data'){
		data.list_of_participants = new global.MRVUtils().getCSVData(input.cartID);
	}
	*/

	if (input) {
var localInput = input;
	if (localInput.action == 'get-CSV-data'){
		data.list_of_participants = new global.MRVUtils().getCSVData(localInput .cartID);
	}}
	


	gs.addErrorMessage("input "+input);
		gs.addErrorMessage("data "+data);
	
/*
	if (data != 'undefined'){
			data.test_multi_row = new global.MRVUtils().getCSVData(input.cartID);
//		data.list_of_participants = new global.MRVUtils().getCSVData(input.cartID);
		gs.addErrorMessage("input internal "+input);
	}
	*/

//	gs.addErrorMessage("input "+input);
	
	//data.list_of_participants = new global.MRVUtils().getCSVData(input.cartID);
//	data.list_of_participants = ({post_transcription_key:'test1',post_voucher_currency_amount:'test2',post_tax_code:'test3',post_account_code:'test4',post_alternate_control_account:'test5'});
	
})($sp, data, input);

 

0 REPLIES 0