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

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

1. Condition Builder

a. Choose Condition Builder while creating the Transaction Security Policy.

b. Set the condition as Country Does not equal to India.

c. Use Action as Block to prevent the logins.

2. Apex:

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.

Transaction Security feature can be used to prevent users login access based on country in Salesforce as per the above example.

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.

Following Exception will be thrown if the user tries to Login or Reset the password.

A security policy is preventing you from logging in to this org. For more information about security policies, contact your administrator.

Leave a Reply