Pass Value From Visualforce Page To Controller

Controller:

Public with sharing class Sample {
    
    Public string myInput{get;set;}
    Public string myOutput{get;set;}
    
    Public void myMethod(){
        myOutput = myInput;
    }
}

Visualforce Page:

<apex:page controller="Sample">
    <!--Pass parameters from visualforce page to controller-->
    <apex:form>
        <apex:pageblock >
            Input Here - <apex:inputText value="{!myInput}"/>
            <apex:commandButton value="Submit" reRender="outputID" action="{!MyMethod}"/> 
        </apex:pageblock>
        <apex:pageblock >
            <b>Output Here - </b><apex:outputText value="{!myOutput}" id="outputID">
            </apex:outputText>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading...

Difference between Sql Server Char and Varchar Data Type

Everyone knows about the basic difference between CHAR and VARCHAR data types. In this article apart from the basic difference, will discuss on one more interesting difference.
CHAR Data Type is a Fixed Length Data Type. For example if you declare a variable/column of CHAR (10) data type, then it will always take 10 bytes irrespective of whether you are storing 1 character or 10 character in this variable or column. And in this example as we have declared this variable/column as CHAR(10), so we can store max 10 characters in this column.
On the other hand VARCHAR is a variable length Data Type. For example if you declare a variable/column of VARCHAR (10) data type, it will take the number of bytes equal to the number of characters stored in this column. So, in this variable/column if you are storing only one character then it will take only one byte and if we are storing 10 characters then it will take 10 bytes. And in this example as we have declared this variable/column as VARCHAR (10), so we can store max 10 characters in this column.
Below example illustrates the basic difference explained above:

DECLARE
@CharName Char(20) = 'Biswajeet',
@VarCharName VarChar(20) = 'Biswajeet';
SELECT DATALENGTH(@CharName) CharSpaceUsed,
DATALENGTH(@VarCharName) VarCharSpaceUsed;

Result:
CharSpaceUsed    VarCharSpaceUsed
————-                —————-
20                            9
(1 row(s) affected)

Below is an interesting difference, which I have observed recently while writing some script. Concatenation of CHAR variables:

DECLARE
@FirstName Char(20) = 'Biswajeet',
@LastName Char(20) = 'Samal';
IF (@FirstName + ' ' + @LastName = 'Biswajeet Samal')
   PRINT 'I was Expecting'
ELSE
   PRINT 'Its different'
SELECT @FirstName + ' ' + @LastName AS Name,
LEN(@FirstName + ' ' + @LastName) AS Length

Result:
Its different
Name                           Length
————————–     ———
Biswajeet Samal        26
(1 row(s) affected)

Concatenation of VARCHAR variables:

DECLARE
@FirstName VarChar(20) = 'Biswajeet',
@LastName VarChar(20) = 'Samal';
IF (@FirstName + ' ' + @LastName = 'Biswajeet Samal')
  PRINT 'I was Expecting'
ELSE
  PRINT 'Its nice'
SELECT @FirstName + ' ' + @LastName AS Name,
LEN(@FirstName + ' ' + @LastName) AS Length;

Result:
I was Expecting
Name                                              Length
—————————————–      ———–
Biswajeet Samal                           17
(1 row(s) affected)

So, it is clear from the above examples that during concatenation of CHAR data type variables, it includes space in-place of unused space in the result of concatenation.

Clone a Record Using Apex in Salesforce

Salesforce sObjects have a method called clone() which creates a copy of the sObject record. This method has four Boolean type optional parameters.

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

Clone method parameters are as follows:

opt_preserve_id
Type: Boolean
Description: Determines whether the ID of the original object is preserved or cleared in the duplicate. If set to true, the ID is copied to the duplicate. The default is false, that is, the ID is cleared.

opt_IsDeepClone
Type: Boolean
Description: Determines whether the method creates a full copy of the sObject field, or just a reference:
If set to true, the method creates a full copy of the sObject. All fields on the sObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned sObject, the original sObject is not affected.
If set to false, the method performs a shallow copy of the sObject fields. All copied relationship fields reference the original sObjects. Consequently, if you make changes to a relationship field on the cloned sObject, the corresponding field on the original sObject is also affected, and vice-versa. The default is false.

opt_preserve_readonly_timestamps
Type: Boolean
Description: Determines whether the read-only timestamp fields are preserved or cleared in the duplicate. If set to true, the read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the duplicate. The default is false, that is, the values are cleared.

opt_preserve_autonumber
Type: Boolean
Description: Determines whether auto number fields of the original object are preserved or cleared in the duplicate. If set to true, auto number fields are copied to the cloned object. The default is false, that is, auto number fields are cleared.

Example:

Account acc = new Account(Name = 'Salesforce', Billingcity = 'Texas', Phone = '9999999999');
insert acc;    

//Cloning the above Account Record acc
Account accCloneCopy  = acc.clone(false, false, false, false);
insert accCloneCopy;

Picklist Without None Value in Visualforce Page

Controller:

public class Sample{
    
    public Account acc {get;set;}
    public List<SelectOption> typeOptions {get;set;}
    
    // Constructor called when page is accessed.
    public Sample() {
        
        acc = new Account();        
        typeOptions = new List<SelectOption>();
        
        //Use DescribeFieldResult object to retrieve status field.
        Schema.DescribeFieldResult typeFieldDescription = Account.Type.getDescribe();
        
        //For each picklist value, create a new select option
        for (Schema.PickListEntry pl:typeFieldDescription.getPicklistValues()){
            typeOptions.add(new SelectOption(pl.getValue(),pl.getLabel()));
            
            //Obtain and assign default value
            if (pl.defaultValue){
                acc.Type= pl.getValue();
            }  
        }     
    }
}

Visualforce Page:

<apex:page controller="Sample">
    Please select account type:
    <br/>
    <apex:form >
        <apex:selectList size="1" value="{!acc.Type}">
            <apex:selectOptions value="{!typeOptions}"/>
        </apex:selectList>
    </apex:form>
</apex:page>

Output:

How To: Reset Identity column in SQL Server

This is one of those simple tip posts that may seem obvious and taken for granted by those of us who have been working with MS SQL Server for a while now but maybe a newbie or two out there will find this helpful.

I find myself resetting an identity column value back to 0 after I’ve deleted all the existing records so the table gets a fresh start at primary key 1. Yes, I know all about primary keys not changing and how the value in the primary key doesn’t matter and so on. Sometimes I just like the primary keys starting at 1.

The following line resets the Identity value for the Customer table to 0 so that the next record added starts at 1.

DBCC CHECKIDENT(table_name, RESEED, new_reseed_value)

Example:

DBCC CHECKIDENT('Customer', RESEED, 0)