Another Salesforce blog!!!

Salesforce, Apex

May, 2017

HOW TO GET LIST OF RECORD TYPE FOR THE OBJECT

How to get the record type name/id: Map<String, Id> mapOfRecordTypeAndId = getMapOfRecordTypeAndId(‘EMPLOYEE__c’); system.debug(‘//’ + mapOfRecordTypeAndId); system.debug(‘// id of the record type’ + mapOfRecordTypeAndId.get(‘recordtypename’); Util class: public static Map<String,Id> getMapOfRecordTypeAndId(String sObjectName){ Map<String , Id> mapOfRecordTypeAndId = new Map<String , Id>(); if (object_recordtypes.containsKey(sObjectName) && object_recordtypes.get(sObjectName).size()>0 ){ mapOfRecordTypeAndId= object_recordtypes.get(sObjectName); }else{ List<RecordType> recordTypeList = [SELECT Id, Name, DeveloperName FROM […]

dynamic apex

PowerPoint DYNAMIC APEX

date format

public static String formatDatetime(Datetime dt) { return dt.format(‘yyyy-dd-MM’); } public static Datetime parseDatetime(String dateStr) { if (dateStr==null) return null; // split the original string String[] tokens = dateStr.split(‘ ‘, 0); //String dayOfWeek = tokens[0]; Integer day = Integer.valueOf(tokens[1]); Integer month = monthToInteger.get(tokens[2]); Integer year = Integer.valueOf(tokens[3]); String[] timeOfDay = tokens[4].split(‘:’); String offset = tokens[5]; // […]

Randomizer

Randomizer public class Randomizer { //returns a random Integer public static Integer getRandomNumber(Integer size){ Double d = math.random() * size; return d.intValue(); } //returns either true or false randomly public static Boolean getRandomBoolean(){ if(math.mod(getRandomNumber(10),2) == 0){ return true; } else{ return false; } } //Get’s a random value from a list of strings public static […]

how to get recordtype id

RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get(‘record type name here’).getRecordTypeId();

concatenate using string.join

List<String> messages = new List<String>(); messages.add(‘one’); messages.add(‘two’); messages.add(‘three’); Employee__c emp = [SELECT Id,Emp_Message__c FROM Employee__c WHERE Id =: empId limit 1 ]; emp.Emp_Message__c += ‘,’ + String.join(messages, ‘,’); update emp;

Integrating with Apex

Explore how to configure Apex classes to provide REST- and SOAP-based Web services to third party systems. Create a SOAP-based Web service that provides transactional support for adding two objects through a single Web service method. Apex Web Services Apex Web Services

Instantiating an SObject Dynamically at Run-time

So how would you create a Generic SObject at run-time? String sObjectName = ‘MyObject__c’; Schema.SObjectType t = Schema.getGlobalDescribe().get(sObjectName); SObject s = t.newSObject();