Questions tagged [generics]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-1 votes
1 answer
165 views

Generic lightning web component [closed]

I want to create a related list with LWC that i can use for all object. Can someone help me with any ideato solve this problem.
0 votes
1 answer
1k views

How to use custom metadata to map generic JSON

I want to create a generic REST API Webservice where the configuration of mapping fields can be done trough custom meta data. How would I set this up?. I want to be able to have an administrator add ...
  • 2,206
0 votes
2 answers
466 views

Is it possible to Generically deserialize a Json string wit nested arrays in Apex

I want to be able to Generically deserialize a Json string with nested arrays for a web-service, since the structure isn't known beforehand and kan change a lot. I did some reading a seems doable but ...
  • 2,206
1 vote
1 answer
1k views

Cannot deploy flow, Getting error 'Screen component 'ComponentName' is missing a type mapping 'T'

I have created a flow screen component that work for multiple object. In the flow, I have created a collection variable. I have created an LWC component that uses the collection variable. When I try ...
  • 355
0 votes
2 answers
334 views

updating reference field using generics sObject

I am trying to perform upsert on opportunity based on external Id. I am building the opportunity objects based on sObject as I am using that function for other objects too. Now Opportunity object ...
  • 207
0 votes
1 answer
376 views

Tips to generalize code in apex with Map<string, set<string>> and Map<string, set<SObject>> and how to call method of SObject

I am working on some generalization of my code and I'm struggling with some cases which are irritating me because I think I'm having code duplicity. 1) I'm often working with some map<string, set&...
  • 123
1 vote
0 answers
71 views

Create dynamic triggers based on user selection from managed package [duplicate]

I have a requirement where I need to create two triggers inside a managed package. So, when the package is installed, the admin has a choice to either use the default objects available in the package ...
7 votes
2 answers
4k views

How to accept generic "SObject" in Lightning Web Component for Flows?

I've got a custom built LWC to be used within a screen component in a flow. My component basically renders a collection of SObjects in a distinct way. It can also deal with different types of sObjects....
  • 942
1 vote
2 answers
1k views

Generic function - Custom meta data type

I would like to do a generic function to get custom meta data type records and return it as a Map. In a first time with just one field , that means Map<String,String> and in a second time with ...
  • 473
0 votes
1 answer
2k views

How to get recordtypeid from generic object

I want to be able to pass an object, and recordtypelabel as string into a controller and get the recordtypeId. How do I achieve this. I currently get variable does not exist for the custom object. @...
  • 2,206
0 votes
2 answers
287 views

Is it possible to create a generic Map swapper?

I have a method: public Map<Object, Object> swap(Map<Object, Object> valueByKeyMap) { Map<Object, Object> keyByValueMap = new Map<Object, Object>(); for (Object key : ...
1 vote
2 answers
2k views

Is it possible to convert Map<Object, List<Object>> to Map<Id, List<SomeConcreteSObject>> without a loop?

As a Salesforce developer, I am frequently working with Maps and am looking to abstract the process of generating maps from SObject lists. I've created the following helper class: private class ...
-2 votes
1 answer
135 views

How to create a Map with generics Id and String?

How to create a Map<Id, String>([SELECT Id, Name from Account]);? The query returns Map<Id, Account>. I want to get a Map<Id, String> without using loops and without having to ...
  • 397
1 vote
1 answer
465 views

Dealing with Generics in Apex

I have some code that i want to try and emulate generics for. I know there are no generics in Apex but is there something we can do instead? Let me explain (String) sfBorr.get('SSN__pc'); sfBorr is ...
  • 397
8 votes
1 answer
465 views

If I know the API Name, how can I get and work with the Schema.ChildRelationship?

Like many SFDC developers, we have SObjects which are related to other custom relationships. So, for example, MyCustomObject__c could have a relationship with MyCustomRelationship__c which in Apex we ...
6 votes
2 answers
1k views

In Apex, can I detect if an Object is a Map, regardless of parameterized types?

When I declare a Map, I need to give the types, e.g.: Map<Id, SObject> sObjectByIdMap = new Map<Id, SObject>(); We can assign map to generic values as Objects: Object value = ...
10 votes
2 answers
8k views

What is the best way to cast/convert Map<String, SObject> to Map<Object, Object> and back again?

To reduce code duplication, at the risk of type safety, I have some method: public Map<Object, Object> doSomething(Map<Object, Object> inputMap) { Map<Object, Object> outputMap; ...
2 votes
1 answer
61 views

How can I abstract building maps from SObject Lists in a good way? [duplicate]

I frequently find I want/need to map a list of SObjects by a value other than it's own Id. So, I'd like to abstract this process to make it reusable. For example, I might want to map Contacts by ...
4 votes
2 answers
426 views

How can I upsert a homogeneous List<SObject>, in a good way? [duplicate]

I have some abstracted code, which at the end of some process will create a homogeneous list of SObjects. It could be a list of Accounts, it could be a list of contacts, it could be a list of some ...
1 vote
1 answer
1k views

Generic SObject Utility to dynamically set field values from Map

I'm trying to create a generic utility method which takes a List of sObjects and a Map of fieldNames to fieldValues. I'd like to, for example, do the following: List<Lead> leadList = [SELECT Id ...
  • 1,057
6 votes
3 answers
2k views

Does apex support generics?

I don't know if Apex supports generics but how can I make the below class more generics where I should be able to pass any data type? below is the example: the problem I have is this: public class ...
  • 6,712
7 votes
1 answer
2k views

Pros and cons of generic SObject list vs multiple lists of objects

For one of the requirements I have to write code to update multiple records of different objects. Originally I was planning to do the obvious: get needed records, change some value, update these lists,...
  • 4,992
13 votes
2 answers
9k views

How to write a generic list<t> method that will cast at runtime?

I recently tripped across the need to take a list of objects (instances of a class--not sobjects) and split them into smaller lists of 200 members each. The unit test below shows the code inside a ...
  • 447
5 votes
3 answers
5k views

Getting a type of the generic object

How to get a type of the generic object? I have a method that gets as parameter some object. It can be realy everything. In my example it is a list of strings. But it can be a map or set or some other ...
  • 21.7k
7 votes
1 answer
5k views

Is it possible to create a method that uses generics? [duplicate]

... or at least pass in a specific class? Here is what I have right now: private static invoice_response GetResponse(String jsonResponse) { invoice_response deserialized = (invoice_response) ...
  • 1,481
3 votes
1 answer
2k views

Is it possible to write a method that accepts both SObjects and Apex Class Instances?

I have an Apex method that takes a list of SObjects, a source field name, and a destination fieldname. For each SObject in the list, it looks at the source field, does some work, and populates the ...
  • 11.1k
33 votes
1 answer
7k views

Generic Apex Trigger

So I was kind of throwing round the idea of making a generic apex trigger that is callable by multiple objects. Say you have the following sObjects: Account Sales__c Sales_Contacts__c And I wanted ...
  • 845
14 votes
1 answer
5k views

How do I use Type.forName to deserialize a generic list of SObjects?

When the new Apex type methods were added in Summer '12, it was possible to do Type t = Type.forName('MyClass'); MyClass newObj = (MyClass)t.newInstance(); What is the equivalent when I want to ...
  • 16.2k
5 votes
1 answer
73 views

How to get the old Test Class UI back? Or possible workarounds with the new UI?

Is there anyone else who is sorely missing the old test class UI where it shows the entire list of Apex classes covered by running one test class? Another good feature of the old UI was that you could ...
  • 4,562
7 votes
2 answers
2k views

What is "Skip to main content"?

In any Salesforce page, if no element is in focus and you press Tab⇄, a hidden element Skip to main content is put into focus. What does Skip to main content do?
  • 4,562
10 votes
1 answer
8k views

What is the correct JSON to deserialize a map of sObjects?

I'm trying to store a map of sObjects in a static resource. I want to be able to store default values for test setup methods. I was able to get it working with the following JSON in the static ...
9 votes
4 answers
3k views

How do you deserialize json into the correct virtual class?

Let's say I have a virtual class and two classes that extend it and I am getting a JSON response that could be either one of those classes. Is there a way to deserialize the JSON into the appropriate ...
38 votes
2 answers
16k views

Can you use generics in Apex?

While researching the possibility of using generics in apex I can across a blog with this: "Generics are partially supported in Apex, you must be using them with Collections and Batch Apex." ...
  • 7,537