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
Kent ManningKent Manning 

Need Help comparing two lists

I need some direction.  I currently have two lists - one called upgrades and the other called serial number / upgrades.  What I want my code to do is compare the values in the upgrades list with the values in the serial number / upgrades list.  If a value on the upgrades list is found on the serial number / upgrades list then don't do anything.   However, if a value on the upgrades list is NOT found on the serial number / upgrades list, then add that value to the serial number / upgrades list.  Both lists are variable in size, meaning that they could have 1 value or as many as 40 values.  How can I achieve this, with apex code preferably inside of a trigger? 

 

UPGRADES                                                          SN/UPGRADES

UG-001   <-----compare This     To all of these ---->   UG-004

                                                                                   UG-001 This value found, don't add it again.

UG-002 <-- This value            is not found here -->     ----       So add it to SN/UPGRADE list. 

UG-003 <-- This value            is not found here -->     ----        So add it to SN/UPGRADE list.

 

 

Any help would be appreciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

Hi Bioscience Man,

 

Perhaps something like:

set<string>upgrades = new set<string>();
set<string>SNUpgrades = new set<string>();

//populate upgrades/SNUpgrades from wherever...
upgrades.add('UG-001');
upgrades.add('UG-002');
upgrades.add('UG-003');
SNUPgrades.add('UG-004');
for(string s : upgrades){
if(!SNUpgrades.contains(s){
SNUpgrades.put(s);
//can do other custom code here...
}
}
system.debug(Upgrades);
system.debug(SNUpgrades);

Hope this helps.

Rich.

All Answers

Richie DRichie D

Hi Bioscience Man,

 

Perhaps something like:

set<string>upgrades = new set<string>();
set<string>SNUpgrades = new set<string>();

//populate upgrades/SNUpgrades from wherever...
upgrades.add('UG-001');
upgrades.add('UG-002');
upgrades.add('UG-003');
SNUPgrades.add('UG-004');
for(string s : upgrades){
if(!SNUpgrades.contains(s){
SNUpgrades.put(s);
//can do other custom code here...
}
}
system.debug(Upgrades);
system.debug(SNUpgrades);

Hope this helps.

Rich.

This was selected as the best answer
Kent ManningKent Manning

Rich,

 

Thanks for your suggestion.  Your code snippet worked beautifully!  Here is what I ended up with:

 

Set<ID> upGradesNeeded = new Set<Id>();  // declare a set of ID's called upGradesNeeded	
Set<ID> SN_upgrades_listed = new Set<Id>(); // declare a set of ID's called SN_upgrades_listed
		 	
 	// Loop thru the Upgrades list and populate the upGradesNeeded set. Put values into the set.
 	For(Upgrade__c ug_needed_ids : UG_List){
             upGradesNeeded.add(ug_needed_ids.id);
	}
	
	 	
	// Loop thru the SN Upgrades related list and populate the SN_upgrades_listed set - put values into the set.
	For(SN_Upgrades__c ug_listed_ids : snugRelatedList){
	     SN_upgrades_listed.add(ug_listed_ids.Upgrade__c);
	}
		 	
		 	
	// Compare the Upgrades needed with those listed on the SN Upgrades related list.  
	// Remove all items from the set that are the same, so that the result is(are) unique values.
	 if(upGradesNeeded.removeAll(SN_upgrades_listed)){
	            upGradesNeeded.removeAll(SN_upgrades_listed);
	 }
	 System.debug('The upgrades needed contains' + upGradesNeeded);

 Kent