Code snippets to use in fields configuration

Related Articles

Introduction

The advanced option Configure fields... allows making almost all fields either mandatory or read only.

By using this option, you take control of elements that could impact Octopus normal behaviour. It is very important to use this option with care and and test your settings in an environment that is not your main one.

This artice contains code examples that you can use in the code editor to configure required fields.

Code Excerpts

Code excerpts that can be used in fields configuration. Most of the examples are interchangeable between REQUIRED or READONLY. Please take note that these examples are isolated and that it is possible to combine several conditions.

Services available in the code

  • injector :the object by which we can have all services.
  • entity : the data being saved.
  • OnlyIf() : feature that reverts the decision to the default one if the condition is false.
  • You can put var util = injector.GetInstance() to access these features
    • util.CurrentUser : current user.

    • util.Break() : allows a developper to debug a rule.

ReadOnly if the current user does not have the role X

Example for CI - Costs Center :

var util = injector.GetInstance<ESI.Octopus.Core.ScriptUtility>(); 

var role = "nom du rôle"; 
var isInRole = util.CurrentUser.UserTeamInfoForCurrentTeam.Roles.Any(g => g.Role.NameFR == role); 
return OnlyIf(!isInRole);

Readonly if the user does not have the permission to administer Octopus

Example (version 4.1)

var util = injector.GetInstance<ESI.Octopus.Core.ScriptUtility>(); 

var hasPermission = util.CurrentUser.HasPermission(ESI.Octopus.Constants.Enums.Permission.CanAdministerHelpDesk); 
return OnlyIf(!hasPermission);

Example (version 4.2)

var teamID = 1; 
var util = injector.GetInstance<ESI.Octopus.Core.ScriptUtility>(); 

var hasPermission = util.CurrentUser.HasPermission(teamID, ESI.Octopus.Constants.Enums.Permission.CanAdministerHelpDesk); 
return OnlyIf(!hasPermission);

Readonly if the field already has a value

We can only assign a value once.

Example on Incident - Description :

var original = entity.GetOriginalValue("FullDescription"); 
return OnlyIf(original.IsNotNull());

Required if the field already has a value

Once the data entered, it is impossible to delete it, it can be changed but it can't be left blank. 

Example on Incident - Description:

var original = entity.GetOriginalValue("FullDescription"); 
return OnlyIf(original.IsNotNull());

Required if the CI state is In Operation

Example on CI - Inventory number :

var inService = entity.StatusID == (int)ESI.Octopus.Constants.Enums.ConfigurationItemStatus.InService; 
return OnlyIf(inService);

Field required of the current user has a Batch license

Allows leaving an open door for the batch programs.

Example sur CI - Inventory number :

var util = injector.GetInstance<ESI.Octopus.Core.ScriptUtility>(); 

var isBatch = util.CurrentUser.LicenseType.NameFR == "Traitement en lot"; 
return OnlyIf(!isBatch);

Normaly, this is combined with another rule. For example, Inventory number required if CI is in operation.

Example on CI - Inventory number :

var util = injector.GetInstance<ESI.Octopus.Core.ScriptUtility>(); 

var isBatch = util.CurrentUser.LicenseType.NameFR == "Traitement en lot"; 
var inService = entity.StatusID == (int)ESI.Octopus.Constants.Enums.ConfigurationItemStatus.InService; 

return OnlyIf(!isBatch && inService);

Assignee required if the group is X

If we have a group for which we always want an assignee since it has no dispatch system in place.

Example on Incident - Assignee:

var group = "Spécialistes Applicatifs"; 
return OnlyIf(entity.IsOpened && entity.Group.DescrFr == group);

Make a field obligatory if another is filled

If Purchase cost is not empty, then Cost center is not required.

Example on CI - Cost Center :

return OnlyIf(entity.PurchasePrice.IsNotNull());

Make a fied obligatory  for a specific CI type

Category is mandatory for Server CI type.

Example on CI - Category : 

return OnlyIf(entity.Type.DescrFR == "Serveur");

Make a field obligatory if another field is empty

It needs at least Serial number or Inventory number. Ceci se fait en mettant deux règles, une sur chaque champ.

Example on CI - Inventory number

return OnlyIf(entity.SerialNumber.IsNull());


Example on CI - Serial number

return OnlyIf(entity.InventoryNumber.IsNull());

Make a status in ReadOnly if the user is not part of the CI responsible group

...unless the CI does not have a responsible group.

Example on CI - Status

var util = injector.GetInstance<ESI.Octopus.Core.ScriptUtility>(); 

var canWrite = entity.MaintenanceGroup.IsNull() || entity.MaintenanceGroup.HasMember(util.CurrentUser.ID); 
return OnlyIf(!canWrite);

Make a Status in ReadOnly if the user is not part of the Service Desk group

var serviceDeskGroupID = 1; 
var groupService = injector.GetInstance<ESI.Octopus.Entities.Service.Interface.IGroupService>(); 

var group = groupService.GetEntity(serviceDeskGroupID); 

var util = injector.GetInstance<ESI.Octopus.Core.ScriptUtility>(); 
var canWrite = group.HasMember(util.CurrentUser.ID); 
return OnlyIf(!canWrite);
X
Help us improve our articles