Another Salesforce blog!!!

Salesforce, Apex


using string literals

Literal strings must be enclosed in single quotation marks. double quotation marks are reserved for enclosing attribute values, and must be escaped in strings. Use the “\” character to escape strings.

Validating an Id length(15 or 18)

static public String validateId(String Idparam) { String id = String.escapeSingleQuotes(Idparam); if((id.length() == 15 || id.length() == 18) && Pattern.matches(‘^[a-zA-Z0-9]*$’, id)) { return id; } return null; }

Check InstanceOf of String or ID

static public Boolean isValidId(String strId) { Boolean isValid = (strId InstanceOf ID) ? true : false ; return isValid ; }

how to get the RecordType/Id name based on the ID

link question another link id myid = ‘a1aV0000001czYE’; String sObjectType = ((Id)myid).getSobjectType().getDescribe().getName(); private string getRecordTypeId(String objName, String whereClause) { String sqry = ‘ SELECT Id, RecordtypeId, ‘; sqry += ‘ FROM ‘+objName+’ WHERE Id =: whereClause ‘; List<SObject> objs = Database.query(sqry); //system.debug(‘soql: ‘+ objs); string recordTypeId = ”; for(SObject obj : objs) { system.debug(‘soql: […]

How to add a confirm dialog to a command button

<apex:commandButton reRender=”pb” value=”Associate Payment” action=”{!associatePayment}” onclick=”if(!confirm(‘Do you want to proceed?’)){return false};” title=”Associate Payment”/>

How to get FieldSet fields in Apex Dynamically (fieldset name is not static)

public static List<Schema.FieldSetMember> readFieldSet(String fieldSetName, String ObjectName) { Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe(); Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(ObjectName); Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe(); //system.debug(‘====>’ + DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName)); Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName); //List<Schema.FieldSetMember> fieldSetMemberList = fieldSetObj.getFields(); //system.debug(‘fieldSetMemberList ====>’ + fieldSetMemberList); return fieldSetObj.getFields(); } You can use result as follows – List<Schema.FieldSetMember> fieldSetMemberList = Util.readFieldSet(‘Account_FieldSet’,’Account’); for(Schema.FieldSetMember fieldSetMemberObj : fieldSetMemberList) { […]

Cloning dynamic in Apex

public with sharing class Utils{ // Returns a dynamic SOQL statement for the whole object, includes only creatable fields since we will be inserting a cloned result of this query public static string getCreatableFieldsSOQL(String objectName, String whereClause){ String selects = ”; if (whereClause == null || whereClause == ”){ return null; } // Get a […]

Apex passing string variable within quotes

String query=’select column_name from information_schema.COLUMNS where table_name =\”+ String.escapeSingleQuotes(selectedValue1)+’\”;

dynamic apex



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 […]

Previous Posts