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...

To get pager on page

Use of pager query :   Drupal 4.6 - 6 :  pager_query($query, $limit = 10, $element = 0, $count_query = NULL) Perform a paged database query. Use this function when doing select queries you wish to be able to page. The pager uses LIMIT-based queries to fetch only the records required to render a certain page. However, it has to learn the total number of records returned by the query to compute the number of pages (the number of records / records per page). This is done by inserting "COUNT(*)" in the original query. For example, the query "SELECT nid, type FROM node WHERE status = '1' ORDER BY sticky DESC, created DESC" would be rewritten to read "SELECT COUNT(*) FROM node WHERE status = '1' ORDER BY sticky DESC, created DESC". Rewriting the query is accomplished using a regular expression.  $output1 .= theme('pager',null, $count, 0); This is used to get the number. We can use this pager query in "Custom Search...