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 

Trigger On Lead

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), can some one help me with the logic of getting the related techcian id and populate the Created date on technician.

Thank You.

pconpcon
To do this you will want to build a set of Technician Ids and include a query that gets the Leads for that technician.  Your code will be something like:
 
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 LatestLead__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.LatestLead__c != tech.Lead__r.get(0).CreatedDate) {
                tech.LatestLead__c = tech.Lead__r.get(0).CreatedDate;
                techsToUpdate.add(tech);
            }
        }
    }

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

I'm doing this from memory so the syntax for getting the leads from the subquery may be a bit off, but it should be enough to get you going.
WikWik

thank you for the reply.

i tried the below:
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;
    }
}

}

But it throws an error :  [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

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
pconpcon
As Sampath said, I do not know your data structure so I put in placeholder names.  You will need to replace Lead__r with the child relationship name for your relationship.  To see this you can go to your Technician object, goto your Lead field and on that field's information page there will be a field call "Child Relationship Name"  If you take that and append __r to the end that will be what you need to use in the SOQL query.