SetValue on a onSubmit Catalog Client Script on Service Portal

AdriaM
Mega Sage

Hi all,

Our users maintain certain CIs by Catalog Items. In my case, when they ask for CI modification, we load all updateble CI data on variables, user updates some of this data and, after approval, the CI data is updated automatically by a script.

Certain CIs have a lot of updatable data and approver only wants to review the data that has been updated. To achive this We store, in a hidden variable, the original loaded data of the CI and, onSubmit, we compare it with the submitted data and populate the changes in a changes variable with the g_form.setValue instruction.

This method works in UI but it doesn't work in SP. Is there any way to inform a varible value in a onSubmit Catalog Client Script in SP?

1 ACCEPTED SOLUTION

AdriaM
Mega Sage

The problem is the getMessage call.

It's an asynchronous call so script ends before receiving the message so field is submitted without being updated.

It's necessary to preload the message in order to avoid an asynchronous call on Submit.

View solution in original post

9 REPLIES 9

AdriaM
Mega Sage

function onSubmit() { alert('onSubmit'); // Only Populate changed fields on Update if (g_form.getValue('action')!='update'){ g_form.clearValue('hidden_string'); return; } // Retrieve stored data alert('Retrieve stored data'); var sObj=g_form.getValue('hidden_string'); if (sObj==''){ return; } var obj = JSON.parse(sObj); //Get Business app info //Get App service information alert('Get Business app info'); var answer = JSON.parse(obj['answer']); var fields = obj['fields']; var formFields = obj['formFields']; var popField = obj['popField']; // Calculate Changed Fields alert('Calculate Changed Fields'); var aChangedFields = []; var aFields = fields.toString().split(","); var aFormFields = formFields.toString().split(","); for (var i=0; i<aFields.length; i++){ if ((answer[aFields[i]] != g_form.getValue(aFormFields[i]))) { if ((answer[aFields[i]] == 0 && g_form.getValue(aFormFields[i]) == 'false') || (answer[aFields[i]] == 1 && g_form.getValue(aFormFields[i]) == 'true')){ null; } else { aChangedFields.push(g_form.getLabelOf(aFormFields[i])); } } } // Populate Changed Fields alert('Populate Changed Fields'); if (aChangedFields.length>0){ getMessage('Changed fields', function(msg) { var text = g_form.getValue(popField); if (text != '') { text += '\r\n'; } text += msg + '=['+aChangedFields.join(',')+']'; g_form.setValue(popField,text); }); } alert('Clear hidden fields to hide it on sp approval'); // Clear hidden fields to hide it on sp approval g_form.clearValue('hidden_string'); //g_form.submit(); alert('End'); return true; }

AdriaM
Mega Sage
function onSubmit() {
	alert('onSubmit');
	// Only Populate changed fields on Update
	if (g_form.getValue('action')!='update'){
		g_form.clearValue('hidden_string'); 
		return;
	}
	
	// Retrieve stored data
	alert('Retrieve stored data');
	var sObj=g_form.getValue('hidden_string');
	if (sObj==''){
		return;
	}
	var obj = JSON.parse(sObj);
	
	//Get Business app info
    //Get App service information
	alert('Get Business app info');
	var answer = JSON.parse(obj['answer']);
	var fields = obj['fields'];
	var formFields = obj['formFields'];
	var popField = obj['popField'];

	// Calculate Changed Fields
	alert('Calculate Changed Fields');
	var aChangedFields = [];
	var aFields = fields.toString().split(",");
	var aFormFields = formFields.toString().split(",");
	for (var i=0; i<aFields.length; i++){
		if ((answer[aFields[i]] != g_form.getValue(aFormFields[i]))) {
			if ((answer[aFields[i]] == 0 && g_form.getValue(aFormFields[i]) == 'false') ||
				(answer[aFields[i]] == 1 && g_form.getValue(aFormFields[i]) == 'true')){
				null;
			} else {
				aChangedFields.push(g_form.getLabelOf(aFormFields[i]));
			}
		} 
	}
	
	// Populate Changed Fields
	alert('Populate Changed Fields');
	if (aChangedFields.length>0){
		getMessage('Changed Fields', function(msg) {
			var text = g_form.getValue(popField);
			if (text != '') {
				text += '\r\n';
			}
			text += msg + '=['+aChangedFields.join(',')+']';
			g_form.setValue(popField,text);
		});
	}
	alert('Clear hidden fields to hide it on sp approval');
	// Clear hidden fields to hide it on sp approval
	g_form.clearValue('hidden_string'); 
	//g_form.submit();
   alert('End');
	return true;
}

Hi @AdriaM 

So you not getting any alerts of this script on portal?

Thank you
Prasad

AdriaM
Mega Sage

No. I'm getting all alerts, no problem.

The problem is that after submiting the catalog item in the Service Portal, the variable informed in popField doesn't contains any information about changed field.

When the same is done by UI, popField contains changed fields.

AdriaM
Mega Sage

The problem is the getMessage call.

It's an asynchronous call so script ends before receiving the message so field is submitted without being updated.

It's necessary to preload the message in order to avoid an asynchronous call on Submit.