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
Sourav PSourav P 

Autopopulate a custom object field into the Lead object using a Trigger

Hi, I just wrote the below trigger but its showing the error, Can anyone plz help me out where it went wrong. thanks
Custom object : True_Promo__c, Custom field name : TrueYou_Privilege1_Code__c
Standaard Object : Lead, Custom Field Name : TrueYou_Privilege_Code__c
I want the "TrueYou_Privilege1_Code__c" input from the custo object should auto populate into teh lead object field.

ERROR showing : Compile Error : Invalid field lead for sObject True_Promos__c at line 18 column 13

trigger SetLeadOnTruePromo on True_Promos__c (before update, before insert) {
    Set<string> TruePrivileges = new Set<string>();

    for (True_Promos__c  checkTPrivilege : Trigger.new) {
    TruePrivileges.add(checkTPrivilege.TrueYou_Privilege1_Code__c);
    }

    List<Lead> LeadList = [SELECT id, TrueYou_Privilege_Code__c FROM Lead WHERE TrueYou_Privilege_Code__c IN :TruePrivileges];

    Map<string, Lead> PrivilegeCodeToLeadMap = new Map<string, Lead>();

    for (Lead c : LeadList) {
        PrivilegeCodeToLeadMap.put(c.TrueYou_Privilege_Code__c, c);
    }

    for (True_Promos__c  p : Trigger.new) {
        if (p.TrueYou_Privilege1_Code__c != null) {
            p.Lead = PrivilegeCodeToLeadMap.get(p.TrueYou_Privilege_Code__c).id;
        }
        else {
            p.Lead = null;
        }
    }
VineetKumarVineetKumar
There is no field TrueYou_Privilege_Code__c pm True_Promos__c, this field is available on Lead, the field that you want to refer is TrueYou_Privilege1_Code__cm check the below corrected code
trigger SetLeadOnTruePromo on True_Promos__c (before update, before insert) {
    Set<string> TruePrivileges = new Set<string>();
    for (True_Promos__c  checkTPrivilege : Trigger.new) {
        TruePrivileges.add(checkTPrivilege.TrueYou_Privilege1_Code__c);
    }

    Map<string, Lead> PrivilegeCodeToLeadMap = new Map<string, Lead>();
    for (Lead c : [SELECT id, TrueYou_Privilege_Code__c FROM Lead WHERE TrueYou_Privilege_Code__c IN :TruePrivileges]) {
        PrivilegeCodeToLeadMap.put(c.TrueYou_Privilege_Code__c, c);
    }

    for (True_Promos__c  p : Trigger.new) {
        if (p.TrueYou_Privilege1_Code__c != null && PrivilegeCodeToLeadMap.get(p.TrueYou_Privilege1_Code__c) != null) {
            p.Lead = PrivilegeCodeToLeadMap.get(p.TrueYou_Privilege1_Code__c).id;
        }else {
            p.Lead = null;
        }
    }
} 
Sourav PSourav P
Hi Vineet

Thanks for pointing out an issue. I ran your code, but till i am getting the same error alert " Error: Compile Error: Invalid field Lead for SObject True_Promos__c at line 14 column 13.
I cant understand why its showing invalid Lead field. Do we have to do somthing to the standard object Lead ? Thanks
VineetKumarVineetKumar
Yes, it is looking for a field on lead but there is no field in there.
Can you share your requirement?
Sourav PSourav P
Ya, Sure. I have created a custom object " True Promo" with a field "TrueYou_Privilege1_Code__c".  Now, I want the same records get autopopulate when created  in the Lead custom field that i have created " TrueYou_Privilege_Code__c". So the field actually do exists in the Lead.
VineetKumarVineetKumar
How is the True Promo object associated to lead? What is the lookup field API name?
Sourav PSourav P
I have created a look up relationship between Lead and True promo object. In the Lead , a field has been created " True Promos ( True_Promos__c) which is a look up field to " True Promo" object. and thats how both the objects are connected, Is that some issue here ?