Author Archives: Biswajeet

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.

Disable Copy and Paste in Visualforce Page

Visualforce Page:

<apex:page standardcontroller="Contact">
    <script>
        function DisableCtrlKey(e){
            
            var code = (document.all) ? event.keyCode:e.which;
            if (parseInt(code)==17){
                alert("Please re-type your email address");           
            }   
        }
    </script>
    <apex:form>
        <apex:pageblock title="Create Contact">
            <apex:pageblockbuttons>
                <apex:commandbutton value="Save" action="{!save}"/>
            </apex:pageblockbuttons>
            <apex:pageblocksection columns="1">
                <apex:inputfield value="{!Contact.Firstname}"/>
                <apex:inputfield value="{!Contact.LastName}"/>
                <apex:inputfield value="{!Contact.Email}"/>
                <apex:inputtext value="{!Contact.Email}" label="Re-Type Email Id" onkeydown="DisableCtrlKey(event)"/>              
                <apex:inputfield value="{!Contact.Phone}"/>              
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output

Different Ways of Making a Field Mandatory in Salesforce

There are 4 ways of making the field mandatory:

Page Layout: Field can be made mandatory from the page layout when it needs to be made mandatory for a set of users.
Field Level Security: Field can be made mandatory from the FLS when it needs to be made mandatory for all the users in the Organization and even from the API’s.
Validation Rule: Field can be made mandatory from the Validation Rule, when it needs to be made mandatory for user who is using the same Page layout used by other users.
Before Triggers: we can also make a field mandatory using before trigger

Note: Salesforce.com recommends using the Page Layout option for making the field mandatory.

Avoid Duplicate Record Using Apex Trigger

Here in below example the trigger is on “Account” object, to check duplicate Account Names.

Apex Trigger:

trigger accountDuplicateCheck on Account (before Insert, before Update) {
    
    Map<String, Account> accMap = new Map<String, Account>();
    
    for (Account acc : System.Trigger.new) {
        
        //Make sure we don't treat account Name that isn't changing during an update as a duplicate.  
        if (System.Trigger.isInsert || (acc.Name != System.Trigger.oldMap.get(acc.Id).Name)) {
            
            //Make sure another new account isn't also a duplicate  
            if (accMap.containsKey(acc.Name)) {
                acc.Name.addError('Another account has the ' + acc.Name + ' same name.');
            } else {
                accMap.put(acc.Name, acc);
            }
        }
    }
    
    //Query to find all the Accounts in the database that have the same name as any of the Accounts being inserted or updated.  
    for (Account acc : [SELECT Name FROM Account
                        WHERE Name IN :accMap.KeySet()]) {
                            Account newAcc = accMap.get(acc.Name);
                            newAcc.Name.addError('An account with this name ' + acc.Name + ' already exists.');
                        }
}