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 

Error: Illegeal assignment from Date to LIST<Date>

I am trying to pull the date off a custom SObject entry and use that date to filter another SOQL query. However I get an error everytime I try to set the variable to the date from the custom Sobject. Here is my code:

 

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);
        }
}

 

However I get the Error: Illegeal assignment from Date to LIST<Date>

 

Later I use StartDate in the follow query:

 

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

 

Any help as to how I can set a variable from the date stored on a custom sObject. The data type of the startDate__c on Custom__c is "Date".

 

Thank you.,

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal
Use
StartDate.add(Date.valueof(cus.StartDate__c));
Instead of
StartDate = Date.valueof(cus.StartDate__c);

All Answers

Dhaval PanchalDhaval Panchal
Use
StartDate.add(Date.valueof(cus.StartDate__c));
Instead of
StartDate = Date.valueof(cus.StartDate__c);
This was selected as the best answer
AshlekhAshlekh

Hi 

 

Try this code:

 

      List<Date> startDate = new list<date>();
      List<Custom__c> custom = [Select startDate__c from Custom__c Where Name = 'FreightDashboard' ];

      for (Custom__c cus:custom)

       {

            if(cus.StartDate__c !=null)

            {

               StartDate.add( Date.valueof(cus.StartDate__c));  //We always add item in list instead of assign (StartDate = Date.valueOF(cus.StartDate))
                system.debug('start date is'+StartDate);
             }         

        }

 

if cus.StartDate is date type field then you directly add in list not need to parse.

eg .StartDate.add(cus.startDate); but verify cus startdate in not null.

 

Thanks

Ashlekh

 

If this post helps you then mark it as a solution and don't forget to give me kudo's 

cmtgolf05cmtgolf05

Thank you both for you reply.

 

The following worked perfectly:

 

StartDate.add(Date.valueof(cus.StartDate__c));    .

 

Thank you again!