Update field using PreProcess scripts


#1

I want to modify a field in a preprocess script but it is not working.

The script is

if (event.request.payload.resource) {

    lodash._.each (event.request.payload.resource, function( record ) {
        
    record.snapTypeID = 2;  

However that is never run because
event.request.payload.resource is null.

when i log:

var_dump(event.request.payload);
var_dump(event.request.payload.resource);

I get for (event.request.payload):

object(Object)#2016481607 (2) {
[“title”] =>
string(4) “ssss”
[“description”] =>
NULL
}

and for (event.request.payload.resource):

NULL


Adding record to db problem
#2

Hi @Matt_2012,

Try this way:

//check if u have a payload
if (event.request.payload) {
    //check if u have the key snapTypeID
    if (!event.request.payload.snapTypeID) {
        throw 'snapTypeID cannot be empty';
    } else {
        //we have the key, so change it
        event.request.payload.snapTypeID = 2;
    }
}

I hope it helps


#3

If you’re trying to create records, your payload should contain an array named ‘resource’.

http://wiki.dreamfactory.com/DreamFactory/Tutorials/Posting_records


#4

Hi should the resource be where I update the data ?

I am getting closer but still not able to modify (change the title) in the record before it is added to the table.

I now have a resource array in the payload

dump_var(event.request.payload.resource)

array(1) {
  [0] =>
  object(Object)#220134100 (2) {
    ["title"] =>
    string(5) "Alice"
    ["description"] =>
    string(5) "Jones"
  }
}

but I cant update the record in the script.

if (event.request.payload.resource) {

    lodash._.each (event.request.payload.resource, function( record ) {
        var_dump(record);
        record.title = 'not alice';
        record['title'] = 'not alice 2';
        var_dump(record);
    });
}

event.request.payload.resource[0].title = 'not alice 3';
var_dump(event.request.payload.resource);
event.request.payload.resource[0]['title'] = 'not alice 4';
event.request.payload['title'] = 'not alice 5';
var_dump(event.request.payload.resource);

payload and resource are being changed but alice is still being submitted into the database.


#5

Make sure you are editing the correct script depending on POST or PATCH, and that the scripts are marked as Active.

For example,

db._table.contact.post.pre_process

vs

db._table.contact.patch.pre_process


#6

Hi Todd

Script definitely active and I am using post to add a new record.
It wouldn’t be appearing in my logs otherwise.

I can modify the resource object but not what gets added to mysql table.

What would be very helpful here is a working example where data is modified at the preprocess stage.

Many Thanks

Matt


#7

I confirmed there is an issue here. I’m working with the dev team to resolve and will report back here with updates.


#8

Any date, for this issue ?

I have the same with php script


#9

Hi There,

I am still getting similar kind of issue. I am unable to update resource from event. below here is my event and code within event. I can validate the value but when I update the value of it, its not reflecting in db.

Event: mysql mysql._table.{table_name} mysql._table.{table_name}.post.pre_process mysql._table.employee.post.pre_process

Code:
console.log(event.request); // outputs to file in storage/log of dreamfactory install directory

// use NPM to install lodash
event.request.content_changed = true;
var lodash = require(“lodash”);

event.request.payload.resource[0].salary = 90;

Note: I have enabled both check boxes “Active” and “Allow script to modify request payload”. I am sure that Event is triggering because I can validate the data and throw errors.


#10

To update the Payload here are things to do

  1. Tick Allow script to modify request payload to True.

  2. Modify your data something like this

    $event['request']['payload']['resource'][0]['fullname']=$firstname." ".$lastname;

    There are few instances of data which doesn’t contain resource as array, specially system/user based api so you can go like

    $event['request']['payload']['resource']['fullname']=$firstname." ".$lastname;

  3. The most important step which actually modifies the payload content.

    $event['request']['content']=json_encode($event['request']['payload']);

  4. Put in all of the above in Prescript

Done. Can help who are still looking for answer. Cheers.


#11

Sorry to resurrect this topic, but this process is not at all clear from the documentation, especially for Python scripting. It would be very helpful to add this information to the documentation and provide some examples of how to do this in the example scripts. Maybe I can help?

From reading the documents, I guessed that I needed to update event.request.payload.resource[0], but that has no effect on the payload that is actually inserted into the database. Instead, I need to modify the payload, then do event.request.content = json.dumps(event.request.payload).

Is there a reason that this isn’t an automatic process? When payload is updated and “Allow script to modify request payload” is set to True, shouldn’t content automatically update to match payload?


#12

Here is an example script that works in Python: https://pastebin.com/u4qsWAub

This script is used as a pre-post request script. It takes the input document, looks for “start”, “end”, “start_datetime”, and “end_datetime” and creates any that don’t exist based on the ones that do. So, if “start” exists as a timestamp, “start_datetime” is created based on that timestamp.