20

Here is the way: // I have Account name in String String objectStr = 'Account'; // Convert to schema.sObjectType Schema.SObjectType convertType = Schema.getGlobalDescribe().get(objectStr); // Create an instance of that type Sobject genericObject = convertType.newSObject(); // or if I know it is Account !! why making Sobject ? Account acc = (Account)...


14

Functionally, there is no difference. These two options return the same data. These functions are mostly the same. From a performance perspective, the static approach is approximately 2.5x faster. I ran 25 trials each of 1,000 calls, and the static approach averaged 20.48 ms (20.48 µs per call), whereas the dynamic approach averaged 51.40 ms (51.40 µs per ...


13

Mechanics You need to use the get and put methods. Each method supports both String and SObjectField as the parameter type: // terse myObject.put('UniqueId__c', myObject.get('ExternalId__c')); // verbose SObjectField fieldToGet = MyObject__c.ExternalId__c; SObjectField fieldToPut = MyObject__c.UniqueId__c; Object value = myObject.get(fieldToGet); myObject....


13

The SObject class has dynamic get methods that allow you to specify a string. You could, for example, do something like: for (MyObject__c record : trigger.new) { for (Integer i = 1; i < 8; i++) { try { String target = (String)record.get('Target_Name_' + i + '__c'); if (target == null) break; ...


13

There is no difference except the latter becomes less verbose when you drop the optional Schema. portion. Every time you type: SObjectType.Account instead of: Account.sObjectType.getDescribe() you save 14 characters. Here are some examples of the flexibility you have here. Schema.SObjectType accountType = Schema.Account.sObjectType; accountType = ...


12

As mentioned, describe limits have been removed entirely. Just for completeness, there are a few remaining limits that are affected by these describe calls, but consumption is negligible. These limits are Heap Space and CPU Time. The numbers below are for my org, so mileage will vary. I doubt the numbers would ever get high enough to be a concern. ...


12

Generically speaking, you can do this: public static Id getRecordTypeIdForObject(String objName, String recTypeName) { return ((SObject)Type.forName(objName).newInstance()) .getSObjectType() .getDescribe() .getRecordTypeInfosByName() .get(recTypeName) .getRecordTypeId(); } You can call it like this: Id recTypeId ...


8

If the item you are accessing from your map is an sObject, you can get the value of a field thusly: // Create something we can then query Account acc = new Account(Name='Test Account'); // Access the Name field System.debug(acc.get('Name')); I believe this will work with Custom Objects, and Custom Fields, but not with inner classes. However, if you are ...


8

Please note that SObject and Object are very different types. And adding [] at the end of your type definition makes it a List, again a huge difference. The text of your post indicates you meant Object, so I will answer as if that's the case. Regardless, the key here is that if you have a few different types you want to guarantee all implement a specific ...


7

An alternate way to do this without going through Schema.getGlobalDescribe(): SObjectType sObjType = ((SObject) Type.forName(myString).newInstance()) .getSObjectType()); speed comparison between above and getXXDescribe methods


7

You need to use addError on the field in question, which also implies that you must directly bind the field to an apex:inputField. Here's a trivial example: <apex:page controller="q204269"> <apex:form> <apex:pageBlock> <apex:pageBlockSection columns="1"> <apex:inputField value="{!record.Name}...


6

You could do something like: ((sObject)Type.forName('Account').newInstance()).getSObjectType().getDescribe().isAccessible(); Or you could do a global describe and get the values from the returned map Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe(); But you at least need to have the String of the Object name. If you don't at least ...


6

You need to add a null check. The only reason this would fail is if your parent record is null. One option may be to just get the value statically: String text = record.Parent__r.TextField__c; If you cannot use a static reference, I recommend a cross object getter like I shared here. First create this top-level class: public with sharing class CrossObject ...


6

Yes you just use get instead. You can cache a String or SObjectField. SObjectField field = Account.ParentId; for (Account record : [SELECT ParentId FROM Account LIMIT 10]) { Object value = record.get(field); } Note that it returns Object. If you want to return a concrete type, you'll have to cast. Id value = (Id)record.get(field);


6

I am not pretty sure why you wanna do it. You can dynamically instantiate Map using Type.newInstance(), Probably that's what you need? public static Map<String, SObject> createStringFieldToSObjectsMap(String fieldName, List<SObject> sobjects){ String soBjectTypeString = String.valueOf(sobjects[0].getSObjectType()); Type t= Type....


6

Lightweight A full field describe can require hundreds or thousands of bytes in heap, while the token only requires something like 4 bytes. Serializable You can't serialize DescribeFieldResult directly in many cases (e.g. batchable), but the tokens will go through without a problem.


5

As for the title of your question, yes it is possible to reference fields from a dynamic object. Use square brackets ([]): <apex:inputField value="{!record['Some_Field__c']}" /> Not sure if that will solve your NullPointerException... Note also that you have introduced an injection vulnerability by trusting the id parameter. Never trust user input. ...


5

There is no limit for Describe Call in Salesforce. As per the below document, There were few limits before Summer'14 release and it was remove post Summer'14 release. For more information:- https://developer.salesforce.com/releases/release/Summer14/New+Apex+Enhancements


5

You must cast your instance to a Database.Batchable if you want to pass it to a Database.executeBatch call, even if you additionally implement a custom interface. Type classType = Type.forName('MyBatchClassName'); MyInterfaceName instance = (MyInterfaceName)classType.newInstance(); // custom interface methods Database.executeBatch((Database.Batchable<...


5

Object is an ambiguous term in this context. If by custom object you mean SObject, then it is straightforward as you can use the put method inherited from the SObject class: MyCustomObject__c record = new MyCustomObject__c(); record.put('MyCustomField__c', 'Some Value');


5

The snippet you posted in the comments is failing due to a common mistake. Its an order of operations error, because you're adding an extra string to your comparison, it gets merged into your string value, and screws up your comparison. Your code is evaluated like this: '==>' + 'ab' == 'ab' ('==>' + 'ab') == 'ab' '==>ab' == 'ab' // false If you ...


5

I believe I need to rely mostly on the List<X>.getSObjectType() method. At least as a first step. If it doesn't return a null Schema.SObjectType then I can use that to determine the type. If it does return null I've got more work to do. The type of each sObject in the list needs to be checked to see if they are all the same. void processSObjectList(...


4

You don't need to go through the complicated method you went through to get the new record. The following works just as well: Id objId = 'a0Ci000000vd7xA'; SObject record = objId.getSObjectType().newSObject(objId); This approach also has the advantage that it also works even if someone does something silly like creating a class called "Account" and you try ...


4

If you want to put values into fields, you can use the generic put methods: SObject record = Id.valueOf('a0Ci000000vd7xA').getSObjectType.newSObject(); record.put('emp_name__c', 'test'); Or you can also use a SObjectField: record.put(SomeObject__c.SomeField__c, 'Some Value');


4

I was bothered by null pointer exception when i was experimenting with my code. I changed static access method to getSObjects method and found a strange thing which could be important, when we use getSObjects method and related list is empty then returns null, opposite to static method which always returns a empty list. It happend to me when I was trying to ...


4

You are looking for the SObject.put method: r.put(apiName, a.Name);


4

Apex doesn't really have much in the way of reflection or introspection. While we do have the Type class (which has the forName() method), you still need to know the name of the object at compile time to be able to really make use of it. forName() takes a string, and you can call newInstance() to get an instance of the type that was returned by forName(), ...


4

Apex is a strongly-typed language. When you have a variable the compiler knows to be a List<sObject>, it will only allow you to access fields on the individual sObject components of that list that are defined for the sObject class - which doesn't include Contact and Account-specific fields. That's why you receive a Variable does not exist error. ...


4

You can use Sobject's put method to dynamically provide field names and values Indicador_Clinico__c indic = new Indicador_Clinico__c(); indic.put('MyField__c','JAR');


4

Sure. You can chain into a second batch from the finish() method of your first batch. Starting with API version 26.0, you can start another batch job from an existing batch job to chain jobs together. Chain a batch job to start a job after another one finishes and when your job requires batch processing, such as when processing large data volumes. [...] ...


Only top voted, non community-wiki answers of a minimum length are eligible