Skip to main content

Create contact using custom field in civicrm 4.0 (civicrm API)


Create contact using custom field in prifile when event registration.
Create Contacts using API in civicrm 4.0 in drupal 7 on post process.
 If the contact exists then it o/p that contacts id & if not exists then it create new contact & gives the o/p .

function eventType_customPath_civicrm_postProcess( $formName, &$form ){
    if( $formName == 'CRM_Event_Form_Registration_Confirm' ){
        $participantContact = $form->getVar('_params');
        $participantFirstNameId = array( 'custom_11', 'custom_15', 'custom_19' );   /* add custom first name Id */
        $participantLastNameId = array( 'custom_12', 'custom_16', 'custom_20' );    /* add custom last name Id */
        $participantEmailId = array( 'custom_13', 'custom_17', 'custom_21' );       /* add custom email Id */
        $participant = array( );
        for( $count = 0; $count < 3; $count++ ) {
            $participant[ $count ][ 'firstname' ] = $participantContact[ $participantFirstNameId [ $count ] ];
            $participant[$count]['lastname'] = $participantContact [ $participantLastNameId [ $count ] ];
            $participant[$count]['email'] = $participantContact [ $participantEmailId [ $count ] ];
        }
        require_once "api/api.php";
        foreach( $participant as $participantKey => $participantValue ){
            if( !empty( $participantValue['firstname'] ) && !empty( $participantValue['lastname'] ) ) {
                $searchContact = array ( 'first_name' => $participantValue['firstname'] , 'last_name' => $participantValue['lastname'], 'version' =>3 );
                $contact = civicrm_api( 'Contact', 'Get', $searchContact );
                if(isset($contact['value'])){
                    $contactId[]['contact_a'] = $contact['id'];
                }else{
                    $createContact = array ( 'first_name' => $participantValue['firstname'] , 'last_name' => $participantValue['lastname'], 'email' => $participantValue['email'],'contact_type' => 'Individual', 'version' =>3 );
                    $contact = civicrm_api( 'Contact', 'Create', $createContact );
                    $contactId[]['contact_a'] = $contact['id'];
                }
            }
        }
    }
}

Gives the o/p as 
$contact 

[0]=>
          array(
                   [contact_a] => 123
                 )
[1] => array(
                    [contact_a] => 124
                    )

Comments

Popular posts from this blog

Set Multisite in drupal

For Linux 9.10 Please Follow the steps : 1) Extract the drupal in public html folder or any other folder.(say exampleDrupal). 2) Add below lines to exampleDrupal/sites/sites.php     $sites['site1.test'] = 'site1.test';     $sites['site2.test'] = 'site2.test'; 3)  Create two folders in exampleDrupal/sites/ folder.       as site1.test & site2.test.      OR      Using terminal run following commands :      i)  cd example.com/sites      ii) mkdir site1.test/ site2.test/ 4) Copy the default.settings.php file from  exampleDrupal/sites/default to site1.test &  site2.test folder.     OR      Using terminal run following commands :       i) cp default/default.settings.php site1.test/settings.php       ii) cp default/default.settings.php si...