Hot answers tagged

17

You can't hop more than one data type per cast; each additional conversion requires another cast. For example, the following works: Integer i = (Integer)5.2; But you can't write: Object o = 5.2; Integer i = (Integer)o; And you also can't write: Integer i = (Integer)(Object)5.2; Basically, casting a Decimal from an Object reference to an Integer ...


10

This is one of those cases where the Type system just falls flat. Set does actually implement the methods for being an Iterable, as mentioned by the methods in the documentation, but does not officially "implement" it (System.assert(stringSet instanceOf Iterable<String>) fails). The net result is that we have a hybrid object that fails to compile when ...


10

I agree with Rahul's comment that the following is probably the best you can do: String payload = '{"data": [{"s": "a", "i": 1}]}'; Map<String, Object> deserialized = (Map<String, Object>)JSON.deserializeUntyped(payload); List<Map<String, Object>> data = new List<Map<String, Object>>(); for (Object instance : (List<...


10

No, there's no way to do a dynamic typecast in apex. However what you can do to work around this is make a virtual or abstract class with all of the logic using the dynamic apex form (e.g. record.put). Then have a subclass for the Lead type that wraps this method's return type in a Lead cast. For example: public abstract class GenericFactory{ public ...


8

Problem is here: lstAttachments[iDx].LastModifiedDate.Date(). You are getting Date, which is not the same as DateTime (no time stored inside). In this case 2 first and 3 last records have the same date, which mean that they are sorted correctly. To fix that, lstAttachments[iDx].LastModifiedDate.Date(); needs to be replaced by lstAttachments[iDx]....


8

Looking in to this question, I realized that the type system is inherently broken. They have "fixed" Maps, it seems, but in a way that no longer lets you mix and match some types, especially Object. The list class is still broken, however: class c1 { } class c2 extends c1 { } c1[] s = new c2[0]; Generally speaking, unless/until they fix the problem, you ...


6

If you are going to cast a variable, you’re most likely doing what’s known as a downcast. This means that you’re taking the Object and casting it into a more “specific” type of Object. Here’s an example: Object aSentenceObject = 'This is just a regular sentence'; String aSentenceString = (String)aSentenceObject; also you can use "upcast" String ...


5

In Salesforce My_Field__c we create field with Number actually stores the decimal type values even if we define decimal places as 0. It is never be an Integer until we transform the value explicitly from Decimal to Integer. Decimal myDec = (Decimal) mySObject.get('My_Field__c'); So, then from this Decimal we can transform to Integer. Also, refer Rules of ...


4

The "easiest" way would be to go down the dark, dark road of Dynamic Apex, because Visualforce has a problem binding to non-SObject types dynamically. The only other alternative I can immediately think of, which just feels completely wrong, would be to include the child classes within the parent: public abstract class Vehicle { public virtual Boolean ...


4

It's not really a bug, because the returned value is not an instance of an Integer. Here's a snippet to demonstrate: SObject record = [SELECT Integer__c FROM MyObject__c LIMIT 1]; Object value = record.get('Integer__c'); system.debug(value); system.debug(value instanceOf Integer); system.debug(value instanceOf Long); system.debug(value instanceOf Double); ...


4

In this particular case, the issue is with using a Set<SObject> as the value type for your map. I ran into a similar issue (well, one that generated the same error at least) in Is there a specific reason why we can't upcast Sets? The long and short of it is that things like Set<SObject> test = new Set<Contact>(); don't work because of ...


4

I coudn't replicate the issue from provided example. If you could elaborate I may be able to give you more precise answer, why addAll() worked for you. The key here is that the declarion has been with Map<Id, SObject>. And upsert does not work on SObject collections (there is a ways arround it). I was able to reproduce the same behaviour with ...


4

Why not just use Map Constructor? and then use the keySet to get Set? Set<Id> resultIds = (new Map<Id,SObject>(sObjectList)).keySet();


4

I'm pretty sure what you're looking for is: String custMet = 'BillToId__c'; String idStr = (String)invoice.getSObject('BillingAccount__r').get(custMet); getSObject returns the instance of the related SObject. As mentioned in the comments, getSObject is redundant here, but can be useful if you don't know the relationship name upfront. String idStr = (...


3

No, there isn't a better way. With Set, you can hack around it to some extent. But with a Map, you care about the values, not just the keys, so you can't do it without looping. Set<String> values = new Set<String>(); Set<Object> generic = new Set<Object>((List<Object>)new List<String>(values)); It's not immediately clear ...


3

If you need to get the OwnerId as 15 digits, you can do the truncation wherever you set record in your controller. You can either overwrite record.OwnerId, or store a separate recordOwnerId attribute. I would probably do the latter. // somewhere wiithin your controller component.set("v.record", someRecordData); component.set("v.recordOwnerId", ...


3

There is no out of the box way to do what you want. You have to iterate. You can write some simple filter mechanisms or use a library like Selector to make your life easier. With that library, you can do things like: List<Financial__c> paidOnly = Select.Field.isEqual(Financial__c.Status__c, 'Paid') .filter(idWithParent.values()); If your just ...


2

ca_peterson's example is a great start, however how would you handle changes in org requirements, validation rules, etc that would cause your test factory to fail, and how soon would you know they fail?. Not to mention the code needed to add new object, etc. To each their own. We start using the SEED for Test methods package both within our test factory and ...


2

Your OP is somewhat unclear, but if the types are correct, then you simply need to pull the data from the fields, with no need for any additional queries. searchField = tableComponent.SearchFields__c; buttons = String.isBlank(tableComponent.ActionButtons__c) ? new List<String>() : tableComponent.ActionButtons__c.split(',');


2

You can do this: public static void updateTerritory(SObject[] records, SObjectField countryField, SObjectField postalCodeField, SObjectField territoryField) { for(SObject record: records) { String postalCode = (String)record.get(postalCodeField); String country = (String)record.get(countryField); // Do logic record.put(...


2

Sadly, this doesn't work, because collection inheritance is broken. As I outlined in my previous answer, the following code demonstrates the inheritance problem: class c1 { } class c2 extends c1 { } // All C2s are C1s, but this won't compile Map<String, c2> m1 = new Map<String, c1>(); // Not all C2s are C1s, but this compiles and can crash. ...


2

Here is my Utility class that does this with props to @sfdcfox // ------------------------------------------------------ // getIdSetFromField : gets a set of Ids from a list of sobjects in <field> // ------------------------------------------------------ public static Set<ID> getIdSetFromField(SObject[] records, SObjectField fieldname) { ...


2

If you want to follow the path dynamically, you'll want to write a method for that: public static Object getFieldValue(SObject record, String fieldPath) { String[] path = fieldPath.split('\\.'); // split is regex, need to escape SObject temp = record; while(path.size()>1 && temp != null) { temp = temp.getSObject(path.remove(0)); } ...


2

Apex requires types to be known at compile time to directly reference fields. But there are are get methods in the SObject class for this case where the field names can be used in string form: List<sObject> objects = database.query(queryString); for (sObject o : objects) { String name = (String) o.get(camposMainObj.get(0)); String email = (...


2

If you open up the details of the error message, it probably says Invalid conversion from runtime type Map<Id, SObject> to Map<Id, Segment__c> Class. You can't convert Maps with a different type specialization, even if the changed types are convertible. In the particular case of converting a Map<Id, sObject> to a Map<Id, ...


2

You should not try to put a generic type in to a concrete type. This can cause bugs. The ideal situation is to create a copy so you retain the concrete type: public Map<Id, SObject> removeLockedRecords(Map<Id, SObject> newMap) { // Make a copy // Map<Id, SObject> returnMap = newMap.clone(); // Clear out existing key/values ...


2

The issue is that the signature of your method void SOQL(String, List<String>, Set<Object>) is different from the signature of your call void SOQL(String, List<String>, List<String>) There is a constructor for Set that takes a List (and vice-versa), but you cannot assign a Set to a List, nor a List to a Set, nor will explicit type ...


2

You would use the sObject.get method: Id conId = (Id) duInv.Invoice__r.BillingAccount__r.get(Mdt.AccountContactId__c); Assuming that Mdt is the custom metadata record, and AccountContactId__c is the lookup field.


2

You can't dynamically cast in Apex. Fortunately, here, you don't need to. Type obj as an Sobject value, rather than Object. The getPopulatedFieldsAsMap() method is defined on the sObject class, so you can call it regardless of what concrete sObject your method receives. If you do need to write distinct logic for different sObject types, you can use a ...


1

Another way is you can also create a method in lightning component which will convert 15 to 18 digit component.cmp <aura:attribute name="currentUserID" type="String" default="{!($SObjectType.CurrentUser.Id)}" access="global" /> <aura:if isTrue="{!v.recordOwnerId == currentUserID}"> controller: convertTo18 : function(component) { var ...


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