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
Greg MeszarosGreg Meszaros 

Cannot access fields in loop for comparison and can't update records. Please help

I am trying to search through all records passed in by trigger and find ones where buyer zip codes matches an open Open Territory. The "Name" field on Open_Territory__c contains zip codes.

I am getting 2 errors. Variable does not exist: Name, and DML requires SObject or SObject list type: List<Id>.

Any help would be appreciated. 
 
public static void setOpenTerritory(List<dealer__Sales_Up__c> triggerValues) {
        List<Open_Territory__c> openList = [SELECT Id, Name FROM Open_Territory__c WHERE Is_Active__c = true];
        List<Id> suToUpdate = new List<Id>();
        System.debug('openList contains ' + openList);
        // Search through all Sales Ups passed in trigger. Find ones with buyer zip codes matches Open Territory zip and add them to List.
        for(dealer__Sales_Up__c s : triggerValues) { 
            if(s.dealer__Mailing_Zip__c === openList.Name){
                s.Open_Territory__c = true;
                suToUpdate.add(s.Id);
            }
        }
        update suToUpdate;
    }

 
Best Answer chosen by Greg Meszaros
v varaprasadv varaprasad
//== Before insert
public static void setOpenTerritory(List<dealer__Sales_Up__c> triggerValues) {
        List<Open_Territory__c> openList = [SELECT Id, Name FROM Open_Territory__c WHERE Is_Active__c = true];
         System.debug('openList contains ' + openList);
        
        list<string> zips = new list<string>();

        for(Open_Territory__c op : openList){
            zips.add(op.name);
            }
       
        for(dealer__Sales_Up__c s : triggerValues){
       system.debug('===ter '+zips.contains(s.dealer__Mailing_Zip__c));
       
            if(zips.contains(s.dealer__Mailing_Zip__c)){                                       
                      s.Open_Territory__c = true;                
            }
        }
       
    }

All Answers

v varaprasadv varaprasad

Hi Greg,

Please check once below sample code : 

//After insert

public static void setOpenTerritory(List<dealer__Sales_Up__c> triggerValues) {
        List<Open_Territory__c> openList = [SELECT Id, Name FROM Open_Territory__c WHERE Is_Active__c = true];
         System.debug('openList contains ' + openList);
		
        list<string> zips = new list<string>();

        for(Open_Territory__c op : openList){
		    zips.add(op.name);
		}		
		 
		List<dealer__Sales_Up__c> suToUpdate = new List<dealer__Sales_Up__c>();
       
        // Search through all Sales Ups passed in trigger. Find ones with buyer zip codes matches Open Territory zip and add them to List.
        for(dealer__Sales_Up__c s : triggerValues){
       system.debug('===ter '+zips.contains(s.dealer__Mailing_Zip__c));
	   
            if(zips.contains(s.dealer__Mailing_Zip__c)){
                s.Open_Territory__c = true;
                suToUpdate.add(s.Id);
            }
        }
        update suToUpdate;
    }
 



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 

v varaprasadv varaprasad
//== Before insert
public static void setOpenTerritory(List<dealer__Sales_Up__c> triggerValues) {
        List<Open_Territory__c> openList = [SELECT Id, Name FROM Open_Territory__c WHERE Is_Active__c = true];
         System.debug('openList contains ' + openList);
        
        list<string> zips = new list<string>();

        for(Open_Territory__c op : openList){
            zips.add(op.name);
            }
       
        for(dealer__Sales_Up__c s : triggerValues){
       system.debug('===ter '+zips.contains(s.dealer__Mailing_Zip__c));
       
            if(zips.contains(s.dealer__Mailing_Zip__c)){                                       
                      s.Open_Territory__c = true;                
            }
        }
       
    }
This was selected as the best answer
Raj VakatiRaj Vakati
Try this 
 
public static void setOpenTerritory(List<dealer__Sales_Up__c> triggerValues) {
        List<Open_Territory__c> openList = [SELECT Id, Name FROM Open_Territory__c WHERE Is_Active__c = true];
        Set<Sting> str = new Set<String>() ;
		
		For(Open_Territory__c ot : openList){
			str.add(ot.Name);
		}
		List<dealer__Sales_Up__c> suToUpdate = new List<dealer__Sales_Up__c>();


        // Search through all Sales Ups passed in trigger. Find ones with buyer zip codes matches Open Territory zip and add them to List.
        for(dealer__Sales_Up__c s : triggerValues) { 
            if(str.contains(s.dealer__Mailing_Zip__c)){
                s.Open_Territory__c = true;
                suToUpdate.add(s);
            }
        }
        update suToUpdate;
    }