Get a field value from a SObject List Apex

0 votes

I'm facing with the current problem. I have the following code which is a list of SObject and I want to get "CreatedDate" field. I really appreciate ur time and comments, thanks.

public static Object testMeth() {
    List<SObject> leadsAndOpps = new List<SObject>();
    List<Lead> lstLead = [Select Id, Name, CreatedDate FROM Lead];
    List<Opportunity> lstOpp = [Select Id, Name, CreatedDate FROM Opportunity];

    //Assign first and second List to Sobject List
    leadsAndOpps.addAll(lstLead);
    leadsAndOpps.addAll(lstOpp);

    for(SObject lstExa : leadsAndOpps) {
        String getName = (String)lstExa.get('Name'); = This example works cool
        DateTime getDateVal = (DateTime)lstExa.get(CreatedDate); = Variable doesn't exists
    }
 return leadsAndOpps;
}
Mar 28, 2022 in SalesForce by surbhi
• 3,820 points
11,945 views

1 answer to this question.

0 votes
The field API Name is represented by a string in the SObject.get() method. It should be fine if you write 'CreatedDate' in quotes. What you're now passing in is interpreted as an undefined variable named CreatedDate.
answered Mar 29, 2022 by CoolCoder
• 4,420 points

Related Questions In SalesForce

0 votes
0 answers

Apex (Salesforce) read json from response and store it in a string variable

I am trying to hit a API ...READ MORE

Mar 4, 2022 in SalesForce by surbhi
• 3,820 points
1,232 views
0 votes
1 answer
0 votes
1 answer
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
3,457 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
1,068 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
785 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 746 views
0 votes
1 answer

How do i get all values from a global pick list in salesforce using REST Api?

Since API 39.0 it is possible to ...READ MORE

answered Mar 17, 2022 in SalesForce by CoolCoder
• 4,420 points
7,069 views
+1 vote
1 answer

Apex (Salesforce) read json from response and store it in a string variable

String jsonStr = '{ "oktaToken":"eyJraWQiOiIyNlN1NHFMNnVVZTVJX2M5X2Z3WmZvX09ON0dNUHRtQzlEeHFsTGplLS00IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULlVINmdIOF9BcWJ" }'; Map<String, String> ...READ MORE

answered Mar 3, 2022 in SalesForce by CoolCoder
• 4,420 points

edited Jun 27, 2023 by Khan Sarfaraz 9,690 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP