Sunday, 30 October 2011

Bulikfy Apex Code / Trigger


Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.  If a batch of records (via the webservices call) invokes the same Apex code, all of those records need to be processed as a bulk, in order to write scalable code and avoid hitting governor limits.
eg.
trigger accountTestTrggr on Account (before insert, before update) {

   List<String> accountNames = new List<String>() {
 
   //Loop through all records in the Trigger.new collection
   for(Account a: Trigger.new){
      //Concatenate the Name and billingState into the Description field
      a.Description = a.Name + ':' + a.BillingState
   }
   
}

Dynamic Visualforce bindings

Dynamic Visualforce binding is useful to write generic Visualforce pages that display information about records without knowing which fields to show. In other words, fields on the page are determined at runtime, rather than compile time.

To create a new field set:


Navigate to the field set list of the appropriate object:
    • For standard objects, click Your Name | Setup | Customize, select the appropriate object from the Customize menu, and click Field Sets.
    • For custom objects, click Your Name | Setup | Create | Objects, and select one of the custom objects in the list. Then click New above the field sets related list.


eg.
<apex:page standardController="Contact">

    <apex:form >
        <apex:repeat value="{!$ObjectType.Contact.FieldSets.Demo_Field_Set}" var="f">
            <apex:InputText value="{!Contact[f]}" /><br/>
        </apex:repeat>
    </apex:form>
</apex:page>

Salesforce Security

Security in Salesforce is defined at multiple levels and these levels are -
  1. Security at object level (Profile)
  2. Security at field level (Profile)
  3. Security at record level
    1. Organization-wide defaults
    2. Role-hierarchy
    3. Sharing rules
    4. Manual Sharing



Profile: Object & field level security can be setup via profile and CRUD permissions can be set for standard & custom objects.

OWD: Used to restrict the permission.,  

All profiles get at least the privileges defined in OWD. OWD takes three different values -
  1. Private (Cant view and edit)
  2. Public Read only (Can view)
  3. Public Read-Write (Can view and edit)
Manual Sharing:
Manual Sharing is used to grant one-off access. Manual sharing can be granted by record owner, any one above the owner in role hierarchy and System Administrator. Manual sharing is used to handle exception cases where access to a particular record needs to be given to a specific user. There is a Sharing button on the records page. This is used to provide manual sharing. 

Role Hierarchy:
Role Hierarchy allows additional users access to records.    If the role has access to some records, then it's parents and ancestors are able to access the record as well.

Sharing Rules:
Share between roles in diff. hierarchy branches.    Sharing rule is defined using public groups. Record that match certain condition can be assigned to users in public groups using Sharing Rules. Sharing rules functionality is available via the menu Sharing Settings.

How to compare date in Dynamic SOQL

First convert the date value into datetime value, because format method can't used with the Date Type.
Date orig = customPage.startDate;


DateTime dtConverted = Datetime.newInstance(orig.year(),orig.month(),orig.day(),0,0,0);
       
System.debug(' converted date ' + dtConverted.format('yyyy-MM-dd\'T\'h h:mm:ss\'Z\'');




String qry = 'Select id, name, CreatedDate from Account ' +
        'where createddate > ' +  dtConverted.format('yyyy-MM-dd\'T\'h h:mm:ss\'Z\'') + ' order by name ';


List<Account> searchResults = Database.query(qry);

Saturday, 29 October 2011

Find Day of the week in Salesforce

Any of the following date formats can be used to calculate the day of the week .


String shortDayOfweek = DateTime.now().format(‘EEE’); // ‘Sat’
String longDayOfweek = DateTime.now().format(‘EEEEE’); // ‘Saturday’

How to Create a Visualforce page

* Salesforce application is easy to develop

* You can register a 30 day free development environment & start developing your first helloworld application
      http://developer.force.com/

* First enable the Development mode
     i) Click Setup
     ii) From Personal Setup ->
                  Click My Personal Information -> Personal Information
                 
                  Enable the "Development Mode" checkbox

* You can create the visualforce page by entering the url as the following  (by appending /apex/helloword to the salesforce url).
              https://na12.salesforce.com/apex/helloword

* Since this page is not exists, system will display the message to create the page

* Click the click to create the page

* The helloworld page will be created and the visual force code will be displayed in the programming pane.

You can modify the code as the following and the see the result.   It's so simple.

<apex:page>
    <h1> My first Hello World Program </h1>
<apex:page>