How to calculate the sum of a user’s activity duration in minutes, for all activities in the last 60 days , and set the value in a field (entity schema query)
It can be achieved with the help of the business process. First of all you should add the "boolean" type field on the contact's page and on the activity page and name it for example "Is processed". After that you should create a business process with the "Simple" start event that will read "Contact" object, after that there should be "Read data" element that will read "Activity" object with the data read mode set as "Calculate function" and specify the function value as "Sum" by column "Duration (minutes)" and filter activities by field "Owner" that should have Id from the first "Read data" element that reads "Contacts" object. After that "Modified data" element should be added for the "Activity" object that should change the value of boolean field to "True". Then should go "Modify data" element that will modify data in contact section and will set value of the previous element to the specified field and that will change the value of the boolean field added at the beginning to "True" value and this element should be also interconnected via activity Id in the "How to filter records" block. After that you should make a cycle of this process and it should look like on the screenshot http://prntscr.com/kyx20n (conditional flow 2 should satisfy the condition that boolean field for "Contact" object is "False", conditional flow 4 should satisfy the condition that boolean field for "Activity" object is "False" so to read all activities and all contacts). Don't forget to filter activities by Start and End dates so to get all activities for the last 60 days.
I found that query method Execute can be run with DBExecutor passed as parameter and without it. Do you know what's the difference? I didn't find documentation explaining this.
DBExecutor is a class that manages connection to the database. It is called always when you work with queries. By default every time you execute ESQ query, Select, Delete or any other query a new instance of this class is created. You can pass an existing DBExecutor instance just for optimization in order not to create an extra object or for using transactions. For example, if you need to run 3 queries in a row, you can create one DBExecutor and use it again in all these quires. How to create a new instance of this class and use it correctly you can read here
So I want to add couple of objects to database using EntitySchemaQuery and I want to reuse the same database connection. Is what I'm doing here correct?
Your code seems too complex and I cannot predict if it works. You can try it to check the correctness of this code. The one thing I can tell: you cannot directly use Execute method on Entity.
Please, be advised that in 99.9% cases you do not need a dbExecutor class. First, when using dbExecutor directly with Select, Update, Create, Delete classes you bypass any events such as running business processes or validation at application level so you may finish with incorrect object state in the DB. You also bypass rights check because you execute a direct sql query.
Second, the overhead of using every time a new dbExecutor is very small
In your case just use entity.save() without dealing with dbExecutor at all.
So after refactoring your code will look like this: