PradysTechBlog

Yet another blog

Category Archives: Salesforce

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.

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)
    {

    }
    else
    {
     System.debug('Parts+++++     ['+i+']   '+parts[i] );
     columns=parts[i].split('\\|'); 

     System.debug('columns size++++++++++++  '+columns.size());
     string acc='';
     acc=columns[2].trim();
     acc=acc.replaceAll('[^\\d\\w]','');
         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();
     c.incident__c=columns[0];
     c.subject=columns[1];
     c.account__c=columns[2];
     c.created__c=columns[3];
     c.events__c=columns[4];
     c.type__c=columns[5];
     c.begin_date__c=columns[6];
     c.status__c=columns[7];
     c.notification_status__c=columns[8];
     c.AccountID=ac[k].id;
     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

 columns=parts[i].split('\\|'); 

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.

 acc=acc.replaceAll('[^\\d\\w]','');

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.today();
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 = DateTime.now().format(‘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