+ Start a Discussion

COUNT Query total pushed to another object

Hi All,


Im beginning to learn Apex, so bare with me! ive been faced with a curly one to start me off with learning Apex and i am a little stuck already. Here is the situation:


* 2 objects involved - Customer Satisfaction Index (will call it CSI here on in) and Tasks.

* On the CSI object, there are a number of Tasks that need to be completed by the Account Owner. These tasks are created via workflow from the CSI Object and are therefore Tasks

* What i require is a COUNT of all the Tasks that have the status as "Completed" relating to that CSI to be displayed in the field Completed_Tasks_c on the CSI object.


Im having trouble with the COUNT part of it displaying in the Completed Tasks field.


Any help? Thanking you all in advance.







Tell me if this is what you're looking for:



Integer count = [SELECT count() FROM Task WHERE task.CSI__c = :csi.id AND task.status = 'Completed']


 csi.id is the Id of the current CSI__c object your working with.








Yeap, sure is, its a good start so thanks for that. So from what i understand, that will ge the count of the Completed Tasks. How would i push that figure into the field 'Completed Activities__c'?


As you can tell im a novice, but i will be using this code as a base for a few other similar requirements on other objects.


Appreciate your help on this matter.




To which object does that field belong?


Assuming you are creating a new object:



myObject__c obj = new myObject(completed_Activities__c=count); insert obj;

 I'm not sure that this is what you're asking for though.







Hey, even i am working on something similar. I need to get the count of Activities under an account updated in a field at account level. I tried making the code based on the example given by you....did i kept on getting errors.


trigger Test on Task (After Insert,After Update,After Delete)
Integer num_act;
num_act=[Select count() from Task where task.What = :Account.id AND task.status = 'Completed'];


Error: Compile Error: No such column 'What' on entity 'Task'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 4 column 9


I have tried putting "Related To", "Account" and lots of other combinations; didn't help.

Message Edited by Cvent on 07-29-2009 02:32 AM

Am working on this, too. Can anyone assist? trying to get the task count pushed to a custom parent object.\


trigger CountVC on Task (After Insert,After Update,After Delete)
Integer num_act;
num_act=[Select count() from Task where task.Whatid = :CustomObject__c.id AND task.status = 'Completed'];


and get

 Error: Compile Error: Invalid bind expression type of Schema.SObjectField for column of type Id at line 4 column 56


Message Edited by JAW99 on 11-30-2009 11:47 AM
Vipin  PulinholyVipin Pulinholy
Did any one fix this? Could you please share the solution?

I, too, am self-teaching Apex here (counting occurrence of "an email address" on all Leads in this case).   My only problem SEEMS to be returning the value I accumulate to a Field on the Lead.   My biggest hurdle on learning the very beginnings of Apex is setting up actual "access to the objects" to begin with.  I've written hundreds of lines of code of logic for things where the baseline trigger is already written, but the whole List/ Map/ Manager portion of it has confounded me so far.


My trigger is simple - the asterisked area is the item I do not know how to do.   :-( 



trigger CountEmails on Lead (After Insert,After Update,After Delete) {

Integer num_act;
num_act=[Select count() from Lead where lead.Email = 'test@test.com'];
** lead.LastName = num_act; **