Skip to content

Instantly share code, notes, and snippets.

@afawcett
Created June 7, 2016 16:11
Show Gist options
  • Save afawcett/2470b738c266c667523d3d3568d4e3cb to your computer and use it in GitHub Desktop.
Save afawcett/2470b738c266c667523d3d3568d4e3cb to your computer and use it in GitHub Desktop.
PermissionSet permissionSet = new PermissionSet();
permissionSet.Name='ApexTest';
permissionSet.label='Apex Test';
insert permissionSet;
ObjectPermissions objectPermission = new ObjectPermissions();
objectPermission.ParentId = permissionSet.Id;
objectPermission.SObjectType = Book_Order__c.SObjectType.getDescribe().getName();
objectPermission.PermissionsEdit = true;
objectPermission.PermissionsRead = true;
objectPermission.PermissionsDelete = true;
objectPermission.PermissionsCreate = true;
insert objectPermission;
ObjectPermissions objectPermission2 = new ObjectPermissions();
objectPermission2.ParentId = permissionSet.Id;
objectPermission2.SObjectType = Order_Line_Item__c.SObjectType.getDescribe().getName();
objectPermission2.PermissionsEdit = true;
objectPermission2.PermissionsRead = true;
objectPermission2.PermissionsDelete = true;
objectPermission2.PermissionsCreate = true;
insert objectPermission2;
FieldPermissions fieldPermission = new FieldPermissions();
fieldPermission.ParentId = permissionSet.Id;
fieldPermission.SObjectType = Order_Line_Item__c.SObjectType.getDescribe().getName();
fieldPermission.Field = Order_Line_Item__c.SObjectType.getDescribe().getName() + '.' + Order_Line_Item__c.Shipping_Status__c.getDescribe().getName();
fieldPermission.PermissionsEdit = allowUpdateShippingStatus ? true : false;
fieldPermission.PermissionsRead = true;
insert fieldPermission;
User newUser = new User(
Alias = 'test1'+System.today().dayOfYear(),
Email=permissionSet.Id+'@testorg.com',
EmailEncodingKey='UTF-8',
LastName='Testing',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
ProfileId = [select Id from Profile where Name = 'Standard User'].Id,
TimeZoneSidKey='America/Los_Angeles',
UserName=permissionSet.Id+'@testorg.com');
insert newUser;
insert new PermissionSetAssignment(AssigneeId=newUser.Id, PermissionSetId=permissionSet.Id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment