Python scripting questions


We’ve been trying out some of the Python scripting, which is currently available in 2.1.2 (in beta).

Just wanted to point out a few things we found (not sure if some of the behavior is because still in beta or not).

In a table POST pre-process script, we have the following:

verb = event['request']['method']; # always returns POST in this example
payload = event['request']['payload'];
records = payload['resource'];  #all records in the post
record = records[0];  # first record
  1. Verifying fieldnames in the post

The following code using payload - similar to the tutorial code - doesn’t work:

if 'organization_id' not in payload:  # this doesn't work
       raise ValueError('payload: Missing field organization_id');

Looking in the specific record does work:

    if 'organization_id' not in record:
        raise ValueError('record: Missing field organization_id');
  1. Best practice for referencing a specific key/value in a record?

If you directly try to access a key- e.g., lastname = record[‘lastname’] - it works if the key exists. If not, the script stops and the post is completed (any other code is ignored and no error is generated). Is this going to be the default behavior or should this error be caught?

The above can be prevented by checking the key first, but this does have to be done for every field:

if 'lastname' in record:
    lastname = record['lastname'];

Is there a better way to obtain a specific record key?

  1. Integer/numeric values

If a POST has an integer/numeric value, it becomes a string in the payload. For example, we post {“organization_id” =99} and in the script, need to convert to an integer:

if int(record['organization_id'] or -1) < 0 :
    raise ValueError('ERROR id is less than 0 ');

Should all values be string?




In 2.1.2 event and platform are both Python dictionary type. You need to use them as regular dictionary. So, organization_id is not going to exist in payload but will exist in payload['resource']. In the up coming release of DF2, event and platform will be Python bunch type. This will allow easy access to values using dot (.) notation like in JavaScript. You will be able to do the following

verb = event.request.method; # always returns POST in this example
payload = event.request.payload;
records = payload.resource;  #all records in the post
record = records[0];  # first record

Directly accessing a key that doesn’t exist from Python dictionary will throw error, therefore, you need to check the key before accessing it. Unfortunately there isn’t a better way to do this currently or at least I don’t know of one. You could do this in one line like below which is a bit shorter but not really a better way.

lastname = record['last name'] if('lastname' in record) else '';

All values coming from PHP side are going to be string.



Thanks for the information! Just to be clear, when event is made into bunch type, will the dictionary style code we need to use now still work since bunch is subclass of dict?


Yes, the dictionary style code will still work with bunch type.