2

Is there a method of discovering (or a place to look up) all of the interfaces that an out of the box Salesforce class (Like LoginHistory) implements?

The reason I ask: I'm attempting to test a method which interacts with a LoginHistory object that's passed in as a parameter. The method accesses the UserId and LoginTime fields on the object. Salesforce won't allow me to new up this object and assign values to these properties for my test. If the object's class implements an interface, I think I could create a mock object from it use it instead for this testing.

Is there a better solution to my problem?

9

sObject classes don't implement interfaces at all. Unfortunately, that won't be a viable solution here.

One very common solution to this issue (needing to create an object and populate non-writeable fields) is JSON deserialization. It's viable here; an example:

LoginHistory l;

l = (LoginHistory)JSON.deserialize(
    '{"UserId": "'+UserInfo.getUserId()+'", "LoginTime": ' + JSON.serialize(DateTime.now()) + '}',
    LoginHistory.class
);

System.debug(l);

This works for a wide variety of objects you cannot otherwise construct or populate, although it might require some experimentation to get it just right. The pattern is the same: you construct some JSON string representing the object's properties and deserialize it, giving the class of the desired object.

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.