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
WikWik 

Error in Trigger

Hi,
There is a lookup relationship on Lead to a Custom Object "Technician".
I am trying to get the Created date of the latest Lead created on the related Technican Record.
I am writing a trigger on Lead (after insert),

trigger LeadDate on Lead (after insert) {

Set<Id> techIds = new Set<Id>();

for (Lead lead : Trigger.new) {
    techIds.add(lead.Technician__c);
}

techIds.remove(null);

if (!techIds.isEmpty()) {
    List<Technician__c> techsToUpdate = new List<Technician__c>();

    for (Technician__c tech : [
        select LatestLeadDate__c,
            (
                select CreatedDate
                from Lead__r
                order by CreatedDate asc
                limit 1
            )
        from Technician__c
        where Id in :techIds
    ]) {
        if (!tech.Lead__r.isEmpty()) {
            if (tech.LatestLeadDate__c != tech.Lead__r.get(0).CreatedDate) {
                tech.LatestLeadDate__c = tech.Lead__r.get(0).CreatedDate;
                techsToUpdate.add(tech);
            }
        }
    }

    if (!techsToUpdate.isEmpty()) {
        update techsToUpdate;
    }
}

}

Error: Error: Compile Error: Didn't understand relationship 'Lead__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 14 column 31

Thank You.
Best Answer chosen by Wik
Sampath KumarSampath Kumar
Hi Wik,

The code you have written is correct, go to technician and check for LatestLeadDate__c  date, it should be the latest created date. If you will use the above code, query is on all technicians which may lead to performance issues if we have more technicians. 

In your code, we are considering the technicians only for leads getting inserted.

Let me know if you are still not able to get the correct date.

Regards
Sampath Kumar Goud

All Answers

PratikPratik (Salesforce Developers) 
Hi Wik,

Lead is standard object so you don't need Lead__r to refer to it's relationship in SOQL query. Try using Lead instead Lead__r.

Thanks,
Pratik
WikWik

same error:


Error: Compile Error: Didn't understand relationship 'Lead' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 14 column 31

 

Sampath KumarSampath Kumar

Hi Wik,


Go to object Lead object, open it and check for Technician field, open it you can see child relationship name, place in place of lead__r - childrelationname__r

Mark this as best answer if it solves your query.

Regards
Sampath Kumar Goud

WikWik

thank you Sampath. That did the trick.

Facing another issue here

for the below part : 
 if (!tech.Lead.isEmpty()) {
            if (tech.LatestLeadDate__c != tech.Leads__r.get(0).CreatedDate) {
                tech.LatestLeadDate__c = tech.Leads__r.get(0).CreatedDate;
                techsToUpdate.add(tech);

error is being thrown : Error: Compile Error: Invalid field lead for SObject Technician__c at line 25 column 14

The lookup is created on the Lead object.

Sampath KumarSampath Kumar
Hi Wik,

Write !tech.Lead__r.isEmpty() as tech.Lead__r.size() > 0 where Lead__r is the childrelation name as metioned earlier.

Mark this as best answer if this solves your query.

Regards
Sampath Kumar Goud

 
Anupam RastogiAnupam Rastogi

Hi Wik,

You need to find the latest created lead for any particular Technician. Therefore you should start from Technician. Here is the sample code that I tried and worked for me for similar scenario that I could create in my Org.

Basically you first get all the technicians that are present in the Org and are active.
Then go through that list and search for the Leads associated with them ordered by Created date in desc order and limit it to 1.
This should give you the desired result.

Hope this helps.

List<Technician__c> tList = [select Id from Technician];
        
        for(Technician__c tc : tList)
        {
            List<Lead> l = [select Id, CreatedDate from Lead where Technician__c = :tc.Id
                                      order by CreatedDate desc limit 1];
            if(l.size() == 1)
            {
                //---Here you get the created date of the latest lead created for this Technician
            }
        }


Thanks
Anupam

PS: If this solves your problem then do remember to mark the answer as the best answer.

WikWik

i have this final piece of code

trigger LeadDate on Lead (after insert) {

Set<Id> techIds = new Set<Id>();

for (Lead lead : Trigger.new) {
    techIds.add(lead.Technician__c);
}

techIds.remove(null);

if (!techIds.isEmpty()) {
    List<Technician__c> techsToUpdate = new List<Technician__c>();

    for (Technician__c tech : [
        select LatestLeadDate__c,
            (
                select CreatedDate
                from Leads__r
                order by CreatedDate asc
                limit 1
            )
        from Technician__c
        where Id in :techIds
    ]) {
        if (!tech.Leads__r.isEmpty()) {
            if (tech.LatestLeadDate__c != tech.Leads__r.get(0).CreatedDate) {
                tech.LatestLeadDate__c = tech.Leads__r.get(0).CreatedDate;
                techsToUpdate.add(tech);
            }
        }
    }

    if (!techsToUpdate.isEmpty()) {
        update techsToUpdate;
    }
}

}

but it is returning the Technicians created date rather than the Leads created date

Anupam RastogiAnupam Rastogi
You have queried starting from Leads therefore it is returning Technician's Created Date. And moreover I do not see the need for this long code.

Try this simple version. Just check the logs that the correct Lead for each Technician is coming as the output.

trigger LeadDate on Lead (after insert) {

List<Technician__c> tList = [select Id from Technician__c];
        
        for(Technician__c tc : tList)
        {
            List<Lead> l = [select Id, CreatedDate from Lead where Technician__c = :tc.Id
                                      order by CreatedDate desc limit 1];
            if(l.size() == 1)
            {
                System.debug('For Technician: '+ tc.Id + ' the latest Lead created is ' + l[0].Id + ' with created date: ' + l[0].CreatedDate);
                //---Here you get the created date of the latest lead created for this Technician
            }
        }
}


Thanks
Anupam
Sampath KumarSampath Kumar
Hi Wik,

The code you have written is correct, go to technician and check for LatestLeadDate__c  date, it should be the latest created date. If you will use the above code, query is on all technicians which may lead to performance issues if we have more technicians. 

In your code, we are considering the technicians only for leads getting inserted.

Let me know if you are still not able to get the correct date.

Regards
Sampath Kumar Goud
This was selected as the best answer