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

Create Hooks in Civicrm in Joomla 1.5

  Create Hooks in Civicrm in Joomla 1.5 Create folder in any directory like /Joomla/media/civicrm_hook . Create file civicrmHooks.php in this directory. Go to Administer CiviCRM > Global Settings > Directories Set the path for custom php here. Use the civicrm hooks with Joomla_civicrm_hookName format. Ex. function Joomla_civicrm_buildForm( ){ // Write your code here. }

New Services to the world

Different government services & Online Services GST Registration Company Registration ITR PAN Card Aadhar Card E-way Bill Registration Food License Accounting Service Website designing Website development Website Hosting Website Domain Website Testing (Manual) Website SCO Optimization Lead Generation Add Creation and many more...