Tag Archives: Apex

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>

Get Dependent Picklist Values in Apex

Sample Code:

//Pass dependent field parameter e.g.: Account.YourDependentField__c
public static Map<Object,List<String>> getDependentPicklistValues(Schema.sObjectField dependentField){
        Map<Object,List<String>> dependentPicklistValues = new Map<Object,List<String>>();
		//Get dependent field result
        Schema.DescribeFieldResult dependentFieldResult = dependentField.getDescribe();
		//Get dependent field controlling field 
        Schema.sObjectField controllerField = dependentFieldResult.getController();
		//Check controlling field is not null
        if(controllerField == null){
            return null;
        } 
		//Get controlling field result
        Schema.DescribeFieldResult controllerFieldResult = controllerField.getDescribe();
		//Get controlling field picklist values if controlling field is not a checkbox
        List<Schema.PicklistEntry> controllerValues = (controllerFieldResult.getType() == Schema.DisplayType.Boolean ? null : controllerFieldResult.getPicklistValues());
        
		//It is used to decode the characters of the validFor fields. 
        String base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
        
        for (Schema.PicklistEntry entry : dependentFieldResult.getPicklistValues()){
            if (entry.isActive()){
			//The PicklistEntry is serialized and deserialized using the Apex JSON class and it will check to have a 'validFor' field
                List<String> base64chars = String.valueOf(((Map<String,Object>)JSON.deserializeUntyped(JSON.serialize(entry))).get('validFor')).split('');
                for (Integer i = 0; i < controllerValues.size(); i++){
                    Object controllerValue = (controllerValues == null ? (Object) (i == 1) : (Object) (controllerValues[i].isActive() ? controllerValues[i].getLabel() : null));
                    Integer bitIndex = i / 6;
                    Integer bitShift = 5 - Math.mod(i, 6 );
                    if(controllerValue == null || (base64map.indexOf(base64chars[bitIndex]) & (1 << bitShift)) == 0){
                        continue;
                    } 
                    if (!dependentPicklistValues.containsKey(controllerValue)){
                        dependentPicklistValues.put(controllerValue, new List<String>());
                    }
                    dependentPicklistValues.get(controllerValue).add(entry.getLabel());
                }
            }
        }
        return dependentPicklistValues;
    }

Database.Upsert – Get Which Records are Inserted or Updated Based on External Id

Sample Code:

Database.UpsertResult[] results = Database.upsert(accList, Account.ExternalId__c);
for(Integer i = 0, i < results.size(); i++) {
    if(results[i].isSuccess()) {
        if(results[i].isCreated()) {
            System.debug(accList[i].ExternalId__c +' was created');
        } else {
            System.debug(accList[i].ExternalId__c +' was updated');
        }
    }
}