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
Osiris77706Osiris77706 

Invalid Foriegn key ? ..how ?

okay so im getting this error:

 

Compile Error: line 19, column 7: Invalid foreign key relationship: Lead.createdDate

 

 

with this code:

 

it comes from the part where i compare each aspect of CreatedDate to my variables.

     

 

 

public with sharing class stuff {
Date   oldThreshold = Date.newInstance(2010, 1, 1); 
String newStatusForOldLeads = 'Archive-Omitted';

   
List<Lead> oldLeads = [select id, createdDate, status, isConverted
from Lead where status = 'open' OR status = 'working'];

List<Lead> leadsToUpdate = new List<Lead>();


{
if(oldLeads.size() > 0)
{
 
 for(Lead currentLead: oldLeads)
 {  
  integer compareyear = 2010;
integer comparemonth = 1;
integer compareday = 1;
  //Date x = currentLead.CreatedDate.Date;
  
  if (currentLead.CreatedDate.year < compareyear &&
     currentLead.CreatedDate.month < comparemonth &&
     currentlead.CreatedDate.day < compareday)
     {
    
     
     
      currentLead.status = newStatusForOldLeads;  
      leadsToUpdate.add(currentLead);
 


       
     }
  }
}

if(leadsToUpdate.size() > 0)
{
update(leadsToUpdate);
}
 


}
}

 

I don't get it ?

Best Answer chosen by Admin (Salesforce Developers) 
ScoobieScoobie

I believe you'll have more success if you tried

 

currentLead.CreatedDate.year()

currentLead.CreatedDate.month()

currentLead.CreatedDate.day()

All Answers

ScoobieScoobie

I believe you'll have more success if you tried

 

currentLead.CreatedDate.year()

currentLead.CreatedDate.month()

currentLead.CreatedDate.day()

This was selected as the best answer
imuino2imuino2

You need to take .Date out of that sentece. The field is CreatedDate.

If you dont want the whole date or want to format it do it on the Datetime object you created.

You can see all the Date type methods in this page. Thats page is useful for many other things related with apex code.

 

Ignacio.

Osiris77706Osiris77706

HA ! thanx. thats what happens when u look at the same code wayyy too long in one day.