3

Is there a way that I can create some type of Map between 3 objects in a trigger?

I need to map (sObject, Date, Decimal)

I know this may not be possible in a single map, but is there a workaround that would allow me to do this? Essentially the map key would be two values, and it would return the Decimal value

Let me know if you need clarification Thank you

2 Answers 2

4

You could create a custom hash of the SOject Id and the date field. To ensure consistency, you probably want to break this out into a separate function.

private String getMapHash(Id objId, Date d){
   return SObject.Id + myDate.format();
}

Map<String, Decimal> myMap = new Map<String, Decimal>();
myMap.put(getMapHash(SObject.Id, myDate), myDecimal);

Decimal d = myMap.get(getMapHash(SObject.Id, myDate));

Another option would be to create a Map<Id,Map<String,Decimal>>. But this is more work and likely much slower.

Notice I used String for the key for the date indexed Map. I'm not sure what would happen if you used the Date object itself. I suspect SF would hash two different Date Objects with the same value differently.

4
  • Thank you for your reply! I will give this a try. Is it possible to create/call a method from within a trigger? I am fairly new to this. Will I need to create an additional apex class to call the method? Aug 27, 2015 at 17:33
  • @AlexBrigham yes, if you want to factor the hashing out to a function (which is a very good idea) you will have to create a new class. Its generally best practice to create a "Trigger Helper" class for each trigger anyways. This helps to organizes code and improve maintainability. Here is an SE Question that might be of some help: salesforce.stackexchange.com/questions/42668/…
    – NSjonas
    Aug 27, 2015 at 17:41
  • Wow thank you for you help! I will try this when I get back to development later in my workday. I will let you know if it is my solution. :) Thanks again! Aug 27, 2015 at 17:48
  • @AlexBrigham No problem. Good luck on your trigger
    – NSjonas
    Aug 27, 2015 at 17:53
2

You can create your own custom type and use that as the Map key providing you implement equals and hashCode:

public class Key {
    public Id id;
    public Date d;
    private Integer hashCode;
    public Key(Id id, Date d) {
        this.id = id;
        this.d = d;
    }
    public Boolean equals(Object o) {
        if (o instanceof Key) {
            Key that = (Key) o;
            return this.id == that.id && this.d == that.d;
        }
        return false;
    }
    public Integer hashCode() {
        if (hashCode == null) {
            // System.hashCode buggy when used directly on ID values
            hashCode = System.hashCode(d) + 31 * System.hashCode(String.valueOf(id));
        }
        return hashCode;
    }
}

This then allows you to write code where it is clear what the Map key is:

Key k = new Key(mySob.Id, myDate);
Map<Key, Decimal> m = new Map<Key, Decimal>();
m.put(k, myDecimal);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .