5

In below code, we can observe that 'actionPlanTempList' contains an ID 'a076F00000uedBaQAI'. Also ap_Action_Plan_Task_Dependency__c object also contains records for Action_Plan_Template__c=a076F00000uedBa. However 3rd Select statement returns nothing.

System.debug('actionPlanTempList - '+actionPlanTempList);
//returns: actionPlanTempList - (a076F00000uedBaQAI)


List<ap_Action_Plan_Task_Dependency__c> allldependencyList 
    = [Select ID, Action_Plan_Template__c, Dependent_Task__c, Dependent_On_Task__c From ap_Action_Plan_Task_Dependency__c]; 

System.debug('allldependencyList - '+allldependencyList);
//returns: allldependencyList - (ap_Action_Plan_Task_Dependency__c:{Id=a096F00002LKwSSQA1, Action_Plan_Template__c=a076F00000uedBa, Dependent_Task__c=a086F00002DtsfCQAR, Dependent_On_Task__c=a086F00002DtsfAQAR}, ap_Action_Plan_Task_Dependency__c:{Id=a096F00002LKwScQAL, Action_Plan_Template__c=a076F00000uedBa, Dependent_Task__c=a086F00002DtsfoQAB, Dependent_On_Task__c=a086F00002DtsfCQAR}, ap_Action_Plan_Task_Dependency__c:{Id=a096F00002LKwSIQA1, Action_Plan_Template__c=a076F00000uedBa, Dependent_Task__c=a086F00002DtsfBQAR, Dependent_On_Task__c=a086F00002DtsfAQAR}, ap_Action_Plan_Task_Dependency__c:{Id=a096F00002LKwShQAL, Action_Plan_Template__c=a076F00000uedBa, Dependent_Task__c=a086F00002DtsfoQAB, Dependent_On_Task__c=a086F00002Dtsf0QAB}, ap_Action_Plan_Task_Dependency__c:{Id=a096F00002LKwSXQA1, Action_Plan_Template__c=a076F00000uedBa, Dependent_Task__c=a086F00002DtsfoQAB, Dependent_On_Task__c=a086F00002DtsfBQAR})


List<ap_Action_Plan_Task_Dependency__c> dependencyList 
    = [Select ID, Action_Plan_Template__c, Dependent_Task__c, Dependent_On_Task__c From ap_Action_Plan_Task_Dependency__c 
       Where Action_Plan_Template__c IN :actionPlanTempList];

System.debug('dependencyList - '+dependencyList); 
//returns: dependencyList - ()

Interestingly - actionPlanTempList is List of IDs. When I change it to List of String it works as expected.

Why?

Edit 1:

On ap_Action_Plan_Task_Dependency__c Object, the field Action_Plan_Template__c is a formula field and also on Task Object, the field Action_Plan_Template__c is a formula field.

actionPlanTempList is being generated list this.

for(Task t:Trigger.New) { 
    actionPlanTempList.add(t.Action_Plan_Template__c); 
}
  • 1
    What is Action_Plan_Template__c and what is its type? How are you building actionPlanTempList? – Mahmood Nov 5 '18 at 8:32
  • 1
    Possible duplicate of Bug in List.contains for Id data type? – Pranay Jaiswal Nov 5 '18 at 9:59
  • 1
    Its a known bug, changing the debug level to finest will make your code work. – Pranay Jaiswal Nov 5 '18 at 10:00
  • 1
    Hi @PranayJaiswal My understaning is - The bug I am facing is not about methods of List object and I am facing this in Logs. The bug is about the behaviour of the SELECT statement, it works differently if list passed is of IDs and if list is of Strings. – Ganesh Bhosle Nov 5 '18 at 10:22
  • 1
    Cool, lets see what community can answer. +1 for me – Pranay Jaiswal Nov 5 '18 at 10:23
2

Based on your comments:

On ap_Action_Plan_Task_Dependency__c Object, the field Action_Plan_Template__c is a formula field and also on Task Object, the field Action_Plan_Template__c is a formula field.

I would think that the data type of these formula fields are of type TEXT which signifies that these are not of type Id but String.

Here's my take - when you add the formula values to the List<Id>, the platform does not treat it as Ids based on the return type but String. However you are still able to add it in the list because the value is still a well constructed Id.

(There does seem to be an issue and possibly related to what @Pranay has mentioned, where adding a valid String value for Id into List<Id> does not work, I have tried to explain later with a quick finding)

This is the reason why your code works when you declare your actionPlanTempList as List<String>.

As a quick test, you can use the getSObjectType() method to verify that the value being added to the list is not actually an Id. So, where you are adding the formula field values in the iteration, if you put a debug statement as below:

for(Task t:Trigger.New) { 
    actionPlanTempList.add(t.Action_Plan_Template__c);    

    system.debug(t.Action_Plan_Template__c.getSobjectType());
}

You will get an error saying:

Method does not exist or incorrect signature: void getSObjectType() from the type String


UPDATE

Seems like casting or parsing the string value into an Id type just doesn't work in the IN clause of the query. I had earlier thought it worked but when verified again, it simply ignores anything added in List<Id>.

It looks like unless and until the field being returned from the query is not of type Id, List<Id> just doesn't work in IN clause.

So let's say if there's a query as below where mylookup__c is a lookup field (Id), creating the list of Id and using it in SOQL will just work fine.

List<myobj__c> listOfObjs = [select id, name, mylookup__c from myobj__c];
List<Id> mylookupList = new List<Id>();
for(myobj__c m : listOfObjs) {
    mylookupList.add(m.mylookup__c);
}

// ONLY NOW List<Id> WORKS HERE
List<myobj__c> newlistOfObjs = [select id, name from myobj__c where mylookup__c IN :mylookupList]

Its just not clear as how in run-time, the IN clause in SOQL works where there's a difference in behavior when String values are added in List<Id> vs. the same values added in List<String>.

  • Wouldnt assigning a text to Id datatype will throw an exception? – Pranay Jaiswal Nov 5 '18 at 14:30
  • Based on a quick test, could see it just worked fine, as long as it's a well constructed Id value, you can add it to a List<Id> – Jayant Das Nov 5 '18 at 14:33
  • Yeah,, That means Id are valid, so is List<Id> and thus query should return results. – Pranay Jaiswal Nov 5 '18 at 14:34
  • That's what is confusing as how the platform evaluates that at run-time. As it works on list of string but not on list of ids. – Jayant Das Nov 5 '18 at 14:35
  • This is my theory from the bug found few days back, When using IN clause hashcode and equals methods are called for List<ID> as those methods are broken it returns inconsistent results. – Pranay Jaiswal Nov 5 '18 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.