A company recently purchased a salesforce instance to monitor and track all customer sales activities.
Their business process involves creating orders and order products to track customer orders. The shipping/billing department is relying on these records to perform their duties.
A salesforce developer was tasked with writing apex code to report the total quantity of a product ordered by a set of customers.
The code functions properly in lower environments. But when deployed to production, it throws an unhandled exception, "Too many SOQL queries: 101". And it is causing business disruptions in processing orders.
Review the provided apex code and address the business requirement by following salesforce best practice to avoid the governor limit exception "Too many SOQL queries."
Input params
ListId
Output
Integer