How to execute server function in action

This tutorial shows you how to execute Server function in action component. In our example we will use plugin function as action type to insert new promotion in our database.

Action sql

1. Login into developer panel of application.

2. Click on Pages menu item.

3. Select page pagePromotions.

4. Drag and drop Action component from all components list to button for registration named ButtonSaveNewPromotions.

Action server function

5. In dropped validation component properties, for Data source type property choose Server function.

6. Click on Data source Function field and on the left side of dialog window choose plugin. On the right side choose function. You can send values from form components to function in plugin as parameters. Drag and drop Form components from left side to Parameters text fields on the right side. Function in plugin returns result variable. Variable result in plugin function is an instance of AjaxProxy class. Attribute status of result variable ($result->status) can be true or false. If there is an error in action proccess set attribute status to false. You can also set error message in attribute error ($result->error). Click on Save button.
Example server function PHP code:

function SavePromotion($name, $dateFrom, $dateTo) {
  $result = new AjaxResult();
  $result->status = false;

  try {
    // TODO Save promotion.
    $entityService = Services::GetEntityService();
    $entityService->SaveData("Promotion", array("Name"=> $name, "Valid from"=> $dateFrom, "Valid to" => $dateTo));
    $result->status = true;
  } catch (AppException $appEx) {
    $result->status = false;
    $result->error = $appEx->GetErrorCode() . ' - ' . $appEx->GetAppMessage();
  } catch (Exception $e) {
    $result->status = false;
    $result->error = "Custom error message!";
  }
  return $result;
}

For more details see How to create plugin in action
Save promotion server function

7. Click on Save changes, go into your application and check out the results.