0

I want to ask the question that why the value in the map becomes null.

List<Order> insertOrderlist = new List<Order>();
Map<Order,Decimal> OrderPaymentMap = new Map<Order,Decimal>();
For(ObjectName config: [SOQL query])
{
      String PaymentTerm = config.value;
      Order o =  new Order(fields = values);
      insertOrderlist.add(o);
      if(!OrderPaymentMap.containsKey(o))
          OrderPaymentMap.put(o,Decimal.ValueOf(config.attribute__c));
}
For(Order o1: insertOrderlist)
{
      System.Debug('#####OrderPaymentMap.get(o1): '+OrderPaymentMap.get(o1));   //Debug point 1, the map value is config.attribute__c
}

Test.startTest();
System.RunAs(user){
      Insert insertOrderlist;
}
Test.stopTest();

For(Order o2: insertOrderlist)
{
      System.Debug('#####OrderPaymentMap.get(o2): '+OrderPaymentMap.get(o2));  //Debug point 2, the map value becomes null
}

I have the testcase code in a new dev org, it's wired that the map value is changed to null after inserting the Order value. But I have the same code in another sandbox environment, and I don't have this issue.

====================================

Add a screenshot to show the result in another org: Please find the expected and actual marked in red. The expected shows the OrderPaymentMap value

7
  • But if the reason is the sObject is changed, why the code works in another sandbox? Most of people say "Sometimes", what is it? Can I understand it's salesforce's bug? – user82052 Apr 15 '20 at 2:16
  • As the answer states, the best and most reliable practice is to use the object's ID rather than the object itself. – David Cheng Apr 15 '20 at 2:38
  • But we can't get object's ID before the insert. And I really want to know why there's different result between the dev org and sandbox. – user82052 Apr 15 '20 at 2:40
  • The only way I can see this code running in some orgs but not others is if you're not actually inserting any records. This looks like a test class, so keep in mind that you don't have access to most of the data in the org. If that ObjectName config : [query] bit is grabbing data from a custom metadata type (something that is available in unit tests without creating it as part of the test), and you have a record in one org but not another, that could explain why this test is passing in one org but not another. – Derek F Apr 15 '20 at 13:28
  • That said, the main question "why does the map value become null?" is answered by the question David Cheng pointed to. Using SObject instances as a map key is something that takes a great amount of care and understanding to implement. You need to know about how hashing works, how the Map collection type works, the difference between a reference and a value, how to get a "by value" copy of an SObject (or otherwise decouple instances), and probably a few more things I'm forgetting. There are likely better and easier ways to accomplish whatever it is you're trying to do. – Derek F Apr 15 '20 at 13:39

Browse other questions tagged or ask your own question.