Sunday 22 March 2015

CPU Time Out


Salesforce.com introduced a new governor limit called CPU Timeout in winter 14, what exactly is CPU Time out and how its get calculated and whats get counted for CPU Timeout.

As per Salesforce.com documentation CPU Timeout is calculated for all executions on the Salesforce application servers occurring in one apex transaction - for executing apex code and all the process called from code such as workflows, validation rules, package code.

CPU Timeout is private for transaction and is isolated from other transactions.

CPU Timeout for Synchronous apex:   10,000 milliseconds
CPU Timeout for Asynchronous apex: 60,000 milliseconds 

CPU Timeout Counted for:
1. Workflows
2. Validation Rule
3. Library functions exposed in apex code

CPU Timeout do not count for:
1. SOSL
2. SOQL
3. DML
4. Http callouts

CPU Timeout can be explained as Time taken by the apex transaction in a context including Apex code, Validation rules, Workflows excluding time taken in DML, SOSL, SOQL.

Wednesday 11 March 2015

Select Option Sorting

List is an ordered collection of elements which can be of any data type - primitive types, collections, sObjects, that are distinguished by their indices.

First element indices in List is always 0.

we can sort the list using sort() method. For primitive data type sorting is always done in ascending order.

example :
List<String> names = new List<String>{ 'Pawan','Vishal','Vivek','Rishabh','Dinda', 'pawan'};
system.debug(' List before Sorting : ' + names);
names.sort();
system.debug(' List after Sorting : ' + names);


sorted list will be
(Dinda, Pawan, Rishabh, Vishal, Vivek, pawan)
 
 
 




SelectOption is also sorts in ascending order using value and label fields  in sequence

1. The value of fields is used for sorting.
2. If value of fields have the same value or both are empty then label is used.

NOTE: empty fields precede non-empty fields

example:

List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('A','India'));
options.add(new SelectOption('C','Japan'));
options.add(new SelectOption('A','USA'));
options.add(new SelectOption('','China'));
System.debug('SelectOption Before sorting: ' + options);
options.sort();
System.debug('SelectOption After sorting: ' + options);



Before sorting: (System.SelectOption[value="A", label="India", disabled="false"], 
                 System.SelectOption[value="C", label="Japan", disabled="false"], 
                 System.SelectOption[value="A", label="USA", disabled="false"], 
                 System.SelectOption[value="", label="China", disabled="false"])


After sorting: (System.SelectOption[value="", label="China", disabled="false"], 
                System.SelectOption[value="A", label="India", disabled="false"], 
                System.SelectOption[value="A", label="USA", disabled="false"], 
                System.SelectOption[value="C", label="Japan", disabled="false"])




Monday 9 March 2015

Load Test Data from Static Resource

we write test class to test out functionality and in test class we create test data, but in Salesforce using Test.loadData method, you can populate data in our test method without writing any code.

To load test data from Static Resource we have to create a .csv file and upload it to Static Resource

copy and paste the below data and save the file in .csv extension.

Name,Website,Phone,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry
sForceTest1,http://www.sforcetest1.com,(415) 901-7000,The Landmark @ One Market,San Francisco,CA,94105,US
sForceTest2,http://www.sforcetest2.com,(415) 901-7000,The Landmark @ One Market Suite 300,San Francisco,CA,94105,US
sForceTest3,http://www.sforcetest3.com,(415) 901-7000,1 Market St,San Francisco,CA,94105,US


go to Static Resource [ Setup -> Develop -> Static Resource ]
create new Static Resource record, give Static Resource Name as "Test Records"
and save it.

After saving the csv file in static resource go to Apex Class and write a test class with below code, save it and run test class to see the result.


@isTest
private class TestDataLoadClass {
    
    static testMethod void loadTestData() {
        List<sObject> ls = Test.loadData(Account.sObjectType, 'testRecords');
        
        system.assertEquals(ls.size(), 3);
        
        Account acc1 = (Account)ls[0];
        system.debug(' Account Name ' + acc1.Name);
        system.debug(' Account Name ' + acc1.Id);
    }
}