Tag Archives: Apex

Check Current User has a Custom Permission in Salesforce Using Apex

we can use FeatureManagement.checkPermission method, to determine which users have access to a specific custom permission.

Sample Code:

Boolean hasCustomPermission = FeatureManagement.checkPermission('YOUR_CUSTOM_PERMISSION_API_NAME');

Converting DateTime to Date in Salesforce

Solution 1: Create a date newInstance() and pass the Year, Month and Day from the dateTime.

DateTime dt = System.now();
Date d = Date.newInstance(dt.year(), dt.month(), dt.day());
System.debug('Print Date:' + d);

Solution 2: Get date from the DateTime using Date() method.

DateTime dt = System.now()
Date d = dt.date();
System.debug('Print Date:' + d);

Get Record Type Info In Apex

Get Record Type Id by Name:

Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner Account').getRecordTypeId();

Get Record Type Id by Developer Name:

Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Partner_Account').getRecordTypeId();

Get Record Type Developer Name by Name:

String recordTypeDevName = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner Account').getDeveloperName();

Get Record Type Developer Name by Id:

String recordTypeDevName = Schema.SObjectType.Account.getRecordTypeInfosById().get('01258000000DKIg').getDeveloperName();

Get Record Type Name by Developer Name:

String recordTypeName = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Partner_Account').getName();

Get Record Type Name by Id:

String recordTypeName = Schema.SObjectType.Account.getRecordTypeInfosById().get('01258000000DKIg').getName();

Redirect To Standard New Note & New Attachment Pages Using Apex

Redirect to New Attachment:

public PageReference redirectToNewAttachment() {
   PageReference pgRef = new pageReference('/p/attach/NoteAttach?pid=' + String.valueof(acc.Id).subString(0, 15) + '&retURL=' + acc.Id);
   pgRef.setRedirect(true);
   return pgRef;
}

Redirect to New Note:

public PageReference redirectToNewNote() {
   PageReference pgRef = new pageReference('/002/e?parent_id=' + String.valueof(acc.Id).subString(0, 15) + '&retURL=' + acc.Id);
   pgRef.setRedirect(true);
   return pgRef;
}

Inline Visualforce Page Needs To Redirect

Apex Class:

public class AccountExtensionController {

    public String redirectUrl {get; set;}
    public Boolean isRedirect {get; set;}
    private Account acc {get; set;}
    
    public AccountExtensionController(ApexPages.StandardController sc) {
        this.acc = (Account)sc.getRecord();
    }
    
    public PageReference redirectToNewContact() {
        isRedirect = true;
        redirectUrl = '/003/e?retURL=' + acc.Id + '&accid=' + acc.Id;
        return null;
    }
}

Visualforce Page:

<apex:page standardController="Account" extensions="AccountExtensionController" showHeader="false" sidebar="false">
    <apex:form >
        <apex:commandButton value="New Contact" action="{!redirectToNewContact}" rerender="redirectPanel" />
        <apex:outputPanel id="redirectPanel" >
            <apex:outputPanel rendered="{!isRedirect}">
                <!--redirect using javascript-->
                <script type="text/javascript">
                window.top.location.href = '{!redirectUrl}';
                </script>
            </apex:outputPanel>
        </apex:outputPanel>
    </apex:form>
</apex:page>