If and else in form

I am trying to use an “if, else” condition in the Onload event of my form. My goal is to compare 2 fields content to a third one and save the result in a fourth one (result). I am new in Scriptace and can’t find the right syntax (of course if I can use those if and else statement there…)

Thank’s for helping!

Re: If and else in form

To access a field:
{fieldname}

If, you can use:

({field} == 'value') ? {field2} : {field3};

or

if ({fieldname} == 'value') {
{field2}
} else {
{field}
}

for concat of fields:

{field1}.{field2}

Regards,
Scott.

Re: If and else in form

Good morning Scott,

Thank’s for your reply!

There is only one thing I don’t see here; where is the link to my result field( fourth one…)? Is it because I have to insert that code in an Ajax event of Scriptcase?

Thank’s again!

Re: If and else in form

where is the link to my result field( fourth one…)? Is it because I have to insert that code in an Ajax event of Scriptcase?

This depends on how you have your form setup. If the form contains field4 (blank or hidden), then you can simply use {field4} = (field2}.{field3}

You can also use an event onAfterUpdate/Insert to call a SQL to update the field using:


$sql = 'UPDATE ... '.{field4};
sc_exec_sql($sql);

Regards,
Scott.

Re: If and else in form

Ouf! from if to sql… I am lost…

Here is an actual PDF vue of my master-detail form with comments…,

http://www.cjeouestile.qc.ca/masterdetail.pdf

Re: If and else in form

In looking at your PDF, I would guess that Montant field does NOT exist (displayed/hidden) on the edit form and you want this Montant value updated in the table (detail grid) after save with the value of either PrixResident / PrixNonResident based on the value of Ville(city) that is brought into the form?

Master form:
if montant fielddoes not exist on master form, then create a var using

$calc_montant or global: [calc_montant]

; call it want you want …

{montant}/$calc_montant = ({ville} == 'city_resident') ? {PrixResident} : {PrixNonResident};

OnAfterUpdate: (master form)


$sql = 'UPDATE services SET montant =' .{montant}.' WHERE servicesID = '.{servicesID};
or
$sql = 'UPDATE services SET montant =' .$calc_montant.' WHERE servicesID = '.{servicesID};
sc_exec_sql($sql);

This will update the child table with the value decided on in the master form. You have to use SQL to update a child table.

If you also need to populate the form with the value based on the IF statement, then onLoad:

{montant} = ({ville} == 'city_resident') ? {PrixResident} : {PrixNonResident};

Then use the above to SQL option to update the table of the displayed value (montant) … where the calculation is created on form load instead of calculating it at the time of save.

If I missed the ball, please explain it to me in more detail.

Regards,
Scott.

Re: If and else in form

It has been a while, but you may have to use a global [var] instead of $var, if the value does not exist as a field on the master form due to scope.

{field} is global to the app, [var] is global
$var is local to the function … so it if defined in onLoad, it will not be seen in onAfterSave to update your table.

Regards,
Scott.

Re: If and else in form

When you say;

I would guess that Montant field does NOT exist (displayed/hidden) on the edit form…

It is there but empty… ok? Why have it on the master form???

And my variable for the City of resience is declare in the detail form with the Onload event: {Ville}=[global]; Then I can see it as soon as I am updating the description field. Is it ok???

Re: If and else in form

Just in case…

The customer choose the city in the second part (tab) of the master with a dropdown list. Then when getting to the course (description) dropdown list in the detail form, the city has already been copied to the detail record AND the PrixResident + PrixnonResident fields are populated with an sc_lookup event. Then depending if the city name in the same record is ‘LaSalle’ the Montant field should get the Resident value (less expensive one).

Sorry for all those precisions but since I am french speaking, I always think I am not clear enough!!

Re: If and else in form

Foud almost all my solutions:

For the if, else question,

{Ville}=[global];
{montant} = ({ville} == ‘LaSalle’) ? {PrixResident} : {PrixNonResident};

So half is comming from Diego(Scriptcase support) and half Scott (forum)

Still missing: How to get a person age from date of birth…

Thank you Scott!

Re: If and else in form

You can google: ‘php calculate age’ and it will produce many results. You then create a php method that is called when you load your form (or when you save) to save the value to a var/field.
If you want to perform this on a form live, then make a ajax call to the method and return the result after you change the date field.

I use this JS code if you want to calculate it without going to the server with a call.


function getAge(dateString) {
  var today = new Date();
  var birthDate = new Date(dateString);
  var age = today.getFullYear() - birthDate.getFullYear();
  var m = today.getMonth() - birthDate.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
  }
  return age;
}

Regards,
Scott.