13
votes
Can you use generics in Apex?
I guess we all need to vote here to make them hear us and allow generics in Apex. This is the only way how generics can appear in Apex.
If no ones votes they think we don't really need them.
9
votes
Accepted
If I know the API Name, how can I get and work with the Schema.ChildRelationship?
I'll answer your second question first.
To get the child records if you know the relationship name, you pass a String argument to the SObject.getSObjects method:
MyObject__c parent = new MyObject__c(...
8
votes
Accepted
What is the best way to cast/convert Map<String, SObject> to Map<Object, Object> and back again?
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.
...
8
votes
Accepted
Pros and cons of generic SObject list vs multiple lists of objects
For cons, using SObject instead a concrete type restricts the usage of upsert by external ID, and you can't upsert generic SObject lists (the variable may be SObject[], but the actual list in memory ...
8
votes
How to accept generic "SObject" in Lightning Web Component for Flows?
I thought I would come back to this question because this is now possible I believe using propertyType.
<targetConfig targets="lightning__FlowScreen">
<propertyType name="T&...
7
votes
Accepted
How to accept generic "SObject" in Lightning Web Component for Flows?
Generic SObject and SObject[] are not yet supported at the interface between LWC and Flow (TLDR: You can't get there from here). We're working on it right now, though, and hope to get it into Summer '...
6
votes
Accepted
Does apex support generics?
You want to use Object as a generic class to use. Also, no need to check null AND an empty string (it'll always be null or a non-empty string). Here's the modifications you want:
public class ...
5
votes
Is it possible to convert Map<Object, List<Object>> to Map<Id, List<SomeConcreteSObject>> without a loop?
No. You can't convert key types without a loop. The Map structure is not flexible in this way. The List type is a bit more flexible, however, and so you can make this minor simplification, as ...
4
votes
Does apex support generics?
In Apex, the best you can do is change the parameter on your constructor to have the generic Object type.
If you need to process the values in a particular way based on type, you can use the ...
4
votes
How can I abstract building maps from SObject Lists in a good way?
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 ...
4
votes
Accepted
Generic SObject Utility to dynamically set field values from Map
Loops within loops aren't bad per se. They're bad when they either (a) are an inefficient way of expressing functionality or (b) represent an attempt to execute some code whose computational ...
3
votes
Accepted
Tips to generalize code in apex with Map<string, set<string>> and Map<string, set<SObject>> and how to call method of SObject
It's not easy to generalize this sort of thing since you cannot cast between Set<String> or Set<SObject> and Set<Object>. The best you can do is write infrastructural code that operates ...
3
votes
What is the best way to cast/convert Map<String, SObject> to Map<Object, Object> and back again?
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> ...
3
votes
Accepted
How can I upsert a homogeneous List<SObject>, in a good way?
You need the actual object to be a concrete sobject list type. You can do it either by Type.forName or by String.
SObject[] results = doUpsert(source, List<Account>.class);
...
public SObject[...
3
votes
How can I upsert a homogeneous List<SObject>, in a good way?
I know this is bit ugly, but serializing and deserializing converts sObject to the actual type they are.
List<Account> accountList = (List<Account>) JSON.deserialize(JSON.serialize(...
3
votes
Accepted
How to create a Map with generics Id and String?
As Adrian mentioned in his comment, you cannot do this without a loop.
The Map constructor that takes a List<SObject> (which is what SOQL queries normally return unless you're including ...
3
votes
Accepted
In Apex, can I detect if an Object is a Map, regardless of parameterized types?
It's ugly and hackish, but this works:
public class MAP_Detector {
public Boolean isMap(Object obj) {
String str = String.valueOf(obj);
String firstCharacter = str.substring(0, 1);...
2
votes
Accepted
How to get recordtypeid from generic object
Schema.SObjectType has static members of only objects that are present in your organization. So you cannot access those members dynamically i.e. it will allow
Schema.sObjectType.Account
As per your ...
2
votes
updating reference field using generics sObject
You can't update a related object in a DML operation like this. You need to update the account and opportunity separately. The syntax you've attempted to use is used to set a lookup field by an ...
2
votes
Accepted
Is it possible to Generically deserialize a Json string wit nested arrays in Apex
The API to deserialize JSON of indeterminate structure is:
JSON.deserializeUntyped
described in the JSON Class documentation. I have used this often and have not hit any depth problems; add a link to ...
1
vote
Accepted
Can we rely on reference when it comes to changing Map items changing?
shows both single and array entries.
To be more accurate, it shows array entries with one or more entries. In your map's data type, you can't mix arrays and strings.
tempo is the matter - looks like ...
1
vote
Generic lightning web component
Enhanced Lightning Grid (Sortable datagrid), a free appExchange provides generic Aura component to build customized related list with additional features.
The Enhanced Lightning Grid component allows ...
1
vote
Accepted
How to use custom metadata to map generic JSON
Custom Metadata Types support various relationships. They can have multiple records created against them which would allow you to manage individual mappings. They even allow you to have effectively ...
1
vote
Cannot deploy flow, Getting error 'Screen component 'ComponentName' is missing a type mapping 'T'
I ran into the same error, perhaps the following will work. What I presume to be the case is that you have a sourceApiVersion lower than 49 specified in your sfdx-project.json file. Set it to 49 (i.e. ...
1
vote
Accepted
updating reference field using generics sObject
To be able to do this the field BasicId__c should be an External Id. Also, the parent record with that particular External Id should be present in the database.
If you put the account in opportunity ...
1
vote
Generic function - Custom meta data type
In your code, you're trying to assign a Metadata Type to a List<SObject>, but Metadata Types are not SObjects, they are their own distinct type (i.e. custom sobjects end in __c while Custom MDT ...
1
vote
Accepted
Is it possible to create a generic Map swapper?
To be honest, this isn't exactly the answer I am looking for.
It isn't really a Map and isn't completely interchangeable with a Map.
However, this is a generic solution which effectively reverse ...
1
vote
Is it possible to create a generic Map swapper?
We don't have Generics, like we do in Java, so you would be stuck writing our own custom Map class in order to implement this algorithm, and perhaps methods to convert to/from this class to actual ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
generics × 34apex × 25
sobject × 9
map × 7
json × 5
lightning-web-components × 3
object × 3
deserialize × 3
trigger × 2
visual-workflow × 2
administration × 2
casting × 2
soql × 1
rest-api × 1
api × 1
managed-package × 1
deployment × 1
controller × 1
webservices × 1
list × 1
record-type × 1
relationships × 1
wrapper-class × 1
performance × 1
custom-metadata × 1