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
- 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');
- 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?
- 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?
Thanks,
Mark