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