+ Start a Discussion
NecroGigglesNecroGiggles 

SQOL query OR command in code not working.

I need to edit this SQOL query in this code so that it only looks at task that are (status = complet) and (Type = up to 6 diffrent values)" I asume I need to use a or statmnet but I am having no luck getting it to work. I also need to remove the part were it looks for Events. I am haveing no luck with this any help would be great. 

 
public with sharing class OpportunityCallActivityCount {
 
public static Boolean didRun = false;
public static String oppPrefix = Opportunity.sObjectType.getDescribe().getKeyPrefix();
 
/*
* Takes a set of opportunity IDs, queries those opportunities, and updates the activity count if appropriate
*/
public static void updateOpportunityCounts(Set<ID> oppIds) {
 
if (didRun == false) { //We only want this operation to run once per transaction.
didRun = true;
 
//Query all the opportunites, including the tasks child relationships
List<Opportunity> opps = [SELECT ID, Email_Activities__c, (SELECT ID FROM Tasks where Type='Email' AND Status='Completed'), (SELECT ID FROM Events) FROM Opportunity WHERE ID IN :oppIds];
List<Opportunity> updateOpps = new List<Opportunity>();
 
for (Opportunity o : opps) {
Integer count = o.tasks.size() + o.events.size();
 
if (o.Call_Activity__c != count) {
o.Call_Activity__c = count;
updateOpps.add(o); //we're only doing updates to opps that have changed...no need to modify the others
}
}
 
//Update the appropriate opportunities
try {
update updateOpps;
} catch (Exception e) {
//This is controversial. Anything could happen when updating the opportunity..validation rule, security, etc. The key is we don't
//want the event update to fail...so we put a try catch around the opp update to make sure we don't stop that from happening.
}
}
}
}

 
Best Answer chosen by NecroGiggles
Mahesh DMahesh D
Hi 

Please find below the modified code:
 
public with sharing class OpportunityCallActivityCount {
 
	public static Boolean didRun = false;
	public static String oppPrefix = Opportunity.sObjectType.getDescribe().getKeyPrefix();
	 
	/*
	* Takes a set of opportunity IDs, queries those opportunities, and updates the activity count if appropriate
	*/
	public static void updateOpportunityCounts(Set<ID> oppIds) {
	 
		if (didRun == false) { //We only want this operation to run once per transaction.
			didRun = true;
			 
			//Query all the opportunites, including the tasks child relationships
			List<Opportunity> opps = [SELECT ID, Email_Activities__c, (SELECT ID FROM Tasks where Type IN ('Email', 'Email1', 'Email2', 'Email3', 'Email4', 'Email5') AND Status='Completed'), (SELECT ID FROM Events) FROM Opportunity WHERE ID IN :oppIds];
			List<Opportunity> updateOpps = new List<Opportunity>();
			 
			for (Opportunity o : opps) {
				Integer count = o.tasks.size() + o.events.size();
				 
				if (o.Call_Activity__c != count) {
					o.Call_Activity__c = count;
					updateOpps.add(o); //we're only doing updates to opps that have changed...no need to modify the others
				}
			}
			 
			//Update the appropriate opportunities
			try {
				update updateOpps;
			} catch (Exception e) {
				//This is controversial. Anything could happen when updating the opportunity..validation rule, security, etc. The key is we don't
				//want the event update to fail...so we put a try catch around the opp update to make sure we don't stop that from happening.
			}
		}
	}
}

Regards,
Mahesh

All Answers

venkat-Dvenkat-D
Try this and replace 1 to 6 with your values.
List<Opportunity> opps = [SELECT ID, Email_Activities__c, (SELECT ID FROM Tasks where (Type='1' OR Type = '2' OR Type = '3' OR Type = '4' OR Type = '5' OR type = '6') AND Status='Completed') FROM Opportunity WHERE ID IN:oppIds];
Mahesh DMahesh D
Hi 

Please find below the modified code:
 
public with sharing class OpportunityCallActivityCount {
 
	public static Boolean didRun = false;
	public static String oppPrefix = Opportunity.sObjectType.getDescribe().getKeyPrefix();
	 
	/*
	* Takes a set of opportunity IDs, queries those opportunities, and updates the activity count if appropriate
	*/
	public static void updateOpportunityCounts(Set<ID> oppIds) {
	 
		if (didRun == false) { //We only want this operation to run once per transaction.
			didRun = true;
			 
			//Query all the opportunites, including the tasks child relationships
			List<Opportunity> opps = [SELECT ID, Email_Activities__c, (SELECT ID FROM Tasks where Type IN ('Email', 'Email1', 'Email2', 'Email3', 'Email4', 'Email5') AND Status='Completed'), (SELECT ID FROM Events) FROM Opportunity WHERE ID IN :oppIds];
			List<Opportunity> updateOpps = new List<Opportunity>();
			 
			for (Opportunity o : opps) {
				Integer count = o.tasks.size() + o.events.size();
				 
				if (o.Call_Activity__c != count) {
					o.Call_Activity__c = count;
					updateOpps.add(o); //we're only doing updates to opps that have changed...no need to modify the others
				}
			}
			 
			//Update the appropriate opportunities
			try {
				update updateOpps;
			} catch (Exception e) {
				//This is controversial. Anything could happen when updating the opportunity..validation rule, security, etc. The key is we don't
				//want the event update to fail...so we put a try catch around the opp update to make sure we don't stop that from happening.
			}
		}
	}
}

Regards,
Mahesh
This was selected as the best answer