Expression rule to compare two values

I want to write an expression rule to compare two values: 

first value is from rule input and second value from a db table field.If the values are same ,then another field(column) of my db table must be set to inactive or 0.

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    FYI, you're able to edit your post if you want to change something, it's not really needed to post essentially the same thing again.

    For your question, you should be able to query from your database table (using a!queryEntity() or, preferably, referencing an existing expression rule you've already set up to handle queries to that table), and compare the result to the input value.  I'm not exactly clear what you mean when you say "another field of my table must be set to inactive", as an expression rule itself cannot change data in a DB table - what you describe will probably be most easily accomplished by using a process model which sets the desired value determined by some initial logic then writes that row back to your table.

  • To do the comparison, the value should be retrieved from the database. Use query entity to get the value from backend, compare it and set the value (write into database). 

  • Hi Mike

    Thanks for your response,by the line "another field of my table must be set to inactive",what I mean is I have a column name "is active" in my db Table. I also have columns "fieldid" and "formid" in my DB table.So what I exactly want to do is get rule inputs for formid and fieldid.If the rule input values of these 2 fields matches(same value) with the value i have in the db table for "fieldid" and "formid" column, then my "isactive" column must be set to 0 or disabled. 

  • 0
    Certified Lead Developer
    in reply to s0001

    You probably need to look into doing this in a process model; a single expression rule can't really do all the different things you're describing here inherently.

  • load(
    local!dbdata:{
    {id:1,isactive:1,fieldId:1,formId:2},
    {id:1,isactive:1,fieldId:1,formId:3},
    },

    a!forEach(
    items: local!dbdata,
    expression: if(
    and(
    tointeger(fv!item.fieldId) = ri!fieldId,
    tointeger(fv!item.formId) = ri!formId
    ),
    {id:fv!item.id,isactive:0,fieldId:fv!item.fieldId,formId:fv!item.formId},
    fv!item
    )
    )
    )

    I specified dbdata as dictionary in above code. cast it to CDT using type! and write the output to database entity.