How can I prefill/preset attributes values in a create feature ?

Resolved

I need to preset the values of attributes activationUntil and activationFrom to today and today+356 days respectively. I don't want to use an init rule on the attributes themselves, since it may vary for each type. I want the values to be preset only for the creation feature of a given type.

Data model
1
1 reply
Best reply by Inactive Member

https://doc.memority.dev/configuration-guide/latest/features

You can see that initRules also work in features in the options tag.

Using the following code in the create feature of your type will prefill the values of attributes activationFrom and activationUntil.

    <options>
        <completionRedirectURL>feature://identity-internal-hub.identity-internal-read/{routeObjectId}</completionRedirectURL>
        <formLabelOnTop>false</formLabelOnTop>
        <initialization>
            <initRule>
                <script><![CDATA[
                    import com.memority.citadel.shared.api.im.ApiObject
                    import com.memority.citadel.shared.api.services.manager.ObjectManager
                    import java.time.Instant
                    import java.time.ZoneId
                    import java.time.temporal.ChronoUnit

                    // Get settings
                    def zoneIdSettings = SETTINGS.get("bum.tenant.timeZone") as String
                    def zoneId = ZoneId.of(zoneIdSettings) as ZoneId

                    // Set attributes
                    def todayDate = Instant.now().atZone(zoneId).truncatedTo(ChronoUnit.DAYS)
                    def endDate = Instant.now().atZone(zoneId).plusDays(365)
                    final def attributes = []
                    attributes.add(AttributeValue.from("activationFrom", todayDate))
                    attributes.add(AttributeValue.from("activationUntil", endDate))
                    new ApiObject(attributes)
                    ]]></script>
            </initRule>
        </initialization>
        <pendingOperationBehaviour>WARN</pendingOperationBehaviour>
        <submit>
            <reportDisplay>DISPLAY_ALL</reportDisplay>
        </submit>
    </options>
View original