a!localVariables( local!insert:false, local!update:false, a!formLayout( label: "Username", contents: { a!textField( label: "username", labelPosition: if( ri!readOnly, "ADJACENT", "ABOVE" ), value: ri!username.username, saveInto: ri!username.username, refreshAfter: "UNFOCUS", validations: { if( a!queryEntity( entity: cons!usernameConstant, query: a!query( fetchTotalCount: true, pagingInfo: a!pagingInfo( startIndex: 1, batchSize: 1 ), logicalexpression: a!queryLogicalExpression( operator:"OR", filters: { a!queryFilter( field: "username", operator: "=", value:(ri!username.username), applyWhen:not(isnull(ri!username.username)) ), } ) ) ).totalCount > 0, local!insert:true, local!update:true ), } ), a!textField( label: "gender", labelPosition: if( ri!readOnly, "ADJACENT", "ABOVE" ), value: ri!username.gender, saveInto: ri!username.gender, refreshAfter: "UNFOCUS", validations: {} ), a!columnsLayout( columns: { a!columnLayout( contents: { a!textField( label: "Password", labelPosition: if( ri!readOnly, "ADJACENT", "ABOVE" ), value: ri!username.password, saveInto: ri!username.password, characterLimit: 255, readOnly: ri!readOnly ) } ) } ), }, buttons: a!buttonLayout( primaryButtons: { a!buttonWidget( label: "Insert", saveInto:( a!writeToDataStoreEntity( dataStoreEntity: cons!usernameConstant, valueToStore:(ri!username) ), ), disabled:local!insert, submit: true, style: "PRIMARY", ), a!buttonWidget( label: "update", saveInto:( a!writeToDataStoreEntity( dataStoreEntity: cons!usernameConstant, valueToStore:ri!username, ), ), disabled:local!update, submit: true, style: "PRIMARY" ), a!buttonWidget( label: "delete", saveInto:( a!deleteFromDataStoreEntities( dataToDelete: { a!entityDataIdentifiers( entity: cons!usernameConstant, identifiers:{ri!username.username}, ) } ) ), submit: true, style: "PRIMARY" ) }, secondaryButtons: { a!buttonWidget( label: "Cancel", value: true, saveInto: ri!cancel, submit: true, style: "NORMAL", validate: false ) }, showWhen: or( isnull( ri!readOnly ), not( ri!readOnly ) ) ) ) )
For line 17 validation part nearby, i wish the button insert will not work if it find the duplicate username already in database, and disable update button for the username can't be found, that is what i put in the validation part of if condition sentences, however, it report error with "Expression evaluation error at function a!textField", then how can I do?
Discussion posts and replies are publicly visible
totalcount always equal -1,i think this may be the issue
fetchTotalCount ... do you remember?
fetchTotalCount always underscore with yellow line
invalid parameter
Update queryEntity() to have fetchtotal count to true() such as
a!queryEntity( entity:cons!usernameConstant, fetchTotalCount:true(), query:a!query( filter:a!queryFilter( field:"username",operator:"=", value:ri!username.username ), pagingInfo:a!pagingInfo( startIndex: 1, batchSize: 50 )))
A yellow line ?!?!?
a!queryEntity has that parameter. Set it to true.
a!localVariables( local!valid, a!formLayout( label: "Username", contents: { a!textField( label: "username", labelPosition: if( ri!readOnly, "ADJACENT", "ABOVE" ), value: ri!username.username, saveInto: {ri!username.username, a!save(local!valid, if(isnull(ri!userName.userName),{}, if(a!queryEntity( entity:cons!usernameConstant, fetchTotalCount:true, query:a!query( filter:a!queryFilter( field:"username",operator:"=", value:ri!username.username ), pagingInfo:a!pagingInfo( startIndex: 1, batchSize: 50 ), )).totalCount>0,true(),false()))),}, refreshAfter: "UNFOCUS", ), a!textField( label: "gender", labelPosition: if( ri!readOnly, "ADJACENT", "ABOVE" ), value: ri!username.gender, saveInto: ri!username.gender, refreshAfter: "UNFOCUS", validations: {} ), a!columnsLayout( columns: { a!columnLayout( contents: { a!textField( label: "Password", labelPosition: if( ri!readOnly, "ADJACENT", "ABOVE" ), value: ri!username.password, saveInto: ri!username.password, characterLimit: 255, readOnly: ri!readOnly ) } ) } ), }, buttons: a!buttonLayout( primaryButtons: { a!buttonWidget( label: "Insert", disabled: local!valid=true(), saveInto:( a!writeToDataStoreEntity( dataStoreEntity: cons!usernameConstant, valueToStore:(ri!username) ), ), submit: true, style: "PRIMARY", ), a!buttonWidget( label: "update", disabled: local!valid=false(), saveInto:( a!writeToDataStoreEntity( dataStoreEntity: cons!usernameConstant, valueToStore:ri!username, ), ), submit: true, style: "PRIMARY" ), a!buttonWidget( label: "delete", saveInto:( a!deleteFromDataStoreEntities( dataToDelete: { a!entityDataIdentifiers( entity: cons!usernameConstant, identifiers:{ri!username.username}, ) } ) ), submit: true, style: "PRIMARY" ) }, secondaryButtons: { a!buttonWidget( label: "Cancel", value: true, saveInto: ri!cancel, submit: true, style: "NORMAL", validate: false ) }, showWhen: or( isnull( ri!readOnly ), not( ri!readOnly ) ) ) ) )
still not work, buffer memory slow?
It works! Thanks for so much for your help! can be more appreciated
Thanks so much sir, can be more grateful!
This is a lot of hardcoding of values. Using local variables there are usually much more graceful ways to do this, namely using a technique that I named "variable chaining", i.e. setting up a few different local variables whose values will automatically adjust based on the values of previous ones in an informative way. The advantage here is you only need to have your text field save into the "username" variable and not have a query hardcoded into its saveInto. This should be easier to troubleshoot and easier to tweak in the future. This is my own version of your above form but I believe it captures your general use case:
a!localVariables( local!name, local!usernameTaken: if( isnull(local!name), false(), rule!yourQueryPersonRule( fetchTotalCount: true(), pagingInfo: a!pagingInfo(1, 0), queryFilters: { a!queryFilter( field: "username", operator: "=", value: local!name ) } ).totalCount > 0 ), local!disableButton: or( local!usernameTaken ), a!formLayout( contents: { a!textField( label: "Username", value: local!name, saveInto: { local!name }, required: true(), validations: { if( local!usernameTaken, "Username Exists", null() ) } ) }, buttons: a!buttonLayout( primaryButtons: { a!buttonWidget( label: "SUBMIT", value: "submit", submit: true(), style: "PRIMARY", saveInto: { /* etc */ }, disabled: local!disableButton ) }, secondaryButtons: { a!buttonWidget( label: "Cancel", submit: true(), validate: false() ) } ) ) )