Skip to main content

Add / Edit

To save data back to FIYGE we will use the endpoint POST /edit or POST /add if it is a new record.

To edit values on a people record we will call the below request

Request​

POST /crm/peoples/edit.json

Payload​

We will pass the body by form-data and they will all start with the id of the record. The key will be in the form of data[${controller}][${field_key}]. This can be created by iterating over the name from an input element.

{   
"name": [
"peoples",
"people_type_id"
],
"__name": "peoples.people_type_id",
"label": "people_type_id",
}

Here is a js snippet that can turn this name into a valid form-data entry.

function convertToNestedDataFormat(names) {
if (!Array.isArray(names) || names.length === 0) {
throw new Error("Input must be a non-empty array of strings");
}
return names.reduce((acc, curr) => `${acc}[${curr}]`, "data");
}

const key = convertToNestedDataFormat(["peoples", ["people_type_id"]])
// "data[peoples][people_type_id]"

Here is a sample form-body to edit the leads record of id 1811

{
"data[normalized]": 1,
"data[leads][id]": 1811,
"data[leads][first_name]": "Test",
"data[leads][last_name]": "LEAD 1",
"data[leads][primary_email]": "michael@fiyge.com",
"data[leads][lead_source]": 898,
"data[leads][__lead_source]": "B2B Partner - RCIC",
"data[leads][lead_source_model]": "config_lead_sources",
"data[leads][people_type_id]": 11,
"data[leads][__people_type_id]": "Internal Staff",
"data[leads][people_type_id_model]": "config_people_types",
"data[leads][people_sub_type_id]": 14,
"data[leads][__people_sub_type_id]": "Active",
"data[leads][people_sub_type_id_model]": "config_people_sub_types",
"data[leads][mobile]": "9899998988",
"data[leads][email_opt_out]": null,
"data[leads][people_id]": 50,
"data[leads][__people_id]": "Test PEOPLE",
"data[leads][people_id_model]": "peoples",
"data[leads][wechat_contact]": null,
"data[leads][line_contact]": null,
"data[leads][office_phone]": null,
"data[leads][do_not_contact]": null,
"data[leads][stage_log][pause_workflow_notification]": null,
"data[leads][stage_log][node_id]": 4459,
"data[leads][country_to_immigrate_id]": null,
"data[leads][__country_to_immigrate_id]": null,
"data[leads][country_to_immigrate_id_model]": null,
"data[leads][date_of_birth]": null,
"data[leads][country_of_citizenship_id]": null,
"data[leads][__country_of_citizenship_id]": null,
"data[leads][country_of_citizenship_id_model]": null,
"data[leads][marital_status_id]": null,
"data[leads][__marital_status_id]": null,
"data[leads][marital_status_id_model]": null,
"data[leads][level_of_education]": null,
"data[leads][years_of_experience_in_a_job]": null,
"data[leads][current_occcupation]": null,
"data[leads][net_worth_in_cad]": null,
"data[leads][owned_by]": 739,
"data[leads][__owned_by]": "Michael Ilao",
"data[leads][owned_by_model]": "owned_by_user",
"data[leads][primary_acl_group]": 2629,
"data[leads][__primary_acl_group]": "IAI - Software Engineer - Michael",
"data[leads][primary_acl_group_model]": "groups",
"data[leads][created_by]": 739,
"data[leads][__created_by]": "Michael Ilao",
"data[leads][created_by_model]": "created_by_user",
"data[leads][created]": "2025-01-03 14:32:07",
"data[leads][modified_by]": 739,
"data[leads][__modified_by]": "Michael Ilao",
"data[leads][modified_by_model]": "modified_by_user",
"data[leads][modified]": "2025-01-03 14:32:43",
"data[leads][revision]": 1,
"data[leads][business_key]": null,
"data[action][save_&_continue]": "Save & Continue"
}

This json will need to be converted to a form-data which can be achieved with this js snippet.

function jsonToFormData(json) {
const postData = new FormData();
Object.entries(json).forEach(([key, value])=> {
postData.append(key, value)
})

return postData
}

To build this json, we must use a combination of the form meta-data from the initial GET request as well as the actual data values of the record. We can get the key from the name field on each form element and the value from the data record using the name key to index it.

To know which inputs are required and which ones are omited, is dependent on the permission of each elements. Permissions are determined either by a user permission level, form permission level or declared statically on the input.

Permissions can be one of four values

{
0: "INHERIT",
1: "DELETE",
2: "EDIT",
4: "VIEW",
}

Elements with a permission 2 or EDIT must be included in the form body, while VIEW and DELETE are be ommited. The permission INHERIT depends on the parent of the element.

Action​

When submitting a POST call, an action must be included to describe what action to trigger. The form entry will take the form data[{action_name}]: ${action_value}

{
"data[action][save_progress]": "Save Progress"
}

These values action_name and action_value can be found on the input element that triggers the submission. These input elements have the type submit.

 {
"name": [
"action",
"save_progress"
],
"label": "Save Progress",
"value": "Save Progress",
"__value": "Save Progress",
"is_web_compatible": "1",
"is_mobile_compatible": "1",
"rules_grid": "1",
"roles_grid": "1",
"helper": "\\kernel\\form",
"method": "submit",
"allow_copy_to_clipboard": "0",
"allow_user_to_specify_user_level_default": "0",
"business_key": "65dcdcab-a60c-4090-a2f5-4930ac69033c",
"template": "submit",
"packaged_with_module": "crm",
"mw": false
}

The above element is the submit button for a peoples record. We can use the below js snippet to extract the form-data entry

function getActionFromElement(element) {
let actionName = element.name.join("][")
// action][save_progress
actionName = `[${actionName}]`
// [action][save_progress]

return {
[actionName]: element.label
}
}