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.
====================================
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:28Map
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