System.QueryException : Unexpected token ‘(‘ or Unexpected token ‘{‘

I had a problem with doing a Batch Update of OwnersId using Apex Batch.

You can do the following:


Set<String> accNames = new Set<String>();
accNames = ReassignIncorpsAfter60Days.returnAccountIdForContactWithTaskOlder60Days();
System.debug(Database.query('Select id, (Select id from Account.Contacts), (Select id from Account.Opportunities) from Account where Id IN :accNames'));

Note: Make sure the variable or method is available (public) in the method it is called. Below is an example of implementing String sets in a SOQL query, I pass the Set into the conductor.


global with sharing class ReassignIncorpsAfter60Days implements Schedulable{
global void execute(SchedulableContext sc) 
	 {
  	      List<String> createDynamicUpdateString = new List<String>();
           createDynamicUpdateString.add('Select id, (Select id from Account.Contacts), (Select id from Account.Opportunities) from Account');
           createDynamicUpdateString.add('OwnerId');
           createDynamicUpdateString.add('');
           createDynamicUpdateString.add('true');
           createDynamicUpdateString.add('');
           createDynamicUpdateString.add('email@address.com');

           ID batchprocessid = Database.executeBatch(new DynamicUpdate(createDynamicUpdateString, ReassignIncorpsAfter60Days.returnAccountIdForContactWithTaskOlder60Days()));
     }
}

global class DynamicUpdate implements Database.Batchable, Database.AllowsCallouts {

	private String query;
	private String field;
	private String value;
	private String sms;
	private String email;
	private Boolean random;
	private Set<String> accountIds;

	private static final String emailAddress = 'your@email.com';
	//Map<String, Map> fromTofieldNameToBeUpdated;
        global DynamicUpdate(List<String> dynamicUpdateFieldsAndValues, Set<String> accountIds) {
		this.query = dynamicUpdateFieldsAndValues[0];
		this.field = dynamicUpdateFieldsAndValues[1];
		this.value = dynamicUpdateFieldsAndValues[2];
		this.random = Boolean.valueOf(dynamicUpdateFieldsAndValues[3]);
		this.sms = dynamicUpdateFieldsAndValues[4];
		this.email = dynamicUpdateFieldsAndValues[5];
		this.accountIds = new Set<String>(accountIds);
	}
        global Database.QueryLocator start(Database.BatchableContext BC) {
		if (!accountIds.isEmpty())
		  return Database.getQueryLocator(query + ' where Id IN : accountIds');
		else
		  return Database.getQueryLocator(query);
	}
        global void execute(Database.BatchableContext BC, List scope) {
   		if (random)
   		{
   			List<User> activeSalesReps = ACRoundRobinV5.getListOfActiveSalesReps('Receiving_Leads__c', 'Last_Record_Received__c');
			for (sObject updateAccountContactOpportunities : scope)
			{	
				value = activeSalesReps[Integer.ValueOf(Math.random() * activeSalesReps.size()-1)].Id;
				updateAccountContactOpportunities.put(field, value);
			}
		}
		else
		{
			for (sObject updateAccountContactOpportunities : scope)
			{
				updateAccountContactOpportunities.put(field, value);
			}
		}
		update scope;
	}

	global void finish(Database.BatchableContext BC) {
		AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Phone, CreatedBy.Email FROM AsyncApexJob WHERE Id =:BC.getJobId()];

		if (a.CreatedBy.Phone != null && sms.equalsIgnoreCase('true'))
		{
			TwilioRestClient client = new TwilioRestClient('324324324234324324', '324324324324324234324234234');

		Map params = new Map {
 		'To'   => a.CreatedBy.Phone ,
        	'From' => '+1408627625',
        	'Body' => 'Salesforce owner reassignment was ' + a.Status + '. Details: ' + a.TotalJobItems +' batches with '+ a.NumberOfErrors + ' failures.'
    		};
    		TwilioSMS sms = client.getAccount().getSMSMessages().create(params);

		}
		else if (a.CreatedBy.Email != null && email.equalsIgnoreCase('true'))
		{

   			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   			String[] toAddresses = new String[] {a.CreatedBy.Email};
   			mail.setToAddresses(toAddresses);
   			mail.setSubject('Salesforce owner reassignment was ' + a.Status);
   			mail.setPlainTextBody('Salesforce owner reassignment details: ' + a.TotalJobItems +' batches with '+ a.NumberOfErrors + ' failures.');
   			Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

		}
	}
}

1 Comment

  1. j says:

    Bradley sera prêt à tout Chaves doit jeter à lui, et il a un net avantage quand il vient à speed.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s