+ Start a Discussion
Larry D. DortchLarry D. Dortch 

Account field update with Cross Object value

Hello Group,
Need help writing a Trigger on Account object that will run when an account is created or updated.  We use "Service Location Codes" based on the address zip code.  I have a custom object named SLC to Zip where I want to match the account address zip to the SLC Zip, get the SLC Code (ie. CD3BAY) and update it on the account custom field Service_Location_Code__c

Here's the code I have written so far and any help is much appreciated!
// @Description: - Trigger Logic:-

//                1) Collect Account Ids in Set.
//                2) Create Map of Address ID to Zipcode.
//                3) Create Map of ZipCode and SLC to Zip Object.
//                4) Update Account obj Service_Location_Code__c on zip match

trigger SLCInsertAndUpdate on Account (after insert, after update){
    // declare an instance of Account obj as Variable: a
    Account a = new Account();

    List<Account> aSLCupdateList = new List<Account>();
    Map<id, Account> mapAccount = new Map<id, Account>();
    Map<String, String> aZipMap = new Map<String, String>();
    Map<String,SLC_to_ZIP__c> zipCodeSLCMap = new Map<String,SLC_to_ZIP__c>();
    Set<Id> accIDs = new Set<Id>();
    for(SLC_to_ZIP__c zipCode : [SELECT Id, SLC_Code__c, Zip_Code__c
                                     FROM SLC_to_ZIP__c
                                     WHERE Zip_Code__c IN: aZipMap.values()]) {
        system.debug('check zipCodeSLCMap' + zipCodeSLCMap);
        system.debug('check SLC Cose' + zipCode.SLC_Code__c);
           a.Service_Location_Code__c = zipCode.SLC_Code__c;
            if(!mapAccount.containsKey(a.id)) {
                mapAccount.put(a.id, a);
        if(mapAccount.values().size() > 0) {
            update mapAccount.values();

Michael Johnson 136Michael Johnson 136
WHERE Zip_Code__c IN: aZipMap.values()

aZipMap is an empty map, you never put any values in it. You probably need to iterate through the trigger list you are inserting/updating and add the values to it or this statement should always return a blank list (unless you have SLC_Zip__c records where Zip_Code__c is blank). I would make Zip_Code__c required and unique if you are using this as a key as well.
Larry D. DortchLarry D. Dortch
Thanks for your response Michael.  I pointed the Where clause to ierate the Account list (WHERE Zip_Code__c IN: aSLCupdateList.values()])
but getting this error:  Method does not exist or incorrect signature: void values() from the type List<Account>
Andrew GAndrew G
you are asking the SOQL query to search based on a list of objects.  It won't work.  You need a list a primitives such as string, integer, id.  A complex object like an object can't be used.
Remember the WHERE Zip_Code__c ... Zip Code as a field contains a basic number or string.  So what you pass needs to match that.