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
George Laird 12George Laird 12 

Need Trigger Help please!

Hello All!!  I need some guidence with a trigger or class.  I'm new to developing in general so I was hoping that some of the fantastic experts here could help me out.  Here's the deal:

I have a custom object called 'SIC Codes' that has a code as the Name and 4 fields that are simply a 'yes' or 'no' text field.  So a record would look like this:

Name: 1234
Description__c = 'doctors'
WC__c = yes
BOP__c no
GA__c = yes
GL__c = no

Now, on the Lead record I have a description text field that will match a SIC CODE record's description.  So a lead may will have a description field that will say 'doctors'.    The lead also has 4 text fields that match the fields above on the custom object's record.   

What I am trying to do is say "If a lead comes in with a description that matches a description on the custom object, copy the values of the 4 custom fields to the lead record with those same custom fields.  The result would be that the lead's field values for WC__c, BOP__c, ect... will match the 'yes' or 'no' from the record on the custom object that matches that description.  

Now, i have a lead handler where I will put this trigger and call a class that will do all the work.  I'm kind of overwhelmed and don't know how to get started.  Any adivce or guidence would be increcible!  

Thanks so much!

 
Best Answer chosen by George Laird 12
Hemant_SoniHemant_Soni
Hi ,
A Small Change Please Try Below One.
trigger LeadTrigger on Lead (before insert) {
    Map<String,SIC_Codes__c> mapOfDescriptionWiseSICCOde = new Map<String,SIC_Codes__c>();
    for(SIC_Codes__c sCOdes : [Select id,Name,Description__c,WC__c,BOP__c,GA__c,GL__c From SIC_Codes__c]){
        mapOfDescriptionWiseSICCOde.put(sCOdes.Description__c,sCOdes);
    }
    
    for(Lead oLead : trigger.new){
        if(String.isNotBlank(oLead.Description) && mapOfDescriptionWiseSICCOde.containsKey(oLead.Description)){
            oLead.WC__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).WC__c;
            oLead.BOP__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).BOP__c;
            oLead.GA__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GA__c;
            oLead.GL__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GL__c;
        }
    }
}

 

All Answers

Hemant_SoniHemant_Soni
Hi,
Assuming that You are having Single SIC_Codes__c Record With Unique Description
Please try below code and update field accordingly.
trigger LeadTrigger on Lead (before insert) {
    Map<String,SIC_Codes__c> mapOfDescriptionWiseSICCOde = new Map<String,SIC_Codes__c>();
    for(SIC_Codes__c sCOdes : [Select id,Name,Description__c,WC__c,BOP__c,GA__c,GL__c]){
        mapOfDescriptionWiseSICCOde.put(sCOdes.Description__c,sCOdes);
    }
    
    for(Lead oLead : trigger.new){
        if(String.isNotBlank(oLead.Description) && mapOfDescriptionWiseSICCOde.containsKey(oLead.Description)){
            oLead.WC__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).WC__c;
            oLead.BOP__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).BOP__c;
            oLead.GA__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GA__c;
            oLead.GL__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GL__c;
        }
    }
}
Please try it and let me know if you are facing any issue.
Thanks
 
Hemant_SoniHemant_Soni
Hi ,
A Small Change Please Try Below One.
trigger LeadTrigger on Lead (before insert) {
    Map<String,SIC_Codes__c> mapOfDescriptionWiseSICCOde = new Map<String,SIC_Codes__c>();
    for(SIC_Codes__c sCOdes : [Select id,Name,Description__c,WC__c,BOP__c,GA__c,GL__c From SIC_Codes__c]){
        mapOfDescriptionWiseSICCOde.put(sCOdes.Description__c,sCOdes);
    }
    
    for(Lead oLead : trigger.new){
        if(String.isNotBlank(oLead.Description) && mapOfDescriptionWiseSICCOde.containsKey(oLead.Description)){
            oLead.WC__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).WC__c;
            oLead.BOP__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).BOP__c;
            oLead.GA__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GA__c;
            oLead.GL__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GL__c;
        }
    }
}

 
This was selected as the best answer
George Laird 12George Laird 12
Awesome!  I'll try this right now.  I have a few changes to make so I'll try it and post what I did here.  Is it OK if I keep checking in with you on this?
George Laird 12George Laird 12
@Hemant_Soni, here's what I have so far, but I'm getting an error of:

 duplicate value found: <unknown> duplicates value on record with id: <unknown>




trigger LeadTrigger on Lead (before insert) {
    
    string sicDesc;
    
    for(Lead l : Trigger.new){
      sicDesc = l.SIC_Code_Description__c;    
        
    }
    
    Map<String,VRNA__SIC_NAICS_Code__c> mapOfDescriptionWiseSICCOde = new Map<String,VRNA__SIC_NAICS_Code__c>();
    for(VRNA__SIC_NAICS_Code__c sCOdes : [SELECT Name, VRNA__Unique_ID__c,VRNA__Description__c,WC__c,GL__c,GA__c,BOP__c
                                                   FROM VRNA__SIC_NAICS_Code__c 
                                                   WHERE VRNA__Description__c =: sicDesc
                                                   LIMIT 1]){
        mapOfDescriptionWiseSICCOde.put(sCOdes.VRNA__Description__c,sCOdes);
    }
    
    for(Lead oLead : trigger.new){
        if(String.isNotBlank(oLead.Description) && mapOfDescriptionWiseSICCOde.containsKey(oLead.Description)){
            oLead.WC__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).WC__c;
            oLead.BOP__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).BOP__c;
            oLead.GA__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GA__c;
            oLead.GL__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GL__c;
            
        }
    }
}
 
George Laird 12George Laird 12
@Hemant_Soni, here's what I have so far, but I'm getting an error of: duplicate value found: duplicates value on record with id: trigger LeadTrigger on Lead (before insert) { string sicDesc; for(Lead l : Trigger.new){ sicDesc = l.SIC_Code_Description__c; } Map mapOfDescriptionWiseSICCOde = new Map(); for(VRNA__SIC_NAICS_Code__c sCOdes : [SELECT Name, VRNA__Unique_ID__c,VRNA__Description__c,WC__c,GL__c,GA__c,BOP__c FROM VRNA__SIC_NAICS_Code__c WHERE VRNA__Description__c =: sicDesc LIMIT 1]){ mapOfDescriptionWiseSICCOde.put(sCOdes.VRNA__Description__c,sCOdes); } for(Lead oLead : trigger.new){ if(String.isNotBlank(oLead.Description) && mapOfDescriptionWiseSICCOde.containsKey(oLead.Description)){ oLead.WC__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).WC__c; oLead.BOP__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).BOP__c; oLead.GA__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GA__c; oLead.GL__c = mapOfDescriptionWiseSICCOde.get(oLead.Description).GL__c; } } } George Laird AVP Call Center Systems and Operations AmVenture Insurance Agency, Inc. o|818.255.7253 c|818.568.9982 [AmVenture Logos - Final_R2_tm-Color_H (002)] [Admin5][AdvanceAdmin5][AppBuilder5][Dev5]