Skip navigation
All People > banty01 > Tyler Band's Blog > 2017 > May

One of the most common issues that all developers face is how to set specific attribute values in a REST call during a POST or PUT.  For example, the database may have columns on all tables for date created, date updated, and created by user.  These fields may or may not be on every table but the requirement states that these values must be set using the server timestamp and the logged in user.  Since our REST server uses a common connection pool - we will need to use the server to calculate the timestamp and the actual user logon identifier at the start of a REST call.


CA Live API Creator provides a rule type 'Formula' that you can attach to each column attribute.  These formula can be copied from one table to the next.  But if your model has hundreds of these tables - this may be a lot more work and you may ask - "there must be a better way".


row.createdDate = new Date();

row.createdByUser = req.apikey.userIdentifier;


So here is another option. The Update Request (e.g. POST or PUT) will start by calling the Request Event.  So we only need to test the inbound request event for the initial state (req.verb == 'POST' || req.verb == 'PUT')

Event Lifecycle


Then we will test the inbound 'json' object for specific named attribute values and update these values once.

Note that we are testing all inbound requests for POST and PUT.  Then we convert the 'json' payload from a string value to a JSON object named temp. As we iterate over the object (root level only) - we are looking for specific named attributes (ModifiedDate and createdByUser) then setting them to the global values.  Finally, we turn the JSON temp object back into a string using JSON.stringify and we are done.


if(req.verb == 'POST' || req.verb == 'PUT') {
      log.debug("requestEvent json: " + json);
      var temp = JSON.parse(json);
      for (var attr in temp) {
         log.debug(attr + ":"+temp[attr]);
        if( attr == 'ModifiedDate') {
         temp[attr] = new Date();
      if( attr === 'createdByUser'){
         temp[attr] = req.apiKey.getUserIdentifier();
json = JSON.stringify(temp);