Answers.Body: <p>Not exactly a scheduled job or an answer to the questions in regard to civix and the api but you may want to have a look at the <a href="https://github.com/systopia/de.systopia.sqltasks" rel="nofollow noreferrer">SQL-Task Extension</a>.</p>
  <p>It allows you to  configure any number of scheduled tasks via the UI. It is essentially a SQL driven, configurable execution of any of the following generic CiviCRM actions:</p>
  <ul>
  <li>Create activities</li>
  <li>Call any CiviCRM API3 action</li>
  <li>Export to CSV,including zipping, uploading, emailing the results</li>
  <li>Synchronize a SQL result such as a contact ID list with a given tag or group</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-05T11:18:34.923Z
Answers.LastActivityDate: 2022-05-05T11:18:34.923Z
Answers.UserDisplayName: Fabian
Body: <p>I want to define some scheduled jobs so that old data is deleted from time to time. For example contacts in trash, mailings older than X year, drafted mailings never sent... For scheduled jobs I need to use api v3 - is this right? I read it somewhere...</p>
  <p>I found a module with some basic functionality like we need (<a href="https://github.com/fuzionnz/nz.co.fuzion.deleteoldbulkmailings" rel="nofollow noreferrer">https://github.com/fuzionnz/nz.co.fuzion.deleteoldbulkmailings</a>) and tried to adjust to our needs. I would like to understand how CiviCRM finds new API 3 actions and entities so I got rid of all the civix code. I reduced the code so that there is only folowing left:</p>
  <p>In folder api/v3/Bulkmailing there is is a file Deleteoldrecords.php:</p>
  <pre><code>/**
   * Bulkmailing.Deleteoldrecords API
   *
   * @param array $params
   * @return array API result descriptor
   * @see civicrm_api3_create_success
   * @see civicrm_api3_create_error
   * @throws API_Exception
   */
  function civicrm_api3_bulkmailing_Deleteoldrecords($params) {
    $isEnabled = Civi::settings()-&gt;get('logging');
    $logging = new CRM_Logging_Schema();
    if ($isEnabled) {
      $logging-&gt;disableLogging();
    }
    CRM_BulkMailing_BAO_Delete::delete($params);
    if ($isEnabled) {
      $logging-&gt;enableLogging();
    }
  
    return civicrm_api3_create_success(array(), $params, 'BulkMailing', 'DeleteOldRecords');
  }
  </code></pre>
  <p>In folder CRM/BulkMailing/BAO there is a file named Delete.php with the class CRM_BulkMailing_BAO_Delete:</p>
  <pre><code>class CRM_BulkMailing_BAO_Delete extends CRM_Contact_BAO_DeleteBase {
  
    /**
     * Delete older bulk mailing records
     *
     * @param array $parameters
     *  The parameters as defined in scheduled job.
     *  See self::getMailingIdsFromParams() for details
     *  Additionally skip_activity_delete can be set to TRUE so that Activities
     *  related to the deleted mailing should not be deleted.
     *
     * @throws \API_Exception
     * @throws \CiviCRM_API3_Exception
     *
     */
    public static function delete(array $parameters): void {
      $parameters['delete_drafts'] = FALSE;
      $mailingIds = self::getMailingIdsFromParams($parameters);
      if (!empty($mailingIds)) {
        self::deleteMailings($mailingIds);
      }
    }
  }
  </code></pre>
  <p>For what ever reason before my changes I was able to create a scheduled job for entity BulkMailing and with action Deleteoldrecords. Now I can not.</p>
  <p>I find no documentation about how to create custom entities and actions for api v3 - at least none which exactly explains how CiviCRM finds my new entities / actions. All I find is how to use civix to create mass of code which is not actually needed (I am not a fan of these tools creating code for me...). I am sure there are naming conventions which are not documented?! Maybe I have somewhere a typo or a uppercase/lowercase problem which simply leads to this issue?!</p>
  <p>Could somebody explain to me a) if there is a way to use api V4 for scheduled jobs (which I would prefer) and b) if not how CiviCRM actually finds out which apiv3 entities and actions are defined by my extension?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-05T08:31:56.860Z
Comments.Text: Since your question is about doing this without civix, I'll just put this as a comment. Civix is intended to make it easier to do things like this and hide some of the underlying complexity.  See https://docs.civicrm.org/dev/en/latest/extensions/civix/#generate-api  `civix generate:api NewEntity newaction --schedule Hourly` creates all the files you need for your API function and a scheduled job to call it.  Civix doesn't create stuff just for the fun of filling up your filesystem!
Comments.UserDisplayName: Aidan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-05T09:56:11.133Z
Comments.Text: The whole MODULE.civix.php file is full of functions which are not needed. They are all "just in case" functions. So I'd like to understand what actually is needed to achieve just what I want to achieve without a bunch of useless code.
Comments.UserDisplayName: Tobias Krause
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-05T10:53:16.980Z
Comments.Text: totten has just released an updated civix reducing the boilerplate: https://civicrm.org/blog/totten/civix-v2205-how-remove-million-lines-extra-code
Comments.UserDisplayName: Aidan
Tags: <api><developer><civicrm-entity><scheduled-job>
Title: How can I write custom entites/ actions for api v3 - usage of api v4 for scheduled jobs
ThreadText: # How can I write custom entites/ actions for api v3 - usage of api v4 for scheduled jobs
  
  (Asked by: username_0 on 2022-05-05)
  
  I want to define some scheduled jobs so that old data is deleted from time to time. For example contacts in trash, mailings older than X year, drafted mailings never sent... For scheduled jobs I need to use api v3 - is this right? I read it somewhere...
  
  I found a module with some basic functionality like we need (<https://github.com/fuzionnz/nz.co.fuzion.deleteoldbulkmailings>) and tried to adjust to our needs. I would like to understand how CiviCRM finds new API 3 actions and entities so I got rid of all the civix code. I reduced the code so that there is only folowing left:
  
  In folder api/v3/Bulkmailing there is is a file Deleteoldrecords.php:
  
  ```
  /**
   * Bulkmailing.Deleteoldrecords API
   *
   * @param array $params
   * @return array API result descriptor
   * @see civicrm_api3_create_success
   * @see civicrm_api3_create_error
   * @throws API_Exception
   */
  function civicrm_api3_bulkmailing_Deleteoldrecords($params) {
    $isEnabled = Civi::settings()->get('logging');
    $logging = new CRM_Logging_Schema();
    if ($isEnabled) {
      $logging->disableLogging();
    }
    CRM_BulkMailing_BAO_Delete::delete($params);
    if ($isEnabled) {
      $logging->enableLogging();
    }
  
    return civicrm_api3_create_success(array(), $params, 'BulkMailing', 'DeleteOldRecords');
  }
  
  ```
  
  In folder CRM/BulkMailing/BAO there is a file named Delete.php with the class CRM\_BulkMailing\_BAO\_Delete:
  
  ```
  class CRM_BulkMailing_BAO_Delete extends CRM_Contact_BAO_DeleteBase {
  
    /**
     * Delete older bulk mailing records
     *
     * @param array $parameters
     *  The parameters as defined in scheduled job.
     *  See self::getMailingIdsFromParams() for details
     *  Additionally skip_activity_delete can be set to TRUE so that Activities
     *  related to the deleted mailing should not be deleted.
     *
     * @throws \API_Exception
     * @throws \CiviCRM_API3_Exception
     *
     */
    public static function delete(array $parameters): void {
      $parameters['delete_drafts'] = FALSE;
      $mailingIds = self::getMailingIdsFromParams($parameters);
      if (!empty($mailingIds)) {
        self::deleteMailings($mailingIds);
      }
    }
  }
  
  ```
  
  For what ever reason before my changes I was able to create a scheduled job for entity BulkMailing and with action Deleteoldrecords. Now I can not.
  
  I find no documentation about how to create custom entities and actions for api v3 - at least none which exactly explains how CiviCRM finds my new entities / actions. All I find is how to use civix to create mass of code which is not actually needed (I am not a fan of these tools creating code for me...). I am sure there are naming conventions which are not documented?! Maybe I have somewhere a typo or a uppercase/lowercase problem which simply leads to this issue?!
  
  Could somebody explain to me a) if there is a way to use api V4 for scheduled jobs (which I would prefer) and b) if not how CiviCRM actually finds out which apiv3 entities and actions are defined by my extension?
  
  **Tags:** api, developer, civicrm-entity, scheduled-job
  
  > **Comments:**
  > **username_1**: Since your question is about doing this without civix, I'll just put this as a comment. Civix is intended to make it easier to do things like this and hide some of the underlying complexity.  See https://docs.civicrm.org/dev/en/latest/extensions/civix/#generate-api  `civix generate:api NewEntity newaction --schedule Hourly` creates all the files you need for your API function and a scheduled job to call it.  Civix doesn't create stuff just for the fun of filling up your filesystem!  
  > **username_0**: The whole MODULE.civix.php file is full of functions which are not needed. They are all "just in case" functions. So I'd like to understand what actually is needed to achieve just what I want to achieve without a bunch of useless code.  
  > **username_1**: totten has just released an updated civix reducing the boilerplate: https://civicrm.org/blog/totten/civix-v2205-how-remove-million-lines-extra-code  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2022-05-05. Score: 1
  
  Not exactly a scheduled job or an answer to the questions in regard to civix and the api but you may want to have a look at the [SQL-Task Extension](https://github.com/systopia/de.systopia.sqltasks).
  
  It allows you to configure any number of scheduled tasks via the UI. It is essentially a SQL driven, configurable execution of any of the following generic CiviCRM actions:
  
  * Create activities
  * Call any CiviCRM API3 action
  * Export to CSV,including zipping, uploading, emailing the results
  * Synchronize a SQL result such as a contact ID list with a given tag or group
  
  
  ---
  
  