1

According to CiviCRM API documentation, the .create() function with a specified contact ID should modify the records within CiviCRM; however, even after trying that, I still can't seem to properly push changes to CiviCRM.

In the code below, I'm trying to change Jane Doe's gender from "1" to "2", and when I change that by setting val.gender_id="2", it prints out the right result. However, when I try to "get" Jane Doe's information afterwards, it prints her gender_id as "1". So, it doesn't actually update the CiviCRM database.

Is there a way to fix this?

//TESTING FOR UPDATING CONTACT INFORMATION OF 'JANE DOE' in CIVICRM DATABASE

crmAPI.create('contact', {id:'12966'},
  function (result) {
      console.log(result); 
      val=result.values[0]; 
      val.gender_id='2'; 
      console.log('UPDATED CONTACT: '+ val.display_name + " " + val.gender_id + " " + val.country_id);

crmAPI.get('contact', {id:'12966'}, 
  function(result) {
    console.log(result); 
    for (var i in result.values) {
      val=result.values[i]; 
      console.log(val.display_name + " " + val.gender_id + " " + val.country_id); 
    }
  }
); 

});

  • try crmAPI.create('contact', {id:'12966', gender_id:2}? – ErikH - CiviCooP Jan 26 '17 at 12:17
2

EDIT: I figured out what went wrong. The gender_id change must be specified as a parameter in the .create() function. It shouldn't be changed in the callback.

The correct code is:

crmAPI.create('contact', {id:'12966', gender_id:'2'},
   function (result) {
       console.log(result); 
       val=result.values[0]; 
       console.log('UPDATED CONTACT: '+ val.display_name + " " + val.gender_id + " " + val.country_id);
}); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.