Yet another blog

Recordtype Selection screen not available for selection

There was an issue on one the client orgs where one user was not able to select recordtype selection screen. Every time she clicked on the new button it bypassed the recordtype selection screen opened the record with default recordtype.

First i checked if that user profile has access to recordtype, which the user profile had access to. After some head banging it turned out that the user had selected default record type in the My settings page.

Click on the name of the user and from the drop menu click on My settings

Click on Display & Layout -> Set Default recordtypes  

and uncheck the checkbox against the object which you want the recordtype selection screen to appear.

Hope this helps




System.LimitException: Too many SOQL queries: 101 – while moving into production

This exception is one of the common governor limits that a salesforce developer experiences. Generally we would get this exception when the code is not written properly . ie. You have a SOQL query with in a for loop.

But many a times while deploying a trigger or class to production i get ths error. I check and double check if there were any SOQL queries within a for loop. But nothing in the class or trigger has the SOQL with in a loop and this is reflected in a different test class other than the code you are moving into production.

This happens because we would not have used the Test.startTest() and Test.stopTest() methods in our test classes. All the DML operations for setting the data for execution of test should be done before the Test.startTest() method and all the actions for testing your code should be between Test.startTest() and Test.stopTest() methods.

What this does is that it gives context to your test class. In other words only the code executed between the Test.startTest() and Test.stopTest() would be considered in the governor limit calculations. If there are no Test.startTest() and Test.stopTest() then all the setup code and actual test code would count towards the limits.

Capture only the Validation Rule Error message to display in VF page


I had a scenario where i have a VF page which saves records into a custom object. And i was having the controller code in a try catch block to capture the exceptions and was using the getmessage to display the error messages.

But this all the time spit out a lot of ugly stuff along with the error message

” Upsert Failed : First exception on row 0: first error;


All i wanted to show was that Quantity cannot be empty. thats exactly what the end user only wanted to know and all that crap of Upsert failed doesnt make any sense at all.

I had thought of capturing the error message in a string and split it with FIELD_CUSTOM_VALIDATION_EXCEPTION. then displaying the second item in the list to show the error message.

All this seem to be too complicated for this. Indeed there is something very simple to do this.

All you got to do is to add

<apex:pageMessages />

in your VF page. this will capture all your validation errors and diplay it.



Read an email sent to salesforce and read them as records

I was asked to work on a requirement which required that an email is sent to salesforce and we would need to read through the email and create a new case based for each row in the email. This email is generated from a external SQL server and emailed everyday

Here is the sample email

11111|Case1|AC123|Ac_001|9|4|Wed Aug 10 09:02:03 2011|1|completed

11112|Case2|AC123|Ac_001|9|4|Wed Aug 10 09:02:03 2011|1|completed

11113|Case3|AC123|Ac_001|9|4|Wed Aug 10 09:02:03 2011|1|completed

11114|Case4|AC123|Ac_001|9|4|Wed Aug 10 09:02:03 2011|1|completed

11115|Case5|AC123|Ac_001|9|4|Wed Aug 10 09:02:03 2011|1|completed

Go go the solution, first we must create a email service which will create an email id to which the emails are to be sent.

You can create the email service by going to Setup->Develop->email services

Before you create the email service we would need to create a class which this email service would use.

Goto Setup->Develop->Apex Classes

Create a class with following code

global class myHandler implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {

Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
return result;

Now lets create a email service.

Setup->Develop->email services->

click on the button New Email service

To know about the various fields refer the docs here

Make sure you select the apex class you created earlier. After the email service is created you should scroll down to the bottom of the screen to find the email address to which the emails need to be sent.

Now open up the class which you had selected while creating the email service. Here is the code i wrote.

global class ProcessCaseEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    String myPlainText= '';
    myPlainText = email.plainTextBody;
    System.debug('Plain text+++++++++++' + myPlainText );
    List<String> parts = myPlainText.split('\n\n');

    List<String> columns;
    for(integer i=0;i<parts.size();i++)

    System.debug('Parts+++++     ['+i+']   '+parts[i] );
    system.debug('Length of the parts ['+i+']  '+parts[i].length());
    if (parts[i].length()<9)

     System.debug('Parts+++++     ['+i+']   '+parts[i] );

     System.debug('columns size++++++++++++  '+columns.size());
     string acc='';
         System.debug('++++++acc after removing non print chars'+acc);

     List<Account> ac = [select id, name from account where accountid__c=:acc];
     System.debug('acc +++++++++++'+ac);

     for(integer k=0;k<ac.size();k++)

     System.debug('+++++++++++++++matched account++++++++++++++++++');
     case c = new case();
     insert c;

   // }

         return result;


Lets break the code down

To read the content of the email we are using the following code

myPlainText = email.plainTextBody;

Here is where we store the body of the email into a string
Another very important thing is that we need to give a system.debug to get an idea of how the email body is received. In some instance the rows are divided by two lines or one line. Depending on how your email is sent we can break the string to rows by using the code In my case it was a two lines.

 List parts = myPlainText.split('\n\n');

We have a list with all the records.
Now that we have all the records lets break the rows into columns I have the columns seperated by | symbol
So first lets loop through the all the records one by one and break each row into columns and then create a case for that record.

One other challenge i faced was when i was breaking the string into rows, there was a possibility that were blank rows as the email would have had multiple blank rows.
I have taken care of it using the if statement

  if (parts[i].length()<9)

I use 9 coz the no of columns in record is 8. So if the length is less than 9 that would mean a blank record. This may not be the right method. You could break the row into columns and count the no of columns if its less than no of column for the record then its a invalid record and ignore the record.
Here is how you break the records into columns


There is one other thing i am doing i am looking for a account which is related by a custom field. So from one of the column i search for the account which matches the custom field and give the account id to the case so that the new case is related to the account.

Since we are taking the values from email and breaking the records into columns based on the pipe symbol. There value of the column is can contain blank characters and non printing characters which would mean you would not find a matching record in account.

 List ac = [select id, name from account where accountid__c=:acc];

So make sure you trim and remove all non printing charaters.


I know in my case the column just had only numbers and alphabets so i used the above regex you can change the regex according to your requirements.

This really is my first detailed post so if you think there is something i can improve upon do drop a comment.

I currently work for CM-Focus

Change Tab labels in salesforce

For changing Tab labels go to

Setup->Customize->Tab Names and Labels->Rename Tabs and Labels

Click Edit on the tab which you want to rename…

Day of week in apex code of Salesforce

I was looking out for way of getting the day of week of a particular date. On searching on the documentation i couldnt find a way of doing it.

Here is one way of doing it.

There is a function called toStartOfWeek() which returns the start of the week for the Date that called the method, depending on the context user’s locale. For example, the start of a week is Sunday in the United States locale, and Monday in European locales. For example:

date myDate =;
date weekStart = myDate.toStartofWeek();

Now we know the week start date which is going to Monday for European locales and Sunday for US locate. Now we can build up the weekdays for the rest of the week by adding 1 or the no of days, for example you add 2 days to weekstart to get the date for wednesday..

Also i am told there is a much simpler solution though i have not tried it myself.

String shortDayOfweek =‘eee’); // ‘wed’

good luck

Dependent Picklist in salesforce

to create a dependent picklist we need atleast 2 picklists

Setup ->Object->Objectname->Field dependencies

Click on new
Choose the controlling field
Choose the Dependent field

Choose the dependent items under each controlling fields picklist value.

Hope this helps

Add Notes and Attachments to an existing object in salesforce

I already created an custom object but when i tried to add notes and attachment to that object i couldnt find a way to do it. Looks like you have the option of doing it while creating the object.. After a bit of searching i found a way to do it.

Go to the page Layout and click Related List… Wow… There is the elusive Notes and Attachment, you can place it on the layout and its visible back on your page

How to add new item into existing picklist in Salesforce

Sometime back i had to add a new item to an existing picklist in a custom object. Strangely i couldnt find an option to add a new item. I was sure i was overlooking something very basic here. There definetely should be an option to add item.

What i was doing was clicking on the Edit link next to the picklist, this didnt have option to add item.
To Add a new item one should click on the picklist name and scroll down to the bottom of the page where you have the Picklist Values detail from where you can add new, Replace,Reorder etc in a picklist

What is PIVOT and UNPIVOT?

You can use the PIVOT and UNPIVOT relational operators to change a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output. UNPIVOT performs the opposite operation to PIVOT by rotating columns of a table-valued expression into column values.

The following example returns the number of purchase orders placed by employee IDs 164, 198, 223, 231, and 233, categorized by vendor ID.

USE AdventureWorks
SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp4, [233] AS Emp5
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) AS p
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [164], [198], [223], [231], [233] )
) AS pvt

Here is a partial result set:

VendorID Emp1 Emp2 Emp3 Emp4 Emp5
1 4 3 5 4 4
2 4 1 5 5 5
3 4 3 5 4 4
4 4 2 5 5 4
5 5 1 5 5 5

To unpivot the table, assume the result set produced in the previous example is stored as pvt. The query looks like the following.

–Create the table and insert values as portrayed in the previous example.
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
Emp3 int, Emp4 int, Emp5 int);
INSERT INTO pvt VALUES (1,4,3,5,4,4);
INSERT INTO pvt VALUES (2,4,1,5,5,5);
INSERT INTO pvt VALUES (3,4,3,5,4,4);
INSERT INTO pvt VALUES (4,4,2,5,5,4);
INSERT INTO pvt VALUES (5,5,1,5,5,5);
–Unpivot the table.
SELECT VendorID, Employee, Orders
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM pvt) AS p
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt

Here is a partial result set:

VendorID Employee Orders
1 Emp1 4
1 Emp2 3
1 Emp3 5
1 Emp4 4
1 Emp5 4
2 Emp1 4
2 Emp2 1
2 Emp3 5
2 Emp4 5
2 Emp5 5