2

I need to modify the existing transaction security policy to trigger if user logs in from multiple IP addresses in past 8 hours. I'm using the below SFDC example policy. Can someone give a hand on how to tweak the below code to trigger for 8 hours. Currently the policy is configured to trigger for past 24 hours.

global class LoginPolicyCondition implements TxnSecurity.PolicyCondition {
 public boolean evaluate(TxnSecurity.Event e) {
   AggregateResult[] results = [SELECT SourceIp
                                FROM LoginHistory
                                WHERE UserId = :e.userId
                                      AND LoginTime = LAST_N_DAYS:1
                                GROUP BY SourceIp];
   if(!results.isEmpty() && results.size() > 1) {
      return true;
    }
    return false;
  }
}  
  • This is mostly identical to this question but with more context. Please edit your questions to expand on them rather than creating duplication. – David Reed Nov 7 '18 at 0:34
  • David - I deleted the identical question since it was duplicate as you pointed. Thanks – VBALearner Nov 7 '18 at 0:38
2

There is no SOQL Date Literal equivalent to to LAST_N_DAYS:1 that will give you a period of 8 hours.

Instead, you can just calculate the date range and use that in the SOQL query instead.

DateTime eightHoursAgo = DateTime.now().addHours(-8);
AggregateResult[] results = [SELECT SourceIp
                            FROM LoginHistory
                            WHERE UserId = :e.userId
                                  AND LoginTime >= :eightHoursAgo
                            GROUP BY SourceIp];
return (!results.isEmpty() && results.size() > 1);
  • That makes perfect sense. Appreciate your help on this. – VBALearner Nov 7 '18 at 0:33

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.