Welcome to Portal

?Unknown\pull-down

Welcome to Zoho Cares

Bienvenido a Soporte de Zoho

Search our knowledge base, ask the community or submit a request.

Hi,

I have a Deluge script, which works perfectly. We have a custom field in the Campaigns module called Total_Raised which looks at the Donation_Amount field in the custom module, Donations. The idea is to sum up the total of all donation amounts.

However, it stops at 200 records, the Zoho default cut-off, as I understand it. Is there any way of being able to search through more than 200 records, possibly up to 2000? My existing working code is:

  1. void automation.UpdateDonationTotal(Int campaignId)
  2. {
  3.     if(campaignId != null)
  4.     {
  5.         // Fetch related donations for the given Campaign ID
  6.         donations = zoho.crm.getRelatedRecords("Donations", "Campaigns", campaignId, 1, 200);

  7.         // Initialize the total amount
  8.         totalRaised = 0.0;

  9.         // Iterate over each donation and sum the Donation Amount
  10.         for each donation in donations
  11.         {
  12.             if(donation.get("Donation_Amount") != null)
  13.             {
  14.                 totalRaised = totalRaised + donation.get("Donation_Amount");
  15.             }
  16.         }

  17.         // Fetch the current Campaign record to compare Total_Raised
  18.         campaignRecord = zoho.crm.getRecordById("Campaigns", campaignId);
  19.         existingTotal = campaignRecord.get("Total_Raised");

  20.         // Debugging log
  21.         info "Campaign ID: " + campaignId;
  22.         info "Total Raised (calculated): " + totalRaised;
  23.         info "Existing Total Raised: " + existingTotal;

  24.         // Update only if the calculated value differs from the existing value
  25.         if(existingTotal != totalRaised)
  26.         {
  27.             // Prepare data for updating the Campaign's Total Raised field
  28.             updateMap = Map();
  29.             updateMap.put("Total_Raised", totalRaised);

  30.             // Update the Campaign record
  31.             response = zoho.crm.updateRecord("Campaigns", campaignId, updateMap);

  32.             if(response.get("code") == "SUCCESS")
  33.             {
  34.                 info "Successfully updated Total Raised for Campaign ID: " + campaignId;
  35.             }
  36.             else
  37.             {
  38.                 info "Failed to update Total Raised for Campaign ID: " + campaignId + ". Response: " + response;
  39.             }
  40.         }
  41.         else
  42.         {
  43.             info "No update needed. Total Raised is already correct.";
  44.         }
  45.     }
  46.     else
  47.     {
  48.         info "Invalid Campaign ID: " + campaignId;
  49.     }
  50. }
Is there any way we can search more than 200 records and make the calculation? 

1 user has this question.
4 Replies
Reply
  • Zoho MVP
  • 22 days ago

Hi


You can take advantage of a COQL Query. It will also allow you to perform aggregate functions like SUM so that will help save some time and allow you to perform it in one call.


Hope that helps.

Hello,

Thank you Marshall for pitching here.

You can also find the updated script below, this can loop through 2K records in related list

void automation.UpdateDonationTotal(Int campaignId)
{
    pages = [1,2,3,4,5,6,7,8,9,10];
    
    if(campaignId != null)
    {
        // Fetch related donations for the given Campaign ID
        for each page in pages
        {
        donations = zoho.crm.getRelatedRecords("Donations", "Campaigns", campaignId, page, 200);
        
        // Initialize the total amount
        totalRaised = 0.0;
        
        if(donations!=null)
        {
            
        
        // Iterate over each donation and sum the Donation Amount
        for each donation in donations
        {
            if(donation.get("Donation_Amount") != null)
            {
                totalRaised = totalRaised + donation.get("Donation_Amount");
            }
        }

        // Fetch the current Campaign record to compare Total_Raised
        campaignRecord = zoho.crm.getRecordById("Campaigns", campaignId);
        existingTotal = campaignRecord.get("Total_Raised");

        // Debugging log
        info "Campaign ID: " + campaignId;
        info "Total Raised (calculated): " + totalRaised;
        info "Existing Total Raised: " + existingTotal;

        // Update only if the calculated value differs from the existing value
        if(existingTotal != totalRaised)
        {
            // Prepare data for updating the Campaign's Total Raised field
            updateMap = Map();
            updateMap.put("Total_Raised", totalRaised);

            // Update the Campaign record
            response = zoho.crm.updateRecord("Campaigns", campaignId, updateMap);

            if(response.get("code") == "SUCCESS")
            {
                info "Successfully updated Total Raised for Campaign ID: " + campaignId;
            }
            else
            {
                info "Failed to update Total Raised for Campaign ID: " + campaignId + ". Response: " + response;
            }
        }
        else
        {
            info "No update needed. Total Raised is already correct.";
        }
        
        }
        
        }
    }
    else
    {
        info "Invalid Campaign ID: " + campaignId;
    }
}

Check and let us know if you have any further clarification.

  • 10 days ago

Apologies for a late reply. Thanks for the suggestions.
 your code was very close. Below is the final version, which works properly for me. Thanks for the help.

  1. void automation.UpdateDonationTotalForCampaign(Int campaignId)
  2. {
  3. if(campaignId != null)
  4. {
  5. info "Starting UpdateDonationTotalForCampaign function for Campaign ID: " + campaignId;
  6. // Initialize the page number for donations
  7. pages = {1,2,3,4,5,6,7,8,9,10};
  8. // Define up to 10 pages for fetching donations
  9. totalRaised = 0.0;
  10. // Initialize total amount
  11. // Fetch donations for the given Campaign ID (handle pagination)
  12. for each  page in pages
  13. {
  14. donations = zoho.crm.getRelatedRecords("Donations","Campaigns",campaignId,page,200);
  15. if(donations != null && donations.size() > 0)
  16. {
  17. // Iterate over each donation and sum the Donation Amount
  18. for each  donation in donations
  19. {
  20. if(donation.get("Donation_Amount") != null)
  21. {
  22. totalRaised = totalRaised + donation.get("Donation_Amount");
  23. }
  24. }
  25. }
  26. else
  27. {
  28. // If no donations are returned, exit the loop (pagination finished)
  29. break;
  30. }
  31. }
  32. // Fetch the current Campaign record to compare Total_Raised
  33. campaignRecord = zoho.crm.getRecordById("Campaigns",campaignId);
  34. existingTotal = campaignRecord.get("Total_Raised");
  35. // Debugging log
  36. info "Total Raised (calculated): " + totalRaised;
  37. info "Existing Total Raised: " + existingTotal;
  38. // Update only if the calculated value differs from the existing value
  39. if(existingTotal != totalRaised)
  40. {
  41. // Prepare data for updating the Campaign's Total Raised field
  42. updateMap = Map();
  43. updateMap.put("Total_Raised",totalRaised);
  44. // Update the Campaign record
  45. response = zoho.crm.updateRecord("Campaigns",campaignId,updateMap);
  46. // Check if the update was successful (check for "id" in the response)
  47. if(response.containsKey("id"))
  48. {
  49. info "Successfully updated Total Raised for Campaign ID: " + campaignId;
  50. }
  51. else
  52. {
  53. info "Failed to update Total Raised for Campaign ID: " + campaignId + ". Response: " + response;
  54. }
  55. }
  56. else
  57. {
  58. info "No update needed. Total Raised is already correct.";
  59. }
  60. }
  61. else
  62. {
  63. info "Invalid Campaign ID: " + campaignId;
  64. }
  65. }

Hello Chris,

Glad to know you were able to resolve this and thank you for sharing this updated code here in the community. Sure others with similar requirement will find this useful.

Much appreciated!

Reply to ChrisA
/* */
  • 12
  • Insert
  • Plain text
Add Comment
(Up to 20 MB )