Eclipse/Class save error – This Apex class has batch or future jobs pending or in progress

I was constantly getting an error “This Apex class has batch or future jobs pending or in progress” whenever I was saving Apex class written for batch Apex. This was happening in the developer org where we develop a test managed package for the AppExchange product. There was no way I can throw out this org and start with a new one.

For debugging started with following steps:

  1. Check Schedule Jobs – No schedule jobs (If any are running and related to your class somehow you can just delete the schedule job)
  2. Check Apex jobs – No Apex is job is running or in Queued state. (If are running you could just click “Abort”)
  3. Google – Found some known issue and checked workarounds which were of nouse.

As nothing worked logged a case through Salesforce Partner portal as job needed to be deleted from the backend. But case got categorized as a developer support case. Not having the premium support it Case got closed 😐


Then started research –

  • Tried deleting all running schedule jobs through Apex in case any is stuck in the background and not visible on UI.

No Luck!! 🙁

  • Making query on the AsyncApexJob object – SELECT Id, Status, JobItemsProcessed, TotalJobItems, ParentJobId, NumberOfErrors FROM AsyncApexJob Where Status = 'Queued' et voila!! returns the job stuck in Queued status which was not visible through UI. I thought my job is over I will just copy Id from the query result and execute “System.abortJob(jobid);” But that didn’t work. It needs a ParentJobId which was missing from this entry. – No Luck 🙁
  • Then came across a tiny line in the Salesforce Article. If you want to abort a job using Job Id use API version 32.0 or earlier. Login to workbench using v32.0 and from “Execute anonymous” execute System.abortjob(). This time it worked… Finally!!! 🙂

Salesforce: Trigger on Attachment – Restricting user from attaching files with specific extensions

By default, Salesforce doesn’t allow admin to configure or restrict a user from adding a specific type of files. Admin may need that user should not be able to attach files of type exe, dll which can have the virus in them. Also, there is no virus check is done when a file is getting uploaded to the Salesforce.

But Salesforce does allow writing trigger on the Attachment Object by which you can implement such restrictions. Following is the snippet of the code which will restrict the user from adding the files with extension mentioned in the set list. You can also edit the code and do the reverse by checking if extension exists in the set then only allow attaching the file.

Once trigger code is up and running, whenever Salesforce tries to attach any file with extension exe or dll he will come across following error message.


Creating multiple tabs for same Salesforce Object

Many time I have seen Salesforce Admins wants multiple tabs for same object depending upon the RecordTypes or some other criteria like separate tabs for Premium Account, Gold Accounts and Silver. Indeed they can create multiple views and use the picklist and use “Go” button. But it has disadvantage like multiple clicks, not that user friendly.

In short there is no direct way of creating multiple tabs for same object in Salesforce. Then I thought of creating an Apex component which will take some inputs like ObjectName, Fields to display and criteria/Filter. This appraoch was needing too much of coding and generic code writing for supporting it for all objects. And it was limited to the query filter one can have.

Creating multiple views is the feature inbuilt provided by the Salesforce and is very powerful. So I thought to use the apex:enhancedList control which takes the viewId as a parameter. You can find the documentation for the EnhancedList here. The most important thing is apex:enhancedlist only requires the viewid.

I created a simple VisualForce Page like below.

<apex:page >
<apex:enhancedList height=”600″ rowsPerPage=”25″ id=”AccountList” listid=”{!$CurrentPage.Parameters.viewId}” customizable=”false”/>

I wanted to reuse this page so I didn’t added any standard controller to the Page itself.

Then now comes the step of creating the Tabs. Sample purpose I am taking Account’s views.

Salesforce support following three types of tab creation:

  1. Custom Object Tabs: – Not suitable to above approach.
  2. VisualForce Tabs: – This will work but do not allow passing the parameters from the URL, so this will not allow reuse of the same page.
  3. Web Tabs: – This allows any URL integration into the tab and suits requirement.
Complex task is to find the viewId. I didnt found it straight forward.
Creating the Tab now:
Now add this tab to your SelectedTabs.

Salesforce: Adding specific working days to a Date

UPDATE: If you want to consider holidays and business hours you can check out this article.

Many time while coding we come across the requirement in which we need to calculate the EndDate from a StartDate after a specific number of business/working days. That means excluding the weekends (Saturday & Sunday) Following is the code snippet which does this. I know there can be an optimized way of doing this. But I didn’t want to spend more time on this.

Below method takes date and number of working days to be added to the date.

Method daysOff calculates the number of non-working days coming in the start date and end date. And after which we are again adding those number of days.