Is field level validation only supposed to work on update an

Posted by jsniemi79 on 24-Apr-2015 15:42

I'm trying to use field level validation to make sure only a single record can be flagged as primary.  When I create a new record and flag it as primary, even though I already have one set, it allows the record to be saved.  However, if I go to update an existing record and set it as primary, it will not let me save because it sees the other record.  I've included my code below in case there is something in there that is incorrect.

My Validation Code

if ({!d_chk_primary} == true) {
var myArr = rbv_api.selectQuery("SELECT id, name FROM prospect_contacts WHERE RProspect_Contact_Prospect =?", 10000, {!RProspect_Contact_Prospect});

if (myArr.length >0) {
return myArr[0][1]+" is already flagged as primary. Please uncheck the primary on "+myArr[0][1]+" and then this record can be updated.";
}
}

Posted by pvorobie on 24-Apr-2015 16:20

Field-level validation script will run on New Record page, but conditions are different: when script is running, record not created yet and hence not attached to any other record. You need to take this into account.

All Replies

Posted by Godfrey Sorita on 24-Apr-2015 16:04

Hi Jason,

If the field with validation is on the form, the validation should fire even on the NEW page. The best way to debug field-level validation is to return the tokens or variables on the first line. Doing this will allow you to check the values of tokens on actual transactions.


Regards,
Godfrey

Posted by pvorobie on 24-Apr-2015 16:20

Field-level validation script will run on New Record page, but conditions are different: when script is running, record not created yet and hence not attached to any other record. You need to take this into account.

Posted by jsniemi79 on 27-Apr-2015 09:34

What would be the best way to handle the combination of server side field validation with the client side browser information?  I ended up solving this another way, but I'm interested for future reference.

Posted by Godfrey Sorita on 28-Apr-2015 08:31

Can you elaborate what you're trying to achieve? What browser information are you trying to use for server-side validation?

When validating with related record's field values, you can get the record ID then use Query APIs to get its field values.

Posted by jsniemi79 on 28-Apr-2015 08:51

Sure.  I have field level validation setup to verify uniqueness of a checkbox field.  For a particular related record, only one of these records can have this checkbox set.  My validation per the code above works perfectly on an update to a record, but it isn't working for create.  Based on the response above, it seems that is because my current record isn't created yet and therefore, it doesn't have the Id of the relationship yet, so my formula isn't picking it up.  I assumed that meant I needed to grab the ID based on the value selected in the browser, but I don't know how I could use that with my field level validation.  Make sense?

Posted by Godfrey Sorita on 28-Apr-2015 10:27

On the first line of your validation code, add the following code:

return "{!RProspect_Contact_Prospect}";

Then, create a new record. This should return a validation error displaying the form value of the lookup field. This way, you'll be able to compare the difference in new and edit pages.

This thread is closed