CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • How to using API to save value for custom file field?
Pages: [1]

Author Topic: How to using API to save value for custom file field?  (Read 4651 times)

tqnghiep

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 4.2.8
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.3
How to using API to save value for custom file field?
July 15, 2013, 09:00:38 am
I need help for custom file field api.

I create custom file field in Administer -> Customize Data and Screens -> Custom Fields, Field id is 6, => name is custom_6.
I assign this field into register profile for event, When user select and submit the file to server, i save upload it and save to database such as :
<code>
function registration_save_file($file_params){
      if (!civicrm_initialize()) {
         return null;
      }
         $file_params = array(
            'version'       =>3,          
            'file_type_id'  => $file_type_id,
            'mime_type'   => $file_params['type'],
            'name'      => $file_params['name'],
            'uri'         => $file_params['name'],
            'upload_date'   => date('Y-m-d h:i:s', time()),
         );
         $file_save = civicrm_api('File','Create', $file_params);         
         return $file_save;
}
</code>
The next i Save file id to contact info by :
<code>
function registration_save_contact($val){
      $params= array(
         'contact_type'  => 'Individual',   
         'preferred_language' => 'en_US',
         'version' =>3,
         'last_name'  => $val["last_name"],
         'first_name'  => $val["first_name"],
      );
      $upload = civicrm_registration_save_file($file);
      if (!civicrm_error ( $upload)) {
         $params['custom_6'] = $upload["id"];
      }
      if (!civicrm_initialize()) {
         return null;
      }      
      $contact = civicrm_api('Contact','Create', $params);
      if(civicrm_error($contact)){
         drupal_set_message(t($contact['error_message']), 'error');   
      }else{
         return $contact;
      }
}
</code>
OR
<code>
   if (!civicrm_initialize()) {
      return null;
   }
   
   
   $contactID = 17876;
   $params_contact = array(
         'version'       =>3,
         'id'           => $contactID,
         'custom_6'   => $upload["id"]//$upload["id"] = 30
   );
   
   $update = civicrm_api( 'Contact', 'Update', $params_contact);

   if (!civicrm_error ($update)) {
        // error handling
        var_dump($update);

    }
</code>
Or i using such as:
<code>
$contactID = 17820;
$passort = civicrm_api( 'CustomValue', 'Create', array(
      'version'       =>   '3',
      'entity_id'      =>   $contactID,
      $columnName      =>   $upload["id"]//$upload["id"] = 30,
      )   
   );
   var_dump($passort);
   // it return array(2) { ["is_error"]=> int(1) ["error_message"]=> string(57) "A fatal error was triggered: Cannot recognize File for 30" }
</code>

-------------
How to using API to save value for custom file field?

tqnghiep

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 4.2.8
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.3
Re: How to using API to save value for custom file field?
July 16, 2013, 02:00:54 am
System message "Could not move custom file to custom upload directory"

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: How to using API to save value for custom file field?
July 16, 2013, 02:06:59 am
I would take the message literally first? So is your custom upload directory set correctly (in the CiviCRM system settings), do you have enough access to the directory to be able to create the file there?
Is there a reason why you do not want to use REST with the API? I am not sure why you are using the upload file, but that can be me :-)
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • How to using API to save value for custom file field?

This forum was archived on 2017-11-26.