You need to sign in to do that
Don't have an account?

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.
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.
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
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