Damien TEMPLER
·PS at Memority

Prevent a company from not having any leader

Resolved

Hi,

I am implementing a new rule for my B2B context.

Basically, all companies should have AT LEAST one leader.

  • First Leader is the one who self-registered the company and got approved

  • Other leaders can be appointed by the first leader at any time

  • "Leader" status is represented by a boolean "isCompanyLeader"

HOWEVER, NO company should be without a leader. It means that if I try to uncheck "isCompanyLeader" for the last remaining user, I should get an error.

I applied the following validationRule, but it does 2 unexpected things:

  • Wrongly allows me to remove the last Leader

  • Wrongly prevents me from adding Leaders to a company with no leader (as a result of step 1)

<validationRule>
    <script><![CDATA[
        def Companies = OBJECT.companies[0]
        def existingLeader = FIND.identity().withAttributesProjection("id","companies","isCompanyLeader").matching(expr { prop("companies").eqLike(Companies as String) & prop("isCompanyLeader").eq(true)})

        return existingLeader ? ValidationRuleResult.valid() : ValidationRuleResult.invalid("Company has no leader","ui.errors.user.email.generic.noLeader.msg")
    ]]></script>
</validationRule>

Can anyone help?

Data model
1 reply