+ Start a Discussion
erikrusselerikrussel 

Please Help - Simple Apex Trigger

Hello.  I have very little coding experence and was hoping someone could help me with what I'd think should rather be a rather simple Apex Trigger.  Thanks in advance.

 

I have a custom object called sundog_deprm2__Postal_Code__c which has a field called sundog_deprm2__County__c.  I have a custom field on the lead object called County__c.  I just want to pass the value from that sundog_deprm2__County__c field to the County__c lead field.  

 

Here is what I tried to write to no avial.

 

trigger CountyInsert on Lead (after insert)
{
list<sundog_deprm2__Postal_Code__c> PostalCodes = [select  Id, Name,sundog_deprm2__County__c from sundog_deprm2__Postal_Code__c w];

for (Lead lead : Trigger.new) {

County__c=lead.County_c;



}
}

steve456steve456

trigger UpdateCountyInfo on Lead(before insert,before update) {
Set<Id> assIdSet=new Set<Id>();

   forLead l:Trigger.new)
   {
       if(l.CommonLookupField != null) {
     
        assIdSet.add(l.CommonLookUpField);
        }
    }
   

       if(assIdSet.size()>0){
       Map<Id,sundog_deprm2__Postal_Code__c> assMap = new Map<Id,sundog_deprm2__Postal_Code__c>([Select Id,Name,sundog_deprm2__County__c from sundog_deprm2__County__c where Id IN: assIdSet]);
     
         for(Lead LD:Trigger.new)
         {
           if(LD.CommonLookupField!=null) 
           {
               LD.County__c = assMap.get(LD.CommonLookupField).sundog_deprm2__County__c; 

          }

         else{

                   LD.County__c=null;

                 }

                  

erikrusselerikrussel

Steve456 - Thank you so much for your help.  I greatly appricate it.

 

I tried using the code you provided on I got this error

 

"Error: Compile Error: unexpected token: ':' at line 4 column 12"

 

Any ideas?

steve456steve456

i dont  know what is the related field between both the objects so i wrote it as CommonLookupField please change that to the field which is  the relation between those

 

 

open the for loop ..i forgot 

 

for(Lead l:Trigger.new)

erikrusselerikrussel

I see.  That makes sense.  I got a new error.  Any idea what this means?

 

Error: Compile Error: sObject type 'sundog_deprm2__County__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 14 column 97

 

 

trigger UpdateCountyInfo on Lead(before insert,before update) {
Set<Id> assIdSet=new Set<Id>();

   for(Lead l:Trigger.new)
   {
       if(l.sundog_deprm2__Unique_Id__c != null) {
     
        assIdSet.add(l.sundog_deprm2__Unique_Id__c);
        }
    }
   

       if(assIdSet.size()>0){
       Map<Id,sundog_deprm2__Postal_Code__c> assMap = new Map<Id,sundog_deprm2__Postal_Code__c>([Select Id,Name,sundog_deprm2__County__c from sundog_deprm2__County__c where Id IN: assIdSet]);
     
         for(Lead LD:Trigger.new)
         {
           if(LD.sundog_deprm2__Unique_Id__c!=null)
           {
               LD.County__c = assMap.get(LD.sundog_deprm2__Unique_Id__c).sundog_deprm2__County__c;

          }

         else{

                   LD.County__c=null

                 }


steve456steve456

Map<Id,sundog_deprm2__Postal_Code__c> assMap = new Map<Id,sundog_deprm2__Postal_Code__c>([Select Id,Name,sundog_deprm2__County__c from sundog_deprm2__Postal_Code__c where Id IN: assIdSet]);

 

 

 

 

This should work
     

erikrusselerikrussel

Thank you so much. 

 

Should I be able to create a new lead and have the Trigger work before I create the test class or do  I need to create the test class first?  Because the trigger now saves without any errors but when instered a new lead the county field did not get filled.

 

I made sure the postal code I used was a postal code that had a postal code object record with a county.

 

Thanks again

steve456steve456

i think it shuld work ...no need to write test class.test class is only written for deploying into production