Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.