Tag Archives: Apex

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');
        }
    }
}

Send Email To Non-Contacts Using Apex

Using renderStoredEmailTemplate(templateId, whoId, whatId) method of Messaging Class, we can send email to non-contacts using apex. Here is an example to send email to Opportunity owner.

Sample code:

//Send email to opportunity owner
Messaging.SingleEmailMessage mail = Messaging.renderStoredEmailTemplate(emailTemplateId, ownerId, opportunityId);
mail.setTargetObjectId(ownerId);
mail.setSubject(mail.getSubject());
mail.sethtmlBody(mail.gethtmlBody());
mail.saveAsActivity = false;
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

Schedule An Apex Job To Run Every 5 Minutes

Sample Code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 5 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 10 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 15 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

String sch5 = '0 20 * * * ?';
System.schedule('Schedule Job5', sch5, obj);

String sch6 = '0 25 * * * ?';
System.schedule('Schedule Job6', sch6, obj);

String sch7 = '0 30 * * * ?';
System.schedule('Schedule Job7', sch7, obj);

String sch8 = '0 35 * * * ?';
System.schedule('Schedule Job8', sch8, obj);

String sch9 = '0 40 * * * ?';
System.schedule('Schedule Job9', sch9, obj);

String sch10 = '0 45 * * * ?';
System.schedule('Schedule Job10', sch10, obj);

String sch11 = '0 50 * * * ?';
System.schedule('Schedule Job11', sch11, obj);

String sch12 = '0 55 * * * ?';
System.schedule('Schedule Job12', sch12, obj);