Category Archives: Salesforce

Accessing keys from a map in a SOQL Query

Using .KeySet() on a map, we will get a list of all the keys returned.

Map<Id, Master_Obj__c> masterMap = new Map<Id, Master_Obj__c>([SELECT Id, Name FROM Master_Obj__c]);
List<Detail_Obj__c> objDetailList = [Select Id, Name FROM Detail_Obj__c WHERE Master_Obj__c IN :masterMap.KeySet()]

How to cover pagereference method in test class

For Custom Controller


//Page reference to your VF Page
PageReference pageRef = Page.TestPage;
Test.setCurrentPage(pageRef);

//Pass necessary parameter
pageRef.getParameters().put('Id',id); 

//init controller 
CustomCtrl objCtrl = new CustomCtrl();

//Call pageRef mymethod
PageReference objPageRef = objCtrl.mymethod();

//Put system asserts
System.assertEquals (null,pageRef);

For Standard Controller

//First create record
Account acc = New Account();
acc.Name = 'Test Account';
INSERT acc;

//Page reference to your VF Page
PageReference pageRef = Page.TestPage;
Test.setCurrentPage(pageRef);

//Pass necessary parameter
pageRef.getParameters().put('Id',acc.id);   

//Pass your object to controller     
ApexPages.StandardController stc = new ApexPages.StandardController(acc);

//Call controller
CustomCtrl objCtrl = new CustomCtrl(stc);

//Call pageRef mymethod
PageReference objPageRef = objCtrl.mymethod();

//Put system asserts
System.assertEquals (null,pageRef);

Delete All Scheduled Jobs in Salesforce Org

Sample Code:

//Get all scheduled jobs
List<CronTrigger> ctList = [SELECT Id, CreatedDate, CronExpression, CronJobDetailId, EndTime,
                            NextFireTime, OwnerId, PreviousFireTime, StartTime, State,
                            TimesTriggered FROM CronTrigger];
//Abort all jobs one by one
for(CronTrigger ct:ctList){
    System.abortJob(ct.Id);
}

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

Salesforce Display Button On Apex Page Message

Apex Class:

public class SampleController {
    
    public Account acc {get;set;}
    public Boolean isButtonVisible {get;set;}
    public string message {get;set;}
    
    public SampleController(){
        isButtonVisible = false;
        acc = new Account();
        message = '';
    }
    
    public pageReference showMessage(){
        isButtonVisible = true;
        message = 'Do you want to create the account';
        return null;
    }
    
    public pageReference saveAccount(){
        insert acc;
        isButtonVisible = false;
        acc = new Account();
        return null;
    }
    
    public pageReference cancel(){
        isButtonVisible = false;
        acc = new Account();
        return null;
    }
}

Visualforce Page:

<apex:page controller="SampleController" tabStyle="Account">
    <apex:form >
        <apex:pageMessages escape="false"/> 
        <apex:outputPanel rendered="{!isButtonVisible}">
            <apex:pageMessage severity="info" strength="3" summary="{!message}">
                <apex:commandButton value="Yes" action="{!saveAccount}"/>
                <apex:commandButton value="No" action="{!cancel}"/>
            </apex:pageMessage>
        </apex:outputPanel>
        
        <apex:pageblock title="Account">
            <apex:pageBlockSection title="Account Information" columns="2">
                <apex:inputField value="{!acc.Name}"/>
                <apex:inputField value="{!acc.AccountNumber}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Confirm" action="{!showMessage}"> </apex:commandButton>
            </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

Custom Label in JavaScript File

We use {!$Label.CustomLabelName}, when we call custom label in Visualforce page JavaScript function. But if we need to access custom labels in JavaScript file in static resource, then here is the way to get the custom label value.

Load the custom label in Visualforce Page before loading the static resource JavaScript file.

<script>
    window.$Label = window.$Label || {};
    $Label.MyCustomLable1 = '{!JSENCODE($Label.MyCustomLable1)}';
    $Label.MyCustomLable2 = '{!JSENCODE($Label.MyCustomLable2)}';
</script>

Use in JavaScript file.

console.log($Label.MyCustomLable1);
alert($Label.MyCustomLable1);