How to restrict users login access based on country in Salesforce?

Transaction Security feature can be used to restrict users login access based on country in Salesforce.

To know about Transaction Security and setting up use the below link

http://www.infallibletechie.com/2016/11/transaction-security-in-salesforce.html

Sample Apex Code:

global class IndiaOnlySecurityPolicyCondition implements TxnSecurity.PolicyCondition {

    public boolean evaluate(TxnSecurity.Event e) {
        LoginHistory eObj = [SELECT CountryISO FROM LoginHistory WHERE Id = :e.data.get('LoginHistoryId')];
        if ( eObj.CountryISO == 'IN' ) {
            return false;
        }        
        return true; 
    }

}

The above code will allow only users to log in from Country India.

Note:
Please do not do this in Production. It may not allow you to log in if set incorrectly. Try to bypass system administrator profile to avoid issues.

No comments:

Post a Comment