Insert Record by Using Visualforce Page and Apex Class

In below example I’m inserting Account object record using Visualforce Page and Apex Class.

Visualforce Page:

<apex:page controller="CreateAccountController">
                <apex:inputfield value="{!acc.Name}"/>
                <apex:inputfield value="{!acc.Accountnumber}"/>
            <apex:commandbutton action="{!SaveMethod}" value="Save"/>

Apex Class:

public class CreateAccountController{
    public Account acc {get;set;}
    public CreateAccountController(){        
        acc = new Account();
    //Save Method 
    public PageReference SaveMethod(){
        Insert acc;
        return null;

Mass Delete Button for ListView in Salesforce

Sometimes we need a Mass Delete kind of button. Where, we can select records in ListView and simply click “Delete” button for mass delete of records.

Here is an example of “Mass Delete” ListView button to delete multiple records. In below example I’ve implemented the “Mass Delete” button on Lead object.

Go to Setup || Object || Buttons, Links, and Actions || New Button or Link || Fill the ListView Button required information as shown in below image.

Fill the below JavaScript code in JavaScript Code Section:


//Add ObjectType, on which object you want to add the list view button .
var records = {!GETRECORDIDS($ObjectType.Lead)

if (records[0] == null) {
    alert("Please select at least one record to delete.")
} else {

    var opt = confirm("Are you sure you want to delete selected records ?");
    if (opt == true) {
        var errors = [];
        var result = sforce.connection.deleteIds(records);
        if (result && result.length) {
            var nFailed = 0;
            var nSucceeded = 0;
            for (var i = 0; i < result.length; i++) {
                var res = result[i];
                if (res && res.success == 'true') {
                } else {
                    var es = res.getArray("errors");
                    if (es.length > 0) {
            if (nFailed > 0) {
                alert("Failed: " + nFailed + " and Succeeded: " + nSucceeded + " n Due to: " + errors.join("n"));
            } else {
                alert("Number of deleted records: " + nSucceeded);

Now add the above “Mass Delete” ListView button to ListView Layout.
Go to Setup || Object || Search Layouts || List View Layout || Edit || Add the Custom button to List View layout as shown in below image || Save

Now we can delete mass records from List View.

Test Class For Apex Trigger

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

Apex Trigger:

trigger AccountTrigger on Account (before Insert, before Update) {
    Map<String, Account> accMap = new Map<String, Account>();
    for (Account acc : {
        //Make sure you 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('An account already exist with 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 already exist with same name.');

Here is the Test Class for the above apex trigger.
Test Class:

private class TestAccountTriggers {

    static testMethod void AccountTriggerUnitTest() {
        Account acc1 = new Account();
        acc1.Name = 'ABC Corp Ltd';
        acc1.Type = 'Prospect';
        acc1.Industry = 'Banking';
        Insert acc1;
        Account acc2 = new Account();
        acc2.Name = 'ABC Corp Ltd';
        acc2.Type = 'Prospect';
        acc2.Industry = 'Apparel';
        	Insert acc2;
        catch(System.DMLException e)
        	System.assert(e.getMessage().contains('An account already exist with same name.'));

Convert String To Lowercase And Uppercase In Salesforce Apex

Salesforce provides a number of methods to manipulate strings in apex. You can actually play with strings in apex using these methods. Lets see how we can convert a string to a upper case and lower case in the example below.

toLowerCase(): method is to convert all of the characters in the String to lowercase.

String name = 'Biswajeet Samal';
System.debug('Lowercase-' + name.toLowercase());

toUpperCase(): method is to convert all of the characters in the String to uppercase.

String name = 'Biswajeet Samal';
System.debug('Uppercase-' + name.toUppercase());