It subtracts the value of one attribute to the value of another attribute, if you are inserting.
It adds the value of one attribute to the value of another attribute, if you are deleting.
It calculates the difference between the new and old value of the attribute you update and that difference is subtracted from another attribute, if you are updating.

Syntax

Subtract(att1 , att2) [ if cond ] ;

In mode:

  • Insert: the value of the att1 attribute is subtracted from the value of the att1 attribute (if the specified condition is true)
  • Delete: the value of the att1 attribute is added to the value of the att2 attribute (if the specified condition is true)
  • Update: the difference between the new and old value of the att1 attribute is subtracted from the value of the att2 attribute (if the specified condition is true)

Let's see an example.

Customer                              Trip                      Prize                    Country  
{                                     {                         {                        {
    CustomerId*                          TripId*                  PrizeId*                  CountryId*
    CustomerName                         TripDate                 PrizeDate                 CountryName
    CustomerLastName                     CountryId                PrizeDescription          City
    CustomerPhone                        CountryName              PrizeMiles                {
    CustomerTotalMiles                   CityId                   CustomerId                  CityId*
    Trip                                 CityName                 CustomerName                CityName
    {                                    TripMiles                CustomerTotalMiles        }
      TripId*                          }                        }                         }
      TripDate                                                                     
      CountryId
      CountryName
      CityId
      CityName
      TripMiles
    }
}

We have the Prize transaction, which allows defining rewards to be traded for miles.

Each reward requires a number of miles in order to be assigned to a customer who has accrued that number of miles (or who has an even larger number of miles). Therefore, when trying to assign one customer to one reward, we must check if the customer has enough miles to be assigned this reward. If he/she has enough miles and accepts the reward, we must subtract the miles he traded. If the customer doesn’t have enough miles, we need to show the corresponding error message.

In the Prize transaction we defined the two following rules:

Subtract(PrizeMiles, CustomerTotalMiles);
Error(“Customer doesn’t have enough miles accrued”) if CustomerTotalMiles < 0;

The following behavior is incorporated into the Subtract rule:

  • If a new prize is inserted, the value of PrizeMiles is subtracted from CustomerTotalMiles.
  • If a prize is deleted, the value of PrizeMiles is added to CustomerTotalMiles.
  • If the value of PrizeMiles is changed, its old value is automatically added to the the customer’s total miles and the new value is subtracted from the customer’s total miles.

Since both rules involve the CustomerTotalMiles attribute, and one of the rules updates the attribute and the other one assesses its value, GeneXus determines that first it must make the subtraction that updates the CustomerTotalMiles attribute and then evaluate what happened with its value.

Because the subtraction is made first, if the customer has fewer miles than those required by the reward, the CustomerTotalMiles attributes will have a negative value. For this reason, the Error rule evaluates if CustomerTotalMiles<0. If this happens, the Error rule is triggered, an error message is displayed and the subtract rule is undone; that is to say, its execution is reverted as if nothing was done and the customer’s total number of miles is not modified.

If CustomerTotalMiles doesn’t have a negative value (CustomerTotalMiles<0) after making the subtraction, it means that the subtract operation has been performed. 

Note that if the record of a reward assigned to the customer is deleted, the subtract rule adds instead of subtracting. That is to say, to the total number of miles accrued by the customer (CustomerTotalMiles) it adds the number of miles corresponding to the reward (PrizeMiles).

See also

Add rule