Updating a record from a spModal widget

Dan Tracey1
Giga Contributor

 

Hi all,

I have created a custom widget that displays a list of records in which are used to request information from someone.

on that list of records is a button for them to respond, on pressing the button it opens up an spModal asking them to write a response which is then passed to the client and i then want to pass it back to the server so it can then update that record with the input.

I have tried using c.server.update but i have had no luck

my code below - 

HTML

<div>
  <div>
    <table class="table table-striped">
      <thead>
        <tr>
          <th>Number</th>
          <th>Date Requested</th>
          <th>Information Requested</th>
          <th>Permit Request</th>
          <th>Respond</th>

        </tr>
      </thead>
      <tr ng-repeat='rfi in c.data.rfi'>
        <td>{{rfi.rfi_number}}</td> 
        <td>{{rfi.date_requested}}</td>
        <td>{{rfi.information_requested}}</td>
        <td><a target="_blank" href="/contractor_portal?id=form&table=x_virtr_permit_to_request_for_information&sys_id={{ rfi.rfi_sysID }}">{{rfi.permit_request}}</a></td>
        <td>  
          <button ng-click="c.onOpen()" class="btn btn-default">Respond</button>
          <div ng-show="c.response">
            You answered <span>{{c.response}}</span>
          </div></td>
      </tr>
    </table>
  </div>
</div>

 

Client Script - 

function(spModal) {
	var c = this;
	c.onOpen = function() {
		//ask the user for a string
		spModal.open({
			title: 'Response',
			message: 'Please input your response to the requested for information',
			input: true,
			value: c.response

		})
			.then(function(response) {
			c.response = response;

		})
	}
}

 

Server Script - 

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
	data.rfi = [];

	var rfi = new GlideRecord('x_virtr_permit_to_request_for_information');
	rfi.addQuery('state', 'requested');
	rfi.query();

	while (rfi.next()) {
		var o = {};

		o.rfi_number = rfi.getDisplayValue('number');
		o.date_requested = rfi.getDisplayValue('sys_created_on');
		o.information_requested = rfi.getDisplayValue('information_requested');
		o.permit_request = rfi.getDisplayValue('permit_to_work_request');
		o.rfi_sysID = rfi.sys_id.toString();
		data.rfi.push(o);

	}

})();

I think i maybe trying to over complicate this as i'm sure its a lot more simple than things im trying to just pass 1 input value back to the server.

Help would be greatly appreciated

1 ACCEPTED SOLUTION

Oleg
Mega Sage

I think that you can easy modify your existing code to implement your requirements.

First of all, it's important to understand that the server code could be executed multiple times. Some global variables will be initialized before the execution will be started. You use already data variable. Another important global variable is input. At the first execution it's undefined. If you later execute c.server.updatec.server.get or c.server.refresh then the server code will be executed once more time, but input variable will be set to the object with the data, which will be sent to the server. Both methods c.server.update and c.server.get make POST HTTP request to the server, but c.server.get allows you to specify the data, which will be sent to the server explicitly instead of the method c.server.update sent always $scope.data object (c.data). In your case, c.data contains rfi array. You don't need to sent it once more to the server. To prevent that you should include the line like c.data.rti = null; or better delete c.data.rti; before usage of c.server.update. Thus the usage of c.server.get seams to me the best choice in your case.

Now about your code. I suggest to modify your client code to use the following body of spModal.open({...}).then(function (response) {...}):

spModal.open({
	title: 'Response',
	message: 'Please input your response to the requested for information',
	input: true,
	value: c.response
})
.then(function (response) {
	c.server.get({
		action: "my_action",
		userInput: response
	}).then (function (serverResponse) {
		console.log(serverResponse.data)
	});
});

and the server code to the following:

(function (data, input) {
	if (input != null && input.action === "my_action") {
		//console.log(input);
		// one can use here input.userInput;
		data.someProperty = "some value";
		return;
	}
	data.rfi = [];
	// your old code which fills data.rfi array

})(data, input);

 

View solution in original post

2 REPLIES 2

Oleg
Mega Sage

I think that you can easy modify your existing code to implement your requirements.

First of all, it's important to understand that the server code could be executed multiple times. Some global variables will be initialized before the execution will be started. You use already data variable. Another important global variable is input. At the first execution it's undefined. If you later execute c.server.updatec.server.get or c.server.refresh then the server code will be executed once more time, but input variable will be set to the object with the data, which will be sent to the server. Both methods c.server.update and c.server.get make POST HTTP request to the server, but c.server.get allows you to specify the data, which will be sent to the server explicitly instead of the method c.server.update sent always $scope.data object (c.data). In your case, c.data contains rfi array. You don't need to sent it once more to the server. To prevent that you should include the line like c.data.rti = null; or better delete c.data.rti; before usage of c.server.update. Thus the usage of c.server.get seams to me the best choice in your case.

Now about your code. I suggest to modify your client code to use the following body of spModal.open({...}).then(function (response) {...}):

spModal.open({
	title: 'Response',
	message: 'Please input your response to the requested for information',
	input: true,
	value: c.response
})
.then(function (response) {
	c.server.get({
		action: "my_action",
		userInput: response
	}).then (function (serverResponse) {
		console.log(serverResponse.data)
	});
});

and the server code to the following:

(function (data, input) {
	if (input != null && input.action === "my_action") {
		//console.log(input);
		// one can use here input.userInput;
		data.someProperty = "some value";
		return;
	}
	data.rfi = [];
	// your old code which fills data.rfi array

})(data, input);

 

Dan Tracey1
Giga Contributor

Thank your detailed reply and explanation, i will give this a shot.