0

I am new to Salesforce, and programming. Is there a way to reference CustomObject__c when adding to list, without hardcoding the name of the field (customObject__c)? Example:

If(Name ='123'){
updateListOpp.add(new Opportunity(customObject__c = null);
}

I have multiple custom objects that need to be added to a list, So I just wanted to know if there is a better way, instead of having to hardcode each custom object and having multiple conditional statements.

2
  • 1
    There's a bunch of questions covering this. These questions I quickly found, there's heaps more on here as well. salesforce.stackexchange.com/questions/324336/… salesforce.stackexchange.com/questions/305507/… ? If not, can you edit your question to provide more information and why the other questions on here don't cover what you're after.
    – Nick Cook
    Aug 16, 2021 at 3:52
  • Nick, thank you for the links. Basically, I was having issues referencing when trying to add the custom field to a list. This is why other questions are not covering what I'm after.
    – Y_m5437
    Aug 16, 2021 at 3:58

1 Answer 1

4

SObject behaves like a map, meaning you can use .put() method like this.

List<SObject> sObjects = new List<SObject>();

Opportunity o = new Opportunity();
o.put('Name', 'asd');
sObjects.add(o);

Lead l = new Lead();
l.put('Email', 'asd@asd.asd');
sObjects.add(l);

System.debug(sObjects);
1
  • Thank you for your response, I have accepted your answer and help @damecek
    – Y_m5437
    Aug 16, 2021 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.