• brenda.buckner@rsmi.com
  • NEWBIE
  • 0 Points
  • Member since 2005

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
We have a trigger in place that until today keyed off of a Data.com NAICS Code field to update a few other fields based on the value in the NAICS Code field. We no longer have Data.com, and NAICS Code went with it. So, now the trigger is causing errors where no user can create/edit accounts, opportunities, or leads. We need to update the trigger (pasted below) and get that from Sandbox to production. We don't have a developer and we need the code to get this fixed ASAP. It appears the error happens at line 35. We need to change the trigger code from the old NAICSCode field to a custom NAICS__c field. HELP!!!

/**
*  Trigger that updates the account range to correctly reflect the Account Value
**/
trigger RSM_Account_Range_Trigger on Account (before insert, before update) {





    Decimal amount;
    String rangeLabel;
    String errorMessage = null;
    AccountRevenueRangeHelper ranges;
   
   
    /*
    //-- the only time an exceptionwill be thrown is if the picklist options are incorrectly formatted
    //-- HOWEVER, those options are not possible to be manipulated through apex/test. So the trigger would be unable to deploy
    //-- The exception is currently exposed to the trigger to stop the inserts, but is unable to be deployed either way.
    try {
        ranges = new AccountRevenueRangeHelper();
    } catch( Exception err ){
        //-- will only occur if the picklist options are incorrectly formatted
        for( Account a : Trigger.new ){
            a.Revenue_Range__c.addError( 'Error occurred while parsing available Account Revenue Range picklist options. Please contact your administrator for more details.');
        }
        return;
    }
    */
    String tempZip;
    //Find the AccountSIC info on incoming accounts.
    Set <String>    accountSICCodes = new Set<String>();
    Set <String>    accountZIPCodes = new Set<String>();
    for(Account a: Trigger.new){
        accountSICCodes.add(a.NaicsCode);
        if (a.BillingPostalCode != null){
          if (a.BillingPostalCode.length()>5){
            tempZip = a.BillingPostalCode.substring(0,5);
          }else{
            tempZip = a.BillingPostalCode;
          }
        }else{
            tempZip = a.BillingPostalCode;
        }
        accountZIPCodes.add(tempZip);
       
    }
   
    ranges = new AccountRevenueRangeHelper();
   
    //Pull all of the SIC codes in from the custom object
    List <SIC_Code_Industry__c> codeIndustry        = [SELECT Sector__c, Primary_SIC__c, NAICS_Code__c, Industry__c, Sic_Description__c, NAICS_Description__c FROM SIC_Code_Industry__c WHERE NAICS_Code__c in: accountSICCodes AND Unique_NAICS__c = true];
    List <Geography_Footprint__c> countyFootprint   = [SELECT Name, MSA__c, City__c, State__c, County__c, Market_Circle__c FROM Geography_Footprint__c WHERE Name in: accountZIPCodes];

    Map<String,SIC_Code_Industry__c> codeIndustryMap    = new Map<String, SIC_Code_Industry__c>();
    Map<String,Geography_Footprint__c> zipCountyMap     = new Map<String, Geography_Footprint__c>();
    //Create custom map of the SIC-Industry

    for (SIC_Code_Industry__c c: codeIndustry){
        //codeIndustryMap.put(c.Primary_SIC__c, c);
        codeIndustryMap.put(c.NAICS_Code__c,c);
    }
    for (Geography_Footprint__c d: countyFootprint){
        zipCountyMap.put(d.Name, d);       
    }
   
        //Loop through the leads, finding data via a map that references the NAICs value.  If the NAICS value is a match,
    //Populate the data on the Account (Description, Industry, Sector, etc) with the matching info.  Do the same with the
    //Geography Footprint matches.

    for(Integer i = 0; i < Trigger.new.size(); i++){
    //for ( Account e : Trigger.new ){
        Account e = Trigger.new[i];
        Account oldE = NULL;
        if(Trigger.isUpdate){
            oldE = Trigger.old[i];
        }
        SIC_Code_Industry__c currentIndustryInfo    = codeIndustryMap.get(e.NaicsCode);
        // new code to handle zip codes over 5 digits (Model Metrics - ZB - 8/2011)
        if (e.BillingPostalCode != null){
          if (e.BillingPostalCode.length()>5){
            tempZip = e.BillingPostalCode.substring(0,5);
          }else{
            tempZip = e.BillingPostalCode;
          }
        }else{
            tempZip = e.BillingPostalCode;
        }// end new code - ZB
        System.debug(tempZip);
        System.debug(zipCountyMap.get(tempZip));
        System.debug(zipCountyMap.get('55344'));
        System.debug(zipCountyMap.get(e.billingPostalCode));
        Geography_Footprint__c currentAccountZip    = zipCountyMap.get(tempZip);
        amount = e.AnnualRevenue;
        e.Revenue_Range__c = ranges.getRevenueRange( amount );
        if (currentIndustryInfo != null){
           e.Account_Industry__c = currentIndustryInfo.Industry__c;
           e.Industry_Sector__c  = currentIndustryInfo.Sector__c;
           /*
            e.NAICS_Description__c  = currentIndustryInfo.NAICS_Description__c;
           e.Four_Digit_SIC__c = currentIndustryInfo.Primary_SIC__c;
            e.SIC_Description__c = currentIndustryInfo.Sic_Description__c;
           */
            //e.NaicsCode = e.NAICS__c;
            e.NaicsDesc  = currentIndustryInfo.NAICS_Description__c;
            e.Sic = currentIndustryInfo.Primary_SIC__c;
            e.SicDesc  = currentIndustryInfo.Sic_Description__c;
           
        
        }
        if (currentAccountZip != null){
            if(trigger.isUpdate){
                if(e.BillingPostalCode != oldE.BillingPostalCode){
                    e.County__c = currentAccountZip.County__c;
                    e.BillingState = currentAccountZip.State__c;
                    e.BillingCity = currentAccountZip.City__c;
                    e.MSA__c = currentAccountZip.MSA__c;
                    //if(e.Market_Circle__c == null){
                        e.Market_Circle__c = currentAccountZip.Market_Circle__c;               
                    //}
                }
            } else {
                e.County__c = currentAccountZip.County__c;
                e.BillingState = currentAccountZip.State__c;
                e.BillingCity = currentAccountZip.City__c;
                e.MSA__c = currentAccountZip.MSA__c;
                //if(e.Market_Circle__c == null){
                    e.Market_Circle__c = currentAccountZip.Market_Circle__c;               
                //}
            }
           
            if(e.Market_Circle__c == 'New England' || e.Market_Circle__c == 'Northeast'){
                e.Region__c = 'Northeast';
            } else if (e.Market_Circle__c == 'Northwest' || e.Market_Circle__c == 'Southwest'){
                e.Region__c = 'West';
            } else if (e.Market_Circle__c == 'Out of Geography'){
                e.Region__c = 'National';
            } else if (e.Market_Circle__c == 'Great Lakes'){
                e.Region__c = 'Great Lakes';
            } else if (e.Market_Circle__c == 'Northern Plains' || e.Market_Circle__c == 'Southern Plains'){
                e.Region__c = 'Central';
            } else if (e.Market_Circle__c == 'Mid Atlantic' || e.Market_Circle__c == 'Southeast'){
                e.Region__c = 'Southeast';
            }
        }
    }
   
        // Populate the Account Site field with City, State, and Market Circle values
    for(Account ac: Trigger.new){
        ac.Site = ac.BillingCity+', '+ac.BillingState+' ('+ac.Market_Circle__c+')';
    }
}

I'm just getting my feet with with learning triggers, so I'm looking for help on code!

 

We have a custom opportunity field (Opportunity MC--basically like a region and/or area of the company the opportunity is being attributed to) that needs to be updated with the value of a custom field on the user page of the opportunity owner (EU). I've been manually doing this on new opportunities, but it would be nice if this is done automatically, as well as whenever the opportunity owner is changed (which is something not being captured currently). I came up with a formula I could use for a formula field [ TEXT(OwnerLookup__r.EU__c ) ], but I don't want to change the values on closed opportunities, when we used different names for the regions.

Essentially the Opportunity MC field needs to equal the EU field of the opportunity owner whenever a new opportunity is created or when the opportunity owner changes. Thanks for any help!

I'm just getting my feet with with learning triggers, so I'm looking for help on code!

 

We have a custom opportunity field (Opportunity MC--basically like a region and/or area of the company the opportunity is being attributed to) that needs to be updated with the value of a custom field on the user page of the opportunity owner (EU). I've been manually doing this on new opportunities, but it would be nice if this is done automatically, as well as whenever the opportunity owner is changed (which is something not being captured currently). I came up with a formula I could use for a formula field [ TEXT(OwnerLookup__r.EU__c ) ], but I don't want to change the values on closed opportunities, when we used different names for the regions.

Essentially the Opportunity MC field needs to equal the EU field of the opportunity owner whenever a new opportunity is created or when the opportunity owner changes. Thanks for any help!