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.

Dynamically Add Delete Rows In PageBlockTable Visualforce Page

Apex Class:

public class DemoController {
    
    Public List<Account> accList {get;set;}
    public Integer rowIndex {get;set;}
    
    public DemoController(){
        accList = new list<Account>();
        accList.add(new Account());
    }
    
    public void addRow(){
        accList.add(new Account());
    }
    
    public pagereference saveAccount(){
        Insert accList;
        return null;
    }
    
    public PageReference deleteRow(){
        rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
        accList.remove(rowIndex);
        return null;
    }
}

Visualforce Page:

<apex:page controller="DemoController">
    <apex:form id="fm">
        <apex:pageBlock id="pb">
            <apex:variable var="rowNumber" value="{!0}"/> 
            <apex:pageBlockTable value="{!accList}" var="ac">
                <apex:column headerValue="Account Name">
                    <apex:inputField value="{!ac.Name}"/>
                </apex:column>
                <apex:column headerValue="Account Number">
                    <apex:inputField value="{!ac.AccountNumber}"/>
                </apex:column>
                <apex:column headerValue="Account Type">
                    <apex:inputField value="{!ac.Type}"/>
                </apex:column>
                <apex:column headerValue="Industry">
                    <apex:inputField value="{!ac.Industry}"/>
                </apex:column>
                <apex:column headerValue="Action" >
                    <apex:commandButton value="Delete" action="{!deleteRow}" reRender="pb">
                        <apex:param name="rowIndex" value="{!rowNumber}"/>
                    </apex:commandButton>
                    <apex:variable var="rowNumber" value="{!rowNumber}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Row" action="{!addRow}"/>
                <apex:commandButton value="Save Accounts" action="{!saveAccount}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

Test Setup Method in Salesforce Apex

In Spring ’15 release Salesforce has introduced new @testSetup annotation for Test class method. @testSetup annotation defined methods are used for creating common test records, and that records are available for all test methods in the class.

@testSetup methods create common test data easily and efficiently, and reduce test execution times especially when we are working with many records.

Example:

@iSTest
private class AccountControllerTest {
    
    //Test Setup Method
    @testsetup
    static void createAccount() {
        Account acc = new Account();
        acc.Name = 'Salesforce';
        insert acc;
    }
    
    //Test Method
    public static testMethod void test() {
        System.assertEquals(1, [SELECT COUNT() FROM Account Where Name = 'Salesforce']);
    }
}

Considerations:

  • Records created by @testSetup method are available to all test methods in the test class.
  • @testSetup methods are executed first in the test class before any test methods.
  • One Test class can have only one @testSetup method. If multiple @testSetup method is written in a Test class then sequence of execution of those methods are not guaranteed.
  • If the test class or a test method has access to organization data by using the @isTest(SeeAllData=true) annotation, test setup methods aren’t supported in this class.
  • If any error occurss during the execution in @testSetup method, then entire test class fails.
  • The data created by @testSetup methods will be rolled back after the completly execution of the test class.
  • If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

Salesforce Check Permission Set in Custom Button

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}

var result = sforce.connection.query("SELECT Id FROM PermissionSetAssignment WHERE PermissionSet.Name = 'Sample_Permission_Set' AND AssigneeId = '{!$User.Id}'");
var psAssignment = result.getArray("records");

if (psAssignment.length === 1) {
    //Write your logic
} else {
    alert('You don't have access to this feature.');
}