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
ABakABak 

Governor limits in apex

I am hoping someone can guide me with accomplishing this.

 

The Contact Record in salesforce is associated with the ActivityHistory (Child relationship with contact table)

I want to run through all the contacts and get the activity histories filtered with a certain criteria and then based on the subject of the task create a new instance of an Object and Insert it.

 

pseudo code for what i want to do is as follows:

 

 

e.g 
query - Select Id ,accountID (Select Subject From ActivityHistories where Subject like '%KE:%') from  Contact.
RecTypeQuery - Select name, Id from RecordType where sObjectType = KeyEvent;

For each(Contact con in queryResult)
{
	For each(ActivityHistory task in con.task)
	{
		KeyEvent ke  = new KeyEvent();

		For each(rec in RecType)
		{
			if(rec.Name == task.Subject)
			ke.recordTypeId = rec.Id;
		}
		insert ke;


	}
}

 

 

 

If I run a regular scheduled job (script) , I am sure I will hit the governer limit as there are over 200000 contacts.

 

I am not sure if I can do this with batch apex (not sure how the sub queries are handled and if we can insert new records with batch apex) , if yes then any example would be really helpful.

 

I cant use a data loader mass Insert as each new record that I want to create has a specific record type based on the subject of the task.

 

If someone can guide me how to accomplish this , it will be great

 

Thanks

 

bob_buzzardbob_buzzard

Batch apex sounds like the right route here.  Using this mechanism you'll process a maximum of 200 contacts at a time through your execute method.  You can then construct a query based on the contact ids currently being processed to retrieve the activity histories.  You may need to use a SOQL query loop depending on the volume of activity data.  You can certainly create/update/delete records in batch apex - I've got  a batch job that deletes the records passed into the execute method.  One attraction of batch apex is that if you hit governor limits, you can reduce the batch size until you find an acceptable value.

 

There's good information and example code in the apex developer's guide around page 147

 

ABakABak

Hi , 

 

Thanks for the reply. Could you get me an example or some pseudo code for this particular issue. I know there are examples and documentation on batch apex online bu they are usually really simple examples that anyone can figure out how to do.

 

Any helo woiuld be greatly appreciated

 

Thanks