I have a Javascript button, I need to check the number of records having same Owner ID through a SOQL aggregate query. ActIds contains the all selected Activity__c records IDs

var ActIds = [];

var AggregateResult_Owner = sforce.connection.query("SELECT OwnerId, count(Id) FROM Activity__c WHERE Id IN  (\'' + ActIds .join('\',\'') + '\')  GROUP BY OwnerId"); 

On click of button, this query is throwing an error 'MALFORMED QUERY'

Any help will be appreciated

  • From where is the ActIds array been set with the values – RCS Apr 9 '18 at 9:06

You can create a String by iterating over the AccIds array in Javascript and then you can add that string into the SOQL query in the where clause

var selectedAccIds=''; 
for(var i=0;i < ActIds.length; i++)
selectedAccIds = selectedAccIds.substring(0,selectedAccIds.length - 1);

var queryStr = "SELECT OwnerId, count(Id) FROM Activity__c WHERE Id IN ("+selectedAccIds+") GROUP BY OwnerId"; 

var AggregateResult_Owner = sforce.connection.query(queryStr);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.