26

No no, the field (API) name is Schematic__c, you need to start from it. Think about Schematic__r (the rel. name) like a consistent table alias when you'd make JOINs in regular database, nothing more. To go "up": Schema.DescribeFieldResult f = Schema.sObjectType.Order_Line_Item__c.fields.Order__c; System.debug(f.getRelationshipName()); // Outputs "...


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


11

You need to use sobjectType as the attribute of default object. Not sObjectType and not SObjectType. i.e. <aura:attribute name="acc" type="Account" default="{'sobjectType':'Account', 'Name':'Account123'}"/> Starting from Spring 15 Lightning Compomnents markup is case sensitive (Release Notes) therefore you get an error when referencing ...


10

It is absolutely possible! We have even built this exact utility where I work. However, I can't just give you the answer, but I will give some hints. SObject methods: get will return the value of the field you are looking for The aforementioned get method can take either a String or a Schema.SObjectField to specify the field you want to pluck. If you ...


10

You want to call getRelationshipOrder() from your describe. It will be null for a lookup, 0 or 1 for a Master-Detail relationship.


9

You don't need to have a return type to get data back to the callee, and sometimes this is more convenient. Here's my version of a "get values from records" implementation: public static void getValuesFromRecords(Object[] result, SObject[] source, SObjectField field) { for(SObject record: source) { result.add(record.get(field)); } } Typical ...


9

Looks like theres 3 methods you can use to check the definition of a textarea: field.getType() == Schema.DisplayType.TEXTAREA field.isHtmlFormatted() field.getLength() Here's a snippet of these methods in use: if (field.getType() == Schema.DisplayType.TEXTAREA) { if (field.getLength() <= 255 && !field.isHtmlFormatted()) { return '...


8

The when values must be literals, so you cannot use String.valueOf as demonstrated in your second example. That said, using String.valueOf is approximately 10% more efficient when using literal string values: Long t3, t2, t1 = datetime.now().gettime(); for(Integer i = 0; i < 100000; i++) { sobjecttype a = account.sobjecttype; switch on a....


7

it will be possible from Summer'18 https://releasenotes.docs.salesforce.com/en-us/summer18/release-notes/rn_apex_developer_name.htm Get the Developer Name for Record Types More Easily We added Apex methods to help you get the developer name for record types more easily. Previously, the developer name was accessible only via SOQL on the RecordType ...


7

If you don't care about the delete contexts, you can just do: SObjectType triggerType = trigger.new.getSObjectType(); It doesn't add much complexity, however, to use a ternary to check for the null case: SObjectType triggerType = trigger.isDelete ? trigger.old.getSObjectType() : trigger.new.getSObjectType();


6

If there's a number field, set it to 0. If it's a String, set it to '' I verified it in one of my lightning components that this snippet works (I've masked the true obj/ attr names) if I do this in my associated controller. ({ myTask : function(component, problem) { var action = component.get("c.saveTask"); action.setParams({ ...


6

I don't believe this is possible currently. There's no methods on Schema.SObjectField of Schema.DescribeFieldResult that link back to the SObjectType that field belongs to.


5

Instead of Account.Type (a specific field), you can use: // Get the describe for the object DescribeSObjectResult objResult = Schema.getGlobalDescribe() .get(selectedObject).getDescribe(); // Get the field dynamically DescribeFieldResult fieldResult = objResult.fields.getMap() .get(...


5

If you want to write a utility that creates records, this function is absolutely necessary in order to be generic at all. Sure, you could also use Type.forName, but that doesn't take nearly as good advantage of strict typing. Simply contrast these test utility styles and imagine which will balloon massively each time you want to support new object types: ...


5

From a quick check, this looks more likely to be an API version problem; I can save this in a component set to use Salesforce.com API version 20.0 (or greater) but not in one set to use Salesforce.com API version 19.0 (or less). Check the version setting for your component.


5

It will simply instantiate a new empty Account object. Required fields only come into play when you perform the DML. You will have to set those. This and the describe classes are useful when the object type in a method can vary and be identified during runtime.


5

Controller: public class Test{ public Schema.SObjectType myProperty { get; private set; } public Test(){ myProperty = Schema.getGlobalDescribe().get( 'Account' ); } } <apex:page controller="Test"> <apex:variable value="{!myProperty}" var="prop" /> <apex:variable value="{!prop[ 'describe' ]}" var="field" /...


4

The following code (that you can run in the Developer Console's "Execute Anonymous" window) illustrates how starting from a string containing the SObject type name you can get the detailed information about the fields. If all you want is the field names you only need to make the describe call on the SObjectType token. If you want more detailed information ...


4

The key prefix 0A2 corresponds to the DevelopmentPackageVersion object. The key prefix 033 corresponds to the AllPackage object. A more comprehensive (and significantly faster, I imagine) methodology for taking a key prefix or Id is this fairly simple approach: system.debug(Id.valueOf('0A2000000000000').getSObjectType()); system.debug(Id.valueOf('...


4

Depending on your specific use case, I would most likely go with isMruEnabled || isCustom. isMruEnabled returns true for things that have a Tab (e.g. Accounts will return true, but AccountShare will return false). Since custom objects may or may not have a Tab, and having a Tab is one of the requirements for isMruEnabled, you'd want to grab anything that's ...


4

External Ids do not inherently encode type information, and even External Ids that have a uniqueness constraint are not unique across different External Id fields - either on the same or on different objects. From Salesforce's perspective, an External Id value has no association with an sObjectType, and it cannot tell you anything about such a type. This ...


4

From the sObjectType object, use the newSobject method to create a new record in memory. It accepts a single optional parameter for the record Id: targetSObject = new sor.getSObjectType().newSobject(sObjectID); As an aside, if you already know the Id, you don't need the describe: targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Also, you ...


3

There are a very large number of SoapType values which are not contained in your linked document. When I investigated, I found 619. system.debug(SoapType.values().size()); // yields 619 for (SoapType st : SoapType.values()) system.debug(st); If you look through the logs, you will indeed see ADDRESS listed: 12:11:07:006 USER_DEBUG [2]|DEBUG|LOCATION 12:...


3

There's all sorts of dodgy ways to do this, but I find that the easiest is to simply use an "out-parameter," because then you don't even need to worry about casting. It works like this: public static void getValuesByField (Set<Object> results, List<SObject> records, SObjectField field){ for(SObject record: records) { results.add(...


3

Short answer: There's no way to do that. Long answer: Currently, the only way to access the developer name is to query for it. I'd recommend using an utility class with a static map so you can make just one call per execution. You can also vote on this idea.


3

The sample code appears to be using the Partner API. Your example error messages indicate you are using the Enterprise WSDL (com.sforce.soap.enterprise.sobject.SObject). The Partner API is loosely typed versus the Enterprise WSDL which is strongly typed. Because the Partner API is loosely typed you need to set the sObject type (setType) and use a generic ...


3

You've kind of answered your own question in the title. You need to use the SObject.clone method. Note that you can never query for and subsequently insert a record, because it will already have an Id value. Incorrect newRecord = existingRecord; insert newRecord; Correct newRecord = existingRecord.clone(/*preserveId*/ false); insert newRecord; There are ...


3

You're trying to put an instance of Account_Territory_Loader_vod__c into a list of Account_Territory_Loader_vod__c. accATLMap.put(atl.account_vod__c,atl); You need to instantiate a new list for each key in your map, and then add the element to the list: // Check whether the value for that key exists if (accATLMap.get(atl.account_vod__c) == null) { // ...


3

Depending on how you want to use it, the alternative is to sfdcfox's approach is to cache the global describe in a static variable with lazy instantiation e.g. global class MyUtils { private static Map<String, Schema.SObjectType> globalDescribe; public static Map<String, Schema.SObjectType> getGlobalDescribe() { if(...


3

There could be several reasons other than what I am describing here . But here is one for starters. I recently faced a challenge when I was using aura:attribute of type Map in IE11. I was sending this map to apex controllers for a DML operation as follows. Component <aura:attribute name="sampleMap" type="Map" /> Controller var myMap = new ...


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