HomeRecipesAPI ReferenceChangelogDiscussions
Log In
Discussions

Discussions

Ask a Question
Back to All

Updating a Custom Field

Hi All,

I am trying to update a specific WorkOrder Custom Field. I have got the API working to pull all the data but when I am trying to push up a change it is failing.

I am trying to change this:

{"Data": {"CustomFields": [{"Descriptor": {"Id": 1102}, "ObjectId": 65347, "ObjectTypeId": "WO", "Value": "Yes", "CurrencyTypeId": "Unknown", "Id": 25035}, {"Descriptor": {"Id": 1106}, "ObjectId": 65347, "ObjectTypeId": "WO", "Value": "0", "CurrencyTypeId": "Unknown", "Id": 25036}], "ConcurrencyId": 7, "Id": 65347}}
Update Payload:

To This:

{"Properties": ["CustomFields"], "Entity": {"Id": 65347, "CustomFields": [{"Descriptor": {"Id": 1102}, "ObjectId": 65347, "ObjectTypeId": "WO", "Value": "Yes", "CurrencyTypeId": "Unknown", "Id": 25035}, {"Descriptor": {"Id": 1106}, "ObjectId": 65347, "ObjectTypeId": "WO", "Value": "1", "CurrencyTypeId": "Unknown", "Id": 25036}]}, "ConcurrencyId": 7}

So basically updating the Value of ID 1106 from 0 to 1.

I am getting this error though:

Failed to update work order 65347. Status code: 405
{"Message":"The requested resource does not support http method 'PUT'."}

This is the code I have tried:

def update_work_order(work_order_id):
    # Step 1: Get the work order data
    get_url = f'{base_get_url}/{work_order_id}/?properties=CustomFields.*'
    response = requests.get(get_url, headers=get_headers)
    if response.status_code == 200:
        work_order_data = response.json()
        print('Work order data retrieved successfully.\n')
        print(json.dumps(work_order_data))
    else:
        print(f'Failed to retrieve work order {work_order_id}. Status code: {response.status_code}')
        print(response.text)
        return
    
    # Step 2: Modify the custom fields
    work_order = work_order_data['Data']
    custom_fields = work_order.get('CustomFields', [])
    concurrency_id = work_order.get('ConcurrencyId')
    work_order_id = work_order.get('Id')
    
    # Flag to check if any updates are made
    updates_made = False    
    for field in custom_fields:
        descriptor_id = field['Descriptor']['Id']
        if descriptor_id == 1102:
            if field['Value'] != 'Yes':
                field['Value'] = 'Yes'
                updates_made = True
        elif descriptor_id == 1106:
            if field['Value'] != '1':
                field['Value'] = '1'
                updates_made = True
    
    # If no updates are made, skip the PUT request
    if not updates_made:
        print(f'No updates needed for work order {work_order_id}.')
        return
    
    # Update payload.
    update_payload = {
        "Properties": ["CustomFields"],
        "Entity": {
            "Id": work_order_id,
            "CustomFields": custom_fields
        },
        "ConcurrencyId": concurrency_id
    }
    
    print('Update Payload:\n')
    print(json.dumps(update_payload))
    
    # Step 3: Update the work order
    put_url = f'{base_put_url}/{work_order_id}'
    put_response = requests.put(put_url, headers=put_headers, data=json.dumps(update_payload))
    if put_response.status_code == 200:
        print(f'Work order {work_order_id} updated successfully.')
        updated_work_order = put_response.json()
        print(json.dumps(updated_work_order))
    else:
        print(f'Failed to update work order {work_order_id}. Status code: {put_response.status_code}')
        print(put_response.text)

Would anyone be able to advise where I am going wrong?

Thanks,

Rob.