What?
A ridiculously quick article on resolving a zoho.crm.getRelatedRecords() combined with a zoho.crm.bulkUpdate issue.
Why?
Trying to be clever and reducing the API calls to go through a few thousand records and to update the related records. I was getting the error INVALID_DATA with the details being api_name:'data'... But this is shortcode, not an invokeURL.
{ "code":"INVALID_DATA", "details":{ "api_name":"data" }, "message":"invalid data", "status":"error" }
How?
I recall covering this solution before but it boils simply down to the zoho.crm.getRelatedRecords() not retrieving related records which are clearly visible when using the CRM.
The cause of the problem is the same as covered in my article Joel Lipman - ZohoCRM: zoho.crm.searchRecords only returns certain records.
Consider the following code:
/* ******************************************************************************* Function: string standalone.fn_Deal_UpdateRelatedToCancelled() Label: Fn - Customer Deals - Cancel Related Records Trigger: Standalone Purpose: For any deal which is cancelled, cancel its related sales orders. Inputs: - Outputs: - Date Created: 2025-01-30 (Joel Lipman) - Initial release Date Modified: ??? ??? More Information: Custom View ID: 123456000012345678 ******************************************************************************* */ // // initialize v_CountTotal = 0; v_CountUpdated = 0; l_Pages = {1}; v_PerPage = 1; // for each v_Page in l_Pages { l_Deals = zoho.crm.getRecords("Deals", v_Page, v_PerPage, {"cvid":123456000012345678}); for each m_Deal in l_Deals { if(!isNull(m_Deal.get("id"))) { v_CountTotal = v_CountTotal + 1; if(m_Deal.get("Stage").equalsIgnoreCase("Cancelled")) { // // cancel all related quotes // (...) // // cancel all related sales orders l_RelatedSOs = zoho.crm.getRelatedRecords("SalesOrders", "Deals", m_Deal.get("id")); l_UpdateSOs = List(); for each m_SO in l_RelatedSOs { m_UpdateSO = {"id":m_SO.get("id"), "Status":"Cancelled"}; l_UpdateSOs.add(m_UpdateSO); } r_UpdateRelated = zoho.crm.bulkUpdate("Sales_Orders", l_UpdateSOs); // // cancel all related invoices // (...) // // mark the deal as processed r_UpdateDeal = zoho.crm.updateRecord("Deals", m_Deal.get("id"), {"Cancelled_Related_Records":true}, {"trigger":{}}); if(!isNull(r_UpdateDeal.get("id"))) { v_CountUpdated = v_CountUpdated + 1; } } } } }
- /* *******************************************************************************
- Function: string standalone.fn_Deal_UpdateRelatedToCancelled()
- Label: Fn - Customer Deals - Cancel Related Records
- Trigger: Standalone
- Purpose: For any deal which is cancelled, cancel its related sales orders.
- Inputs: -
- Outputs: -
- Date Created: 2025-01-30 (Joel Lipman)
- - Initial release
- Date Modified: ???
- ???
- More Information:
- Custom View ID: 123456000012345678
- ******************************************************************************* */
- //
- // initialize
- v_CountTotal = 0;
- v_CountUpdated = 0;
- l_Pages = {1};
- v_PerPage = 1;
- //
- for each v_Page in l_Pages
- {
- l_Deals = zoho.crm.getRecords("Deals", v_Page, v_PerPage, {"cvid":123456000012345678});
- for each m_Deal in l_Deals
- {
- if(!isNull(m_Deal.get("id")))
- {
- v_CountTotal = v_CountTotal + 1;
- if(m_Deal.get("Stage").equalsIgnoreCase("Cancelled"))
- {
- //
- // cancel all related quotes
- // (...)
- //
- // cancel all related sales orders
- l_RelatedSOs = zoho.crm.getRelatedRecords("SalesOrders", "Deals", m_Deal.get("id"));
- l_UpdateSOs = List();
- for each m_SO in l_RelatedSOs
- {
- m_UpdateSO = {"id":m_SO.get("id"), "Status":"Cancelled"};
- l_UpdateSOs.add(m_UpdateSO);
- }
- r_UpdateRelated = zoho.crm.bulkUpdate("Sales_Orders", l_UpdateSOs);
- //
- // cancel all related invoices
- // (...)
- //
- // mark the deal as processed
- r_UpdateDeal = zoho.crm.updateRecord("Deals", m_Deal.get("id"), {"Cancelled_Related_Records":true}, {"trigger":{}});
- if(!isNull(r_UpdateDeal.get("id")))
- {
- v_CountUpdated = v_CountUpdated + 1;
- }
- }
- }
- }
- }
Corrected Code:
/* ******************************************************************************* Function: string standalone.fn_Deal_UpdateRelatedToCancelled() Label: Fn - Customer Deals - Cancel Related Records Trigger: Standalone Purpose: For any deal which is cancelled, cancel its related sales orders. Inputs: - Outputs: - Date Created: 2025-01-30 (Joel Lipman) - Initial release Date Modified: ??? ??? More Information: Custom View ID: 123456000012345678 ******************************************************************************* */ // // initialize v_CountTotal = 0; v_CountUpdated = 0; l_Pages = {1}; v_PerPage = 1; // // map to retrieve all records (to include approved and converted records, this needs to be specified as the query value) m_IgnoreRestrictions = Map(); m_IgnoreRestrictions.put("approved","both"); m_IgnoreRestrictions.put("converted","both"); // for each v_Page in l_Pages { l_Deals = zoho.crm.getRecords("Deals", v_Page, v_PerPage, {"cvid":123456000012345678}); for each m_Deal in l_Deals { if(!isNull(m_Deal.get("id"))) { v_CountTotal = v_CountTotal + 1; if(m_Deal.get("Stage").equalsIgnoreCase("Cancelled")) { // // cancel all related quotes // (...) // // cancel all related sales orders (including those that have been approved or converted) l_RelatedSOs = zoho.crm.getRelatedRecords("SalesOrders", "Deals", m_Deal.get("id"), 1, 200, m_IgnoreRestrictions); l_UpdateSOs = List(); for each m_SO in l_RelatedSOs { m_UpdateSO = {"id":m_SO.get("id"), "Status":"Cancelled"}; l_UpdateSOs.add(m_UpdateSO); } r_UpdateRelated = zoho.crm.bulkUpdate("Sales_Orders", l_UpdateSOs); // // cancel all related invoices // (...) // // mark the deal as processed r_UpdateDeal = zoho.crm.updateRecord("Deals", m_Deal.get("id"), {"Cancelled_Related_Records":true}, {"trigger":{}}); if(!isNull(r_UpdateDeal.get("id"))) { v_CountUpdated = v_CountUpdated + 1; } } } } }
- /* *******************************************************************************
- Function: string standalone.fn_Deal_UpdateRelatedToCancelled()
- Label: Fn - Customer Deals - Cancel Related Records
- Trigger: Standalone
- Purpose: For any deal which is cancelled, cancel its related sales orders.
- Inputs: -
- Outputs: -
- Date Created: 2025-01-30 (Joel Lipman)
- - Initial release
- Date Modified: ???
- ???
- More Information:
- Custom View ID: 123456000012345678
- ******************************************************************************* */
- //
- // initialize
- v_CountTotal = 0;
- v_CountUpdated = 0;
- l_Pages = {1};
- v_PerPage = 1;
- //
- // map to retrieve all records (to include approved and converted records, this needs to be specified as the query value)
- m_IgnoreRestrictions = Map();
- m_IgnoreRestrictions.put("approved","both");
- m_IgnoreRestrictions.put("converted","both");
- //
- for each v_Page in l_Pages
- {
- l_Deals = zoho.crm.getRecords("Deals", v_Page, v_PerPage, {"cvid":123456000012345678});
- for each m_Deal in l_Deals
- {
- if(!isNull(m_Deal.get("id")))
- {
- v_CountTotal = v_CountTotal + 1;
- if(m_Deal.get("Stage").equalsIgnoreCase("Cancelled"))
- {
- //
- // cancel all related quotes
- // (...)
- //
- // cancel all related sales orders (including those that have been approved or converted)
- l_RelatedSOs = zoho.crm.getRelatedRecords("SalesOrders", "Deals", m_Deal.get("id"), 1, 200, m_IgnoreRestrictions);
- l_UpdateSOs = List();
- for each m_SO in l_RelatedSOs
- {
- m_UpdateSO = {"id":m_SO.get("id"), "Status":"Cancelled"};
- l_UpdateSOs.add(m_UpdateSO);
- }
- r_UpdateRelated = zoho.crm.bulkUpdate("Sales_Orders", l_UpdateSOs);
- //
- // cancel all related invoices
- // (...)
- //
- // mark the deal as processed
- r_UpdateDeal = zoho.crm.updateRecord("Deals", m_Deal.get("id"), {"Cancelled_Related_Records":true}, {"trigger":{}});
- if(!isNull(r_UpdateDeal.get("id")))
- {
- v_CountUpdated = v_CountUpdated + 1;
- }
- }
- }
- }
- }
Source(s):
Add comment