Salesforce Certificate

Friday 12 July 2013

Conditionally Override "New" (Salesforce Standard button) as per Record Type Selection

Override "New" (Salesforce Standard button) as per RecordType Selection

Following is the snippet of anonymous code which will help you to override Salesforce standard "New" button as per RecordType selected by user.

Here is the example for Overriding standard "New" button of Account Object(Any sObject) as per RecordType Selection.

Step 1:
Override your Standard button of Account Object("New") with custom visualforce page to which you want to redirect page.

Step 2:
Create one Custom Label with Name = "Custom_page_rectype". Put here your custom recordtype value (hard coded) For which you want to override conditionally.

Step 3:
Put action attribute in your visualforce page's <apex:page> tag as below.

Visualforce Page:
<apex:page standardController="Account" extensions="extCtrlRecordtyperedirect"
action="{!if(strRecTypeName==$Label.Custom_page_rectype,null,
URLFOR($Action.Account.Edit,
$ObjectType.Account,null,true))}">

This is Your custom visualforce Page

</apex:page>

Apex Class: 
public class extCtrlRecordtyperedirect{ 
     Public string strRecTypeName {get; set; }
     public extCtrlRecordtyperedirect(ApexPages.StandardController controller) {
          strRecTypeid = apexpages.currentpage().getparameters().get('RecordType');
          if(strRecTypeid !=  null){
            Schema.DescribeSObjectResult d = Schema.SObjectType.Account;
            Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
            strRecTypeName = rtMapById.get(strRecTypeid).getName();
        }
}

Schedule Apex to run Every 24 HOUR


Schedule Class To - Run at Every 24 HOUR Everyday..

Following is the snippet of anonymous code which will help you to schedule your apex Job to run Every HOUR.

Write below code in "Developer Console" to schedule class for every One Hour Everyday.


Apex Code:
ScheduleclassEveryhour objschclass = new ScheduleclassEveryhour ();
string strSeconds = '0';
string strMinutes = '0';
string strHours = '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23';

string strDay_of_month = '*';
string strMonth = '*';
string strDay_of_week = '?';

String sch = strSeconds + ' ' + strMinutes + ' ' + strHours + ' ' + strDay_of_month + ' ' + strMonth + ' ' + strDay_of_week;
system.schedule('ScheduleclassEveryhour', sch, objschclass);

Wednesday 17 April 2013

Display sObject Fields list in picklist of visualforce page without using controller



If you want to display picklist with any sObject field list in your visualforce page. you can do it using javascript without using Apex controller. No need to write class.

Here is the example for displaying all the field of Account in picklist.
Visualforce Page:
<apex:page>
    <apex:includeScript value="/soap/ajax/16.0/connection.js"/>
    <label id="ldfield" style="font-weight:bold">Account Fields: </label>
    <select id="selectNumber">
    </select>
     <script>
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        //Global Object
        var describeResults = sforce.connection.describeSObject("Account");
        //select option list
        var select = document.getElementById("selectNumber");
        for(var i = 0; i < describeResults.fields.length; i++) {
            var fieldList = describeResults.fields[i];     
            var el = document.createElement("option");
            el.textContent = fieldList.label;
            el.value = fieldList.Name;
            select.appendChild(el);       
        }
    </script>
</apex:page>

Tuesday 2 April 2013

Too Many Query Rows 50001 & collection exceeds maximum size: 1001


Page: 

<apex:page controller="ctrlCustomcontroller" readOnly="true">
</apex:page>

If your controller does not have any DML operation than you can easily solve above two salesforce governer limits using the attribute readOnly="true" by default it's value is false.

1. Query Rows limits increased from 50001 to 1 million rows
2. collection size limits increased from 1001 to 10,000