Another Salesforce blog!!!

Salesforce, Apex

Dynamic 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.

sObjectUtils class, utility

global with sharing class sObjectUtils { /** * This method accepts the ID of any object type and returns the full name, including prefix of the sObject * type to which it belongs. * @author cpeterson **/ public static Schema.SObjectType getObjectType(id subject){ Schema.SObjectType result; string target = subject; Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); string keyPrefix; […]

Check InstanceOf of String or ID

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

Use Set in dynamic SOQL IN-clause in method without Database.query()

public String inClausify(Set<Id> ids) { String inClause = String.format( ‘(\’\'{0}\’\’)’, new List<String> { String.join( new List<Id>(ids) , ‘\’,\”) }); return inClause; } LINK LINK2

how to get a picklist all values in apex controller

Apex Dynamic PickList values

From the Object Api name retrieving the SObject

┬áSchema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectType);   or id myid = ‘a1a0n00000092jb’; String sObjectType = ((Id)myid).getSobjectType().getDescribe().getName(); public void from_string_object_name_to_sObject() { Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectType);//From the Object Api name retrieving the SObject Sobject Object_name = targetType.newSObject(); }

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

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();

How to get all the required fields of sObject dynamically

There is no direct property available in Apex dynamic API to represent the required field. However there is another way to know about it. If any field have below three properties then it is mandatory field. If it is Creatable If it is not nillable and If it does not have any default value Map<String, […]

get all the fields of sObject using dynamic Apex

Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe(); Schema.SObjectType s = m.get(‘Speaker__c’) ; Schema.DescribeSObjectResult r = s.getDescribe() ; Map<String,Schema.SObjectField> fields = r.fields.getMap() ; system.debug(‘///’ +fields );

Previous Posts