+ Start a Discussion
SFAdmin5SFAdmin5 

Compile Error: Illegal assignment from SOBJECT:RDD_Territory__c to String at line 452

I'm working on part of an account object trigger we've got and keep getting a compile error.  

 

issue is this line

 

                String rddTerritory = userToRddTerritory.get(account.BillingPostalCode);

 

trigger is below.  any ideas how to fix this?

 

 

    // ------------------------------------------------------------------------
    // RDD Territory processing
    // ------------------------------------------------------------------------

 if ((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore) {
        
        List<Account> postalCodeAccountsToProcess = new List<Account>();
        List<Account> ownerAccountsToProcess = new List<Account>();
        Set<String> postalCodes = new Set<String>();
        Set<String> ownerIds = new Set<String>();
        
        Id accountRddRecordTypeId = CSUtils.getRecordTypeId('Account', 'RDD');
        
        for (Account account : Trigger.new) {
            if (account.BillingPostalCode != null && account.RecordTypeId != accountRddRecordTypeId) {
                if (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(account.Id).BillingPostalCode != account.BillingPostalCode)) {
                    postalCodeAccountsToProcess.add(account);
                    postalCodes.add(account.BillingPostalCode);
                }
            } else if (account.RecordTypeId == accountRddRecordTypeId) {
                if (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(account.Id).OwnerId != account.OwnerId)) {
                    ownerAccountsToProcess.add(account);
                    ownerIds.add(account.OwnerId);
                }
            }
        }

        if (postalCodeAccountsToProcess.size() > 0) {
            Map<String, RDD_Territory__c> postalCodeToRddTerritory = new Map<String, RDD_Territory__c>();
            for (RDD_Territory__c  rddTerritory : [select Zip_Code__c, Name from RDD_Territory__c where Zip_Code__c in :postalCodes]) {
                postalCodeToRddTerritory.put(rddTerritory.Zip_Code__c, rddTerritory);
            }
            
            for (Account account : postalCodeAccountsToProcess) {
                String rddTerritory = postalCodeToRddTerritory.get(account.BillingPostalCode);
                
                if (rddTerritory != null) {
                    account.RDD_Territory__c = rddTerritory;
                }
            }         
            
        }

        if (ownerAccountsToProcess.size() > 0) {
            Map<String, String> userToRddTerritory = new Map<String, String>();
            for (User  user : [select Id, RDD_Territory__c from User where Id in :ownerIds and RDD_Territory__c != null]) {
                userToRddTerritory.put(user.Id, user.RDD_Territory__c);
            }
            
            for (Account account : ownerAccountsToProcess) {
                String rddTerritory = userToRddTerritory.get(account.BillingPostalCode);

                if (rddTerritory != null) {
                    account.RDD_Territory__c = rddTerritory;
                }
            }
        }

    }

 

Best Answer chosen by Admin (Salesforce Developers) 
SFFSFF

Actually, I think this is the line that is causing your problem:

 

 

String rddTerritory = postalCodeToRddTerritory.get(account.BillingPostalCode);

If you look at the larger code block, you see that the postalCodeToRddTerritory map indexes RDD_Territory__c records by ID, but you are attempting to assign an RDD_Territory__c record to a String. You'll want to identify a field on that RDD_Territory__c record - Id? Name? - and append it to the statement:

 

String rddTerritory = postalCodeToRddTerritory.get(account.BillingPostalCode).Id;

Hope this helps,