function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cmtgolf05cmtgolf05 

Invalid Date Format

I am trying to grab the date value from a custom Object and use that value to date range another query. Here is my code:

 

Date startDate = Date.valueof([Select startdate__c From Custom__c ]);

 

I do use a where clause to return just one value. StartDate__c is a date field on the Object.

 

This is then used in the following query:

 

Select OpportunityId From Opportunity Where Opportunity.CloseDate >= :startDate 

 

I get the following error:  Invalid date: common.apex.runtime.impl.SObjectList

 

any help?

 

 

SabrentSabrent

Try this ....

 

 

Date StartDate;


List<Custom__c> custom = [Select StartDate__c From Custom__c  WHERE  'your condition' ];

for(Custom__c cus : custom) {
  

  StartDate = Date.valueOf(cus.StartDate__c);
  system.debug('start date is' +StartDate);

 

}

 

List<Opportunity> opp = [Select Id,Name From Opportunity Where Opportunity.CloseDate >= :startDate ];

 

for (Opportunity o: opp){
  system.debug('Opportuniy name is' +o.Name);

  // Add your logic


}

cmtgolf05cmtgolf05

Thank you very much for your help.

 

I executed what you suggested however I get this error: "DML Requires SObject or SObject list type: Date"

 

Here is my updated code:

 

Date startDate;
        List<Custom__c> custom = [Select startDate__c from Custom__c Where Name = 'FreightDashboard' ];{
                   for (Custom__c cus:custom){
                        StartDate = Date.valueof(cus.StartDate__c);
                         system.debug('start date is'+StartDate);
                }
}

 

Thank you

rathi007rathi007

list<Date> startDate=new list<date>();

List<Custom__c> custom = [Select startDate__c from Custom__c Where Name = 'FreightDashboard' ];

 

for (Custom__c cus:custom)

{

StartDate.add(cus.StartDate__c);

system.debug('start date is'+StartDate);

}

 

try once again i hope it's helpful for you  

cmtgolf05cmtgolf05

Thank you again for your response however I am still getting an error.

 

Error: Illegeal assignment from Date to LIST<Date>

 

Here is the code I am using: 

list<Date> startDate = new list<date>();
      List<Custom__c> custom = [Select startDate__c from Custom__c Where Name = 'FreightDashboard' ];{
              for (Custom__c cus:custom){
               StartDate = Date.valueof(cus.StartDate__c);
               system.debug('start date is'+StartDate);
        }
}

 

I am a begginnger with SOQL but I know using PHP/MYSQL I can easily set a variable from a query of the database and then use that varable in another query. I have to believe I can do the same with APEX/SOQL.

 

Thank you,

rathi007rathi007

list<Date> startDate=new list<date>();

List<Custom__c> custom = [Select startDate__c from Custom__c Where Name = 'FreightDashboard' ];

 

for (Custom__c cus:custom)

{

StartDate.add(cus.StartDate__c);

system.debug('start date is'+StartDate);

}

 

try once again i hope it's helpful for you  

SabrentSabrent

If the startDate__c is of data type DateTime, you need to first convert it to Date before adding to the list.  

 

Do this -

 

list<Date> startDate=new list<date>();

List<Custom__c> custom = [Select startDate__c from Custom__c Where Name = 'FreightDashboard' ];

 

for (Custom__c cus:custom)

{

 

Date myDate = date.newinstance(cus.startDate__c.year(), cus.startDate__c.month(), cus.startDate__c.day());

startDate.add(myDate);

system.debug('start date is'+StartDate);

 

}