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. Favorite
Validating an Id length(15 or 18)
1 2 3 4 5 6 7 |
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; } |
Favorite
Check InstanceOf of String or ID
1 2 3 4 |
static public Boolean isValidId(String strId) { Boolean isValid = (strId InstanceOf ID) ? true : false ; return isValid ; } |
Favorite
how to get the RecordType/Id name based on the ID
link question another link
1 2 |
id myid = 'a1aV0000001czYE'; String sObjectType = ((Id)myid).getSobjectType().getDescribe().getName(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private string getRecordTypeId(String objName, String whereClause) { String sqry = ' SELECT Id, RecordtypeId, RecordType.name '; 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: '+ obj); recordTypeId = (string)obj.get('RecordTypeId'); } system.debug('rtypeid: ' + recordTypeId); return recordTypeId; } |
1 2 3 4 5 6 7 8 9 10 11 12 |
private string getRecordTypeName(string objName, string whereClause) { String sqry = ' Select Id,name,developername FROM RecordType '; sqry += ' WHERE sobjecttype =: objName AND id =: whereClause'; List<SObject> objs = Database.query(sqry); string recordTypeName = ''; for(SObject obj : objs) { system.debug('soql: '+ obj); recordTypeName = (string)obj.get('developername'); } system.debug('recordTypeName: ' + recordTypeName); return recordTypeName; } |
1 2 3 |
getRecordTypeId(sObjectType, myId); getRecordTypeName(sObjectType, getRecordTypeId(sObjectType, myId)); |
Favorite
How to add a confirm dialog to a command button
1 |
<apex:commandButton reRender="pb" value="Associate Payment" action="{!associatePayment}" onclick="if(!confirm('Do you want to proceed?')){return false};" title="Associate Payment"/> |
Favorite
How to get FieldSet fields in Apex Dynamically (fieldset name is not static)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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 –
1 2 3 4 5 6 7 8 9 |
List<Schema.FieldSetMember> fieldSetMemberList = Util.readFieldSet('Account_FieldSet','Account'); for(Schema.FieldSetMember fieldSetMemberObj : fieldSetMemberList) { system.debug('API Name ====>' + fieldSetMemberObj.getFieldPath()); //api name system.debug('Label ====>' + fieldSetMemberObj.getLabel()); system.debug('Required ====>' + fieldSetMemberObj.getRequired()); system.debug('DbRequired ====>' + fieldSetMemberObj.getDbRequired()); system.debug('Type ====>' + fieldSetMemberObj.getType()); //type - STRING,PICKLIST } |
Favorite
Cloning dynamic in Apex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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 map of field name and field token Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap(); list<string> selectFields = new list<string>(); if (fMap != null){ for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft) Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd) if (fd.isCreateable()){ // field is creatable selectFields.add(fd.getName()); } } } if (!selectFields.isEmpty()){ for (string s:selectFields){ selects += s + ','; } if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));} } return 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE ' + whereClause; } } |
1 2 3 4 5 |
/* query lead and then clone it */ String soql = Utils.getCreatableFieldsSOQL('lead','id=\'00Q3000000aKwVN\''); lead l = (Lead)Database.query(soql); lead l2 = l.clone(false, true); insert l2; |
Favorite
Apex passing string variable within quotes
1 2 |
String query='select column_name from information_schema.COLUMNS where table_name =\''+ String.escapeSingleQuotes(selectedValue1)+'\''; |
Favorite
Randomizer
Randomizer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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 String getRandomString(List<String> strings){ List<Double> ranks = new List<Double>(); Map<Double,String> rankMap = new Map<Double,String>(); for(String s : strings){ Boolean isDup = true; Double rank; While(isDup){ Double x = getRandomNumber(100000); if(!rankMap.containsKey(x)){ rank = x; isDup = false; } } ranks.add(rank); rankMap.put(rank,s); } ranks.sort(); return rankMap.get(ranks.get(0)); } //Returns a random picklist value public static string getRandomPickListValue(Sobject s_object, String field_name, Boolean allow_blank){ List<String> Strings = new List<String>(); if(allow_blank){ String b = ''; Strings.add(b); } Schema.sObjectType sobject_type = s_object.getSObjectType(); Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); for (Schema.PicklistEntry a : pick_list_values) { Strings.add(a.getValue()); } return getRandomString(Strings); } //returns a map of all picklists and multiselect picklists for a givien object //the keyset is the field name using proper case public static Map<String,List<String>> getPicVals(sObject s_object){ Map<String,List<String>> valueMap = new Map<String,List<String>>(); Schema.sObjectType sobject_type = s_object.getSObjectType(); Schema.DescribeSObjectResult r = sobject_type.getDescribe(); Map<String, Schema.SObjectField> field_map = r.fields.getMap(); for(String s : field_map.keyset()){ List<String> strings = new List<String>(); Schema.DescribeFieldResult F = field_map.get(s).getDescribe(); if(f.GetType() == Schema.DisplayType.Picklist || f.GetType() == Schema.DisplayType.MultiPicklist){ List<Schema.PicklistEntry> pick_list_values = field_map.get(s).getDescribe().getPickListValues(); for (Schema.PicklistEntry a : pick_list_values) { strings.add(a.getValue()); } valueMap.put(String.valueOf(field_map.get(s)),strings); } } return valueMap; } //Returns Lorem Ipsum placeholder text. public static String getPlaceholderText(Integer length){ String firstSentence = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '; List<String> sentenceList = new List<String>(); sentenceList.add('Vivamus nec lacus eget massa cursus pulvinar. '); sentenceList.add('Morbi vel odio eget nunc auctor posuere eget eget ante. '); sentenceList.add('Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. '); sentenceList.add('Pellentesque lacus eros. '); sentenceList.add('Sed suscipit tristique varius. '); sentenceList.add('Mauris ultricies, nibh eu fermentum accumsan, justo quam pulvinar tellus, sed tempor quam eros sit amet ante. '); sentenceList.add('Duis mi libero, cursus nec facilisis ut, commodo eget nunc. '); sentenceList.add('Nulla eros augue, iaculis sed volutpat et, sagittis quis sem. '); sentenceList.add('Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla placerat accumsan vulputate. '); sentenceList.add('Fusce placerat tellus eget tellus faucibus a gravida sapien fermentum. '); String s = firstSentence; while (s.length() < length+1){ s += getRandomString(sentenceList); } s = s.trim(); while (s.length() >= length-1){ s = s.substring(0,s.length()-1).trim(); } s = s.substring(0,s.length()-1).trim(); s += '.'; return s; } static testMethod void testRandomizer() { test.startTest(); Integer testInt = getRandomNumber(10); Boolean testBool = getRandomBoolean(); List<String> testStringList = new List<String>(); testStringList.add('one'); testStringList.add('two'); testStringList.add('three'); String testString = getRandomString(testStringList); String testString2 = getRandomPickListValue(new Account(), 'Industry', true); String testString3 = getRandomPickListValue(new Account(), 'Industry', false); Map<String,List<String>> testMap = getPicVals(new Account()); test.stopTest(); String testString4 = getPlaceholderText(300); } } |
Favorite