Request for Assistance with Script for Updating a Custom Field in Accounts

Request for Assistance with Script for Updating a Custom Field in Accounts

Dear Zoho Support Team,

I have created the following script, intended to automate the update of a custom field called kontrolka in the Accounts module based on data from the Leads module.

Below is the script:

// Get the account ID from the Lead
var accountId = $lookup.Firma_w_bazie; // Check if the account ID exists if (accountId) { try { // Fetch account data using the account ID const account = await V7.api.get(`accounts/${accountId}`); // Retrieve the current value of the 'kontrolka' field const kontrolkaField = account.custom_fields.kontrolka; // Check if the 'kontrolka' field is defined if (kontrolkaField !== undefined) { // Toggle the value of the 'kontrolka' field const newValue = !kontrolkaField; // Update the account record with the new 'kontrolka' value await V7.api.patch(`accounts/${accountId}`, { custom_fields: { kontrolka: newValue } }); } } catch (error) { // Handle any errors that occur during the fetch or update process } } else { // Handle the case where no account ID is found }

Objective of the Script:

  1. Retrieve the account ID (Firma_w_bazie) from the Lead.
  2. Verify if the account ID exists.
  3. If the account ID exists, fetch the account details via the API (accounts/{accountId}).
  4. Check the value of the custom field kontrolka in the account.
  5. Toggle (invert) the value of kontrolka (e.g., true to false or false to true).
  6. Update the account record with the new value for kontrolka using the API (accounts/{accountId}).

The primary purpose of this script is to create a simple toggle mechanism for the kontrolka field in a related account record.

Real Objective:

The ultimate goal of this script is to trigger a change on the Account record because such a change automatically propagates updates from the Account record to the Lead record. This is crucial for my process since there are nearly 30 custom fields in the Account that need to sync with the corresponding fields in the Lead. Manually updating each field individually would be cumbersome, so this automation is intended to handle it efficiently.

Problem:

The script does not work as expected, and I am unable to determine at which step the process fails. There are no error messages, and the data in the Accounts module is not being updated.

Could you please assist me in identifying the issue? Is there something wrong with the logic of my script, or do I need to adjust my system configuration?

I would greatly appreciate any guidance or suggestions to resolve this issue.

Thank you in advance for your support.

Best regards,

Katarzyna Ochnik
katarzyna.ochnik@medidesk.com