function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajnisfRajnisf 

SOQL query need help urgent...thanks

 

this query returns the sum of all the tasks on Sales_Resources__c object...but i want to get the sum of closed tasks of particular record...

if object has 5 records and record 1 contain 2 closed tasks..then i want to get the sum of only those two tasks...

\but this query returning the sum of all the tasks of this object's records...

can sm1 help...

 

aggregateResult[] results  = [Select SUM(Minutes_on_tasks__c)summ From Task WHERE status = 'Completed' AND Minutes_on_tasks__c != null AND whatid IN (SELECT Id FROM Sales_Resources__c)];

Best Answer chosen by Admin (Salesforce Developers) 
Flint LockwoodFlint Lockwood

Hi, 

 

Use the Group By clause in your query. 

 

aggregateResult[] results  = [Select Id, SUM(Minutes_on_tasks__c)summ From Task WHERE status = 'Completed' AND Minutes_on_tasks__c != null AND whatid IN (SELECT Id FROM Sales_Resources__c) GROUP BY Id];

 This should give you the sum of the Minutes on Task field for particular records. 

All Answers

Jeff MayJeff May

If there are Tasks associated with a record related to the Sales_Resources__c record, your SOQL should query Task for that WhatID.  Currently, the SOQL is querying for all Sales_Resource Tasks, and not tasks assosiated with that other record.

RajnisfRajnisf

thanks for ur reply

 

can u change this query...? i m not able to do that

Vinit_KumarVinit_Kumar

Try below :-

 

aggregateResult[] results  = [Select SUM(Minutes_on_tasks__c)summ From Task WHERE status = 'Completed' AND Minutes_on_tasks__c != null AND whatid IN (SELECT Id FROM Sales_Resources__c where name='<some Sales_Resources__c record name>')];

RajnisfRajnisf

but we have 100's of records in this object..how can i give all these names...but yes they are linked to record type...

Flint LockwoodFlint Lockwood

Hi, 

 

Use the Group By clause in your query. 

 

aggregateResult[] results  = [Select Id, SUM(Minutes_on_tasks__c)summ From Task WHERE status = 'Completed' AND Minutes_on_tasks__c != null AND whatid IN (SELECT Id FROM Sales_Resources__c) GROUP BY Id];

 This should give you the sum of the Minutes on Task field for particular records. 

This was selected as the best answer
RajnisfRajnisf
thanks for replying

not working...i tried Group by whatid..it is working on some records and giving abnormal results on other records...
m not able to understand where the problem is...
RajnisfRajnisf
it is working fine on first and second record...but on third record...result is of second record...similarly on 4th record ...result is of 1st record.....dnt know wats gng in...
Flint LockwoodFlint Lockwood

How are you referencing the results of the query?