Create Scope object from a Rule

Which IIQ version are you inquiring about?

8.3

Please share any images or screenshots, if relevant.

Share all details about your problem, including any error messages you may have received.

Is there a way to programmatically create Scope objects from a rule?
I have created few from Global settings. Now my requirement is to create new scope from a rule.
Is that possible? If so, please share sample code

You can use a simple rule to execute it and create scopes


import sailpoint.object.Scope;

Scope newScope = new Scope();
newScope.setName("MyNewScope");
newScope.setDisplayName("MyScope DisplayName");
newScope.setManuallyCreated(true);

// If you want to set it in the hierarchy
newScope.setPath("SCOPE1/SCOPE2/SCOPE3");

Scope childScope = context.getObject(Scope.class,"ChildScope");

// If you want to make new scope a parent to already existing ones
newScope.setChildScopes(childScope);

context.saveObject(newScope);
context.commitTransaction();

@kjakubiak Thanks for your response, scope got created.
Although, the hierarchy doesn’t seem to be working. Below is what I tried

  Scope scope = new Scope();
  scope.setName("testScope");
  scope.setPath("All/testScope");
  scope.setManuallyCreated(true);
  
  context.saveObject(scope);
  context.commitTransaction();

It created testScope outside of parent ‘All’ (PFB screenshot)
image

I want testScope to be created inside ‘All’ scope

Tried below code as well @kjakubiak

  Scope allScope = context.getObjectByName(Scope.class,"All");
  String allScopePath = allScope.getPath();
  
  Scope newScope = new Scope();
  newScope.setName("testScope");

  context.saveObject(newScope);

  newScope.setPath(allScopePath + ":" + newScope.getId());
  newScope.setManuallyCreated(true);
  newScope.setAssignedScopePath(allScopePath + ":" + newScope.getId());
  
  context.saveObject(newScope);
  context.commitTransaction();

Still the hierarchy doesn’t seem to work

The object from debug page looks correct, but on UI it doesn’t seem to be working


image

I think you have to use

List childScopes = allScope.getChildScopes();
childScopes.add(scope);
allScope.setChildScopes(childScopes);

and save allScope object.

@kjakubiak Hi, thanks for your response
Tried your suggestion, but it gave me below error:
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [sailpoint.object.Scope#c0a81dec91b61df18191b832572402d1] BSF info: Run Rule 1 - Create Scope at line: 0 column: columnNo

The code I tried is

  Scope allScope = context.getObjectByName(Scope.class,"All");
  List childScopes = allScope.getChildScopes();
  String allScopePath = allScope.getPath();

  Scope newScope = new Scope();
  newScope.setName("testScope");
  newScope.setManuallyCreated(true);

  context.saveObject(newScope); // to generate id of newScope

  newScope.setPath(allScopePath + ":" + newScope.getId());
  newScope.setAssignedScopePath(allScopePath + ":" + newScope.getId());
  
  childScopes.add(scope);
  allScope.setChildScopes(childScopes);

  context.saveObject(newScope);
  context.commitTransaction();

Works for me quite good - you have typo in your code
childScopes.add(scope); it should be childScopes.add(newScope);

I just created two testScope scopes in the Berlin scope with this code after fixing the typo.
image

@kjakubiak thanks, that worked!