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
Andrew GAndrew G 

Using List contains does detect Id

Hello

I'm doing a bit of code where I'm checking if a Work Type Id in a Work Order is in a List for Work Type Ids, but the contains method does not seem to be detecting the match:
Code snippet 
List<ServiceAppointment> svcAppts = new List<ServiceAppointment>();

		//List of Ids where their is an auto generate feature enabled
		List<Id> listWorkTypeIds = new List<Id>();
		for ( WorkType wt : [SELECT Id FROM WorkType WHERE ShouldAutoCreateSvcAppt = TRUE] )  {
			listWorkTypeIds.add(wt.Id);
		}
		System.debug('DEBUG : Worktype count ' + listWorkTypeIds.size());

		
		for ( WorkOrder workOrder : workOrders ) {
			tempDecimal = workOrder.Crew_Size__c;
			crewSize  = tempDecimal.intValue();
			System.debug('DEBUG : crewSize: ' + crewSize);
			
			tempDecimal = workOrder.ServiceAppointmentCount;
			System.debug('DEBUG : RAW service count : ' + tempDecimal);
			System.debug('****DEBUG : list work type : ' + listWorkTypeIds[0]);
			System.debug('****DEBUG : WO work type : ' + workOrder.workTypeId);

			if (listWorkTypeIds.contains(workOrder.workTypeId)) {
				apptCount = tempDecimal.intValue() + 1;		
				System.debug('DEBUG : ADJUSTED service count : ' + tempDecimal);				
			} else {
				apptCount = tempDecimal.intValue();
				System.debug('DEBUG : SAME service count : ' + tempDecimal);
			}
Now, the debug looks like this:
08:10:49.875 (1834107162)|SOQL_EXECUTE_BEGIN|[92]|Aggregations:0|SELECT Id FROM WorkType 
08:10:49.875 (1848717624)|SOQL_EXECUTE_END|[92]|Rows:1
08:10:49.875 (1849133376)|USER_DEBUG|[95]|DEBUG|DEBUG : Worktype count 1
08:10:49.875 (1849315483)|USER_DEBUG|[101]|DEBUG|DEBUG : crewSize: 1
08:10:49.875 (1849418406)|USER_DEBUG|[104]|DEBUG|DEBUG : RAW service count : 0
08:10:49.875 (1849462073)|USER_DEBUG|[105]|DEBUG|****DEBUG : list work type : 08q0l00000001jtAAA
08:10:49.875 (1849543426)|USER_DEBUG|[106]|DEBUG|****DEBUG : WO work type : 08q0l00000001jtAAA
08:10:49.875 (1849692808)|USER_DEBUG|[113]|DEBUG|DEBUG : SAME service count : 0

So, the debug shows that the Ids are apparently the same, but the IF statement using the the contains does resolve to TRUE.

Any thoughts on what I am missing?

Regards
Andrew
 
Best Answer chosen by Andrew G
Andrew GAndrew G
Resolved the issue by declaring as a list of Strings and comparing Strings, not Ids.
 
List<String> listWorkTypeIds = new List<String>();

List<Worktype> workTypes = new List<WorkType>([SELECT Id FROM WorkType WHERE ShouldAutoCreateSvcAppt = TRUE]);
		for ( WorkType wt : workTypes )  {
			listWorkTypeIds.add(wt.Id);
		}	

		tempString = workOrder.WorkTypeId;
		if (listWorkTypeIds.contains(tempString)) {
				System.debug('DEBUG : work type is auto create');
				//do stuff
				} else {
					System.debug('DEBUG : work type has NOT changed');
				}

would have thought Ids would have worked.

Cheers to me
 

All Answers

Andrew GAndrew G
Resolved the issue by declaring as a list of Strings and comparing Strings, not Ids.
 
List<String> listWorkTypeIds = new List<String>();

List<Worktype> workTypes = new List<WorkType>([SELECT Id FROM WorkType WHERE ShouldAutoCreateSvcAppt = TRUE]);
		for ( WorkType wt : workTypes )  {
			listWorkTypeIds.add(wt.Id);
		}	

		tempString = workOrder.WorkTypeId;
		if (listWorkTypeIds.contains(tempString)) {
				System.debug('DEBUG : work type is auto create');
				//do stuff
				} else {
					System.debug('DEBUG : work type has NOT changed');
				}

would have thought Ids would have worked.

Cheers to me
 
This was selected as the best answer
Ken Fitzpatrick 10Ken Fitzpatrick 10
The solution above is a good workaround, but I found there is a bug with the List.Contains() method. I ran into a similar situation where the Contains method could not see the second element in a list size of 2. Now, I didn't check with a larger list to see if it just couldn't see the last element, or if it could only see the first element, but it definitely couldn't find the right value.

Now, the weirdest part of all this for me was; if I added a debug line somewhere that just referenced the list variable name, everything worked fine. For example, if you are experiencing the same issue as I did, if you put System.Debug(listWorkTypeIds); between line 4 and line 5 in your original code above, I bet your code will work with the List<Id>. If you get a chance, try it, and let me know if that works for you. Now of course, adding a debug line to fix code is not a great idea, which is why I like changing the method to return a String rather than a List<Id>, that's what I did to get around this issue. Also, I tried List<String> and List<Object> and still had the same problem, but again, the debug line made it work every time.