+ Start a Discussion
randheer practiserandheer practise 

i have a basic insert class and two users user1 and user2..but for me only user2 can access the class and insert the class,if user1 executes it should not insert the record or he should not access the class

i have written a class,but both users are executing the class,but only user2 should insert the record what mistake is there in my class can annyone tell me.

public class inserting {
    public  void insertingrecords(string nm,decimal dd){
        Account a = new Account();
        insert a;

testclass using system.runas():
private class TestRunAs {
   public static testMethod void testRunAs() {
      // Setup test data
      // This code runs as the system user
      Profile p = [SELECT Id,name FROM Profile WHERE Name='System Administrator']; 
               system.debug('values in p==='+'p.Name is '+p.name+'p.id is '+p.Id);
      /*User u = new User(Alias ='rprac', Email='sharath.kanukuntla1@gmail.com', 
      EmailEncodingKey='UTF-8', LastName='practise1', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='randheer.practise1@gmail.com');
      system.debug('user details are====== '+'email is '+u.Email+'Alias is '+u.Alias+'Profile id is '+u.ProfileId);
       System.runAs(u) {
         // The following code runs as user 'u' 
         System.debug('Current User: ' + UserInfo.getUserName());
         System.debug('Current Profile: ' + UserInfo.getProfileId()); 
       User u3 = [SELECT Id FROM User WHERE UserName='randheer.practise1@gmail.com'];
         System.runAs(u3) {
             inserting obj = new inserting();
             System.debug('Current User: ' + UserInfo.getUserName());
             System.debug('Current Profile: ' + UserInfo.getProfileId()); 
Jason FlippenJason Flippen

Assuming you have the proper security in place from a configuration standpoint, you can enforce security on an Apex class in Salesforce by using the with sharing keywords.  Here's a link to an article that might help you:  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

I hope this helps.