Category Archives: Salesforce

Salesforce where clause of field1 = field2 of same object

You might wanted to bring your records like Select id from Account where Column1 = Column2, which is not possible in Salesforce.
For this you have to create a formula field as Text and then populate this field as TRUE if the column1 = column2 else populate the value as FALSE.
Finally you can write a query as:

select id from Account where formulafield = 'TRUE'

Compare Old And New Values In Salesforce Trigger

Salesforce provides trigger.oldmap where in all the old records are stored in map with keyset as their ids. We can compare old field value of records with the new values in trigger.

Here in the below example, the trigger compares the account number old value with the new value. If the account number is changed the trigger assigns the type field value “Customer – Direct” else it assigns it a value “Customer – Channel”.

trigger AccountTrigger on Account (before update) {
    
    for (Account acc: Trigger.new) {
        Account oldAccount = Trigger.oldMap.get(acc.Id);
        if(acc.AccountNumber != oldAccount.AccountNumber) {
            System.debug('--Account Number is changed--');
            System.debug('--Old Account Number -' + oldAccount.AccountNumber);
            System.debug('--New Account Number -' + acc.AccountNumber);
            acc.Type = 'Customer - Direct';
        }
        else{
            System.debug('--Account Number has not been updated--');
            acc.Type = 'Customer - Channel';  
        }
    }
}

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...

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: