What is the correct way to set a default value on rendering a form for custom values. The code below works for native fields, but not for custom fields (presumably because they are looked up through API calls.

This is the code I have so far:

            function webform_membership_civicrm_buildForm($formName, &$form) {
                if ($formName == 'CRM_Activity_Form_Activity') {
                    if (in_array($form->getAction(), array(CRM_Core_Action::ADD, CRM_Core_Action::UPDATE))) {
                        $defaults['custom_8_-1'] = 710;

  • I assume you have checked in the _elementIndex array of the form object that you have the correct name of the form field? Apr 18, 2017 at 7:45
  • Yes unfortunately, no _1 or -1 or similar value in the array.
    – Adam
    Apr 18, 2017 at 12:01
  • The ajax call is being made to: /index.php?q=civicrm/custom&type=Activity&subType=58&entityID=2&qf=xxxx&cgcount=1&snippet=json which does contain the HTML value.
    – Adam
    Apr 18, 2017 at 12:32
  • Sorry - not sure I understand exactly what you're trying to do but I often set values in a form based on conditionals (and webform validation) - if Membership is of type x -> then set some other webform element to =y - not sure I've tried this specifically for custom data values - but can't see how that would be any different? Apr 18, 2017 at 14:35
  • I am trying to set default values, specifically within CiviCRM. i.e. Set a custom default value if none has been set within CiviCRM for that entity. For instance, staff create activities (internally) for clients as part of the default workflow, which customers can also do via the web form. The code below sets a custom default value on create or edit of an existing activity within CiviCRM, where a value is not set. I have successfully been able to alter the web forms (front end) without issue.
    – Adam
    Apr 18, 2017 at 21:15

1 Answer 1


Value is present on AJAX form. Solution:

            function webform_membership_civicrm_buildForm($formName, &$form) {

                if (($formName == 'CRM_Activity_Form_Activity') || ($formName == 'CRM_Custom_Form_CustomDataByType')) {

                    if (in_array($form->getAction(), array(0,1,2))) {

                        //$defaults['custom_8_-1'] = 710;
                        //$form->_groupTree[4]['fields'][8]['element_value'] = 111;

                        foreach ($form->_elements as $index => &$element) {
                            if ($element->_attributes['data-crm-custom'] == 'Points_for_this:Points') {
                                $element->_attributes['value'] = 115; // Set custom value here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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