We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1887
    • 50 Posts
    Not being a professional programmer, linking MODX to SugarCRM has not been straight forward. I was previously just using javascript validation which then posted directly to SugarCRM which gave no protection against spammers. Recently the amount of spam has increased so I decided to spend the day coming up with a more robust solution.

    The solution I went for was to use FormItBuilder to help build the forms along with javascript validation and then a custom FormIt hook to send the data via SOAP to SugarCRM. Below is the solution I went with:

    <?php
    $snippetName='FormItBuilder_MyContactForm';
    require_once $modx->getOption('core_path',null,MODX_CORE_PATH).'components/formitbuilder/model/formitbuilder/FormItBuilder.class.php';
    if (function_exists('FormItBuilder_MyContactForm')===false) {
    function FormItBuilder_MyContactForm(modX &$modx, $snippetName) {    
     
    /*--------------------*/
    /*CREATE FORM ELEMENTS*/
    /*--------------------*/
    //Text Fields
    $o_fe_first_name	= new FormItBuilder_elementText('first_name','First Name');
    $o_fe_last_name		= new FormItBuilder_elementText('last_name','Last Name');
    $o_fe_job_title		= new FormItBuilder_elementText('job_title','Job Title');
    $o_fe_company		= new FormItBuilder_elementText('company','Company Name');
    $o_fe_phone		= new FormItBuilder_elementText('phone','Telephone');
    $o_fe_email		= new FormItBuilder_elementText('email_address','Email Address');
    
     
    //Check Boxes
    $o_fe_checkNews     = new FormItBuilder_elementCheckbox('agree_newsletter','Sign me to the newsletter', 'Yes', 'No', true);
    
    //Text area
    $o_fe_notes         = new FormItBuilder_elementTextArea('notes','Additional Comments',8,40,'');
    
    //Hidden Details
    $o_fe_campaign_id	= new FormItBuilder_elementHidden('campaign_id','','PUT YOU CAMPAIGN ID HERE');
    $o_fe_assigned_user_id 	= new FormItBuilder_elementHidden('assigned_user_id','','PUT YOUR ASSIGNED USER ID HERE');
    $o_fe_lead_source 	= new FormItBuilder_elementHidden('lead_source','Web Site','Help Web Site');
    $o_fe_lead_source->showInEmail(true);
    $o_fe_status 		= new FormItBuilder_elementHidden('status','Lead Status','New');
    $o_fe_status->showInEmail(true);
    
    //Form Buttons
    $o_fe_buttSubmit    = new FormItBuilder_elementButton('submit','Submit Form','submit');
    $o_fe_buttReset     = new FormItBuilder_elementButton('reset','Reset Form','reset');
     
     
    /*--------------------*/
    /*SET VALIDATION RULES*/
    /*--------------------*/
    $a_formRules=array();
    //Set required fields
    $a_formFields_required = array($o_fe_notes, $o_fe_first_name, $o_fe_last_name, $o_fe_email);
    foreach($a_formFields_required as $field){
        $a_formRules[] = new FormRule(FormRuleType::required,$field);
    }
    
    $a_formRules[] = new FormRule(FormRuleType::email, $o_fe_email, NULL, 'Please provide a valid email address');
      
      
    /*----------------------------*/
    /*CREATE FORM AND ADD ELEMENTS*/
    /*----------------------------*/
    $o_form = new FormItBuilder($modx,'myContactForm');
    $o_form->setHooks(array('Formit.SugarHook','email','redirect'));
    $o_form->setRedirectDocument(80); //document to redirect to after successful submission 
    $o_form->addRules($a_formRules);
    $o_form->setPostHookName($snippetName);
      
    //specify to and from email addresses, also see replyTo, CC and BCC options
    $o_form->setEmailToAddress('TO EMAIL ADDRESS');
    $o_form->setEmailFromAddress('FROM EMAIL ADDRESS');
    $o_form->setEmailFromName('FROM NAME');
    $o_form->setEmailSubject('Web lead from: [[+first_name]] [[+last_name]] from [[+company]]');
    $o_form->setEmailHeadHtml('<p>This is a response sent by <b>[[+first_name]] [[+last_name]]</b> using the <b>[[+lead_source]]</b> form:</p>');
    $o_form->setJqueryValidation(true);
      
    //add elements to form in preferred order
    $o_form->addElements(
        array(
            new FormItBuilder_htmlBlock('<h2>Personal Information</h2>'),
            $o_fe_first_name, $o_fe_last_name, $o_fe_job_title, $o_fe_company, $o_fe_email, $o_fe_phone,
    	new FormItBuilder_htmlBlock('<hr class="formSpltter" /><h2>Your message</h2>'),
    		$o_fe_notes, 
            new FormItBuilder_htmlBlock('<hr class="formSpltter" /><h2>Newsletter</h2>'),
    		$o_fe_checkNews, 
            new FormItBuilder_htmlBlock('</div>'),
            $o_fe_buttSubmit,   $o_fe_buttReset,
    		$o_fe_campaign_id, $o_fe_assigned_user_id, $o_fe_lead_source, $o_fe_status
        )
    );
    return $o_form;
    }
    }
    //Run the form construction function above
    $o_form = FormItBuilder_myContactForm($modx,$snippetName);
    if(isset($outputType)===false){
        //this same snippet was called via various other hooks
        return $o_form->processCoreHook($hook, $o_form);
    }else{
        //Final output for form
        return $o_form->output();
    }


    The above code runs 3 hooks, the first called Formit.SugarHook which is makes a SOAP connection to SugarCRM:

    <?php
    
    $options = array(
        "location" => 'SUGAR URL/soap.php',
        "uri" => 'FULL SUGAR URL',
        "trace" => 1
    );
    
    //user authentication array
    $user_auth = array(
        "user_name" => 'SUGAR USERNAME',
        "password" => MD5('SUGAR PASSWORD'),
        "version" => '.01'
    );
    
    // connect to soap server
    $client = new SoapClient(NULL, $options);
    
    // Login to SugarCRM
    $response = $client->login($user_auth,'test');
    $session_id = $response->id;
    $user_id = $client->get_user_id($session_id);
    
    // Send Data
    $response = $client->set_entry($session_id, 'Leads', array(
    	array("name" => 'first_name',"value" => $hook->getValue('first_name')),
    	array("name" => 'last_name',"value" => $hook->getValue('last_name')),
    	array("name" => 'title',"value" => $hook->getValue('job_title')),
    	array("name" => 'account_name',"value" => $hook->getValue('company')),
    	array("name" => 'phone_work',"value" => $hook->getValue('phone')),
    	array("name" => 'email1',"value" => $hook->getValue('email_address')),
    	array("name" => 'email_opt_in_c',"value" => $hook->getValue('agree_newsletter')),
    	array("name" => 'description',"value" => $hook->getValue('notes')),
    	array("name" => 'campaign_id',"value" => $hook->getValue('campaign_id')),
    	array("name" => 'assigned_user_id',"value" => $hook->getValue('assigned_user_id')),
    	array("name" => 'lead_source',"value" => $hook->getValue('lead_source')),
    	array("name" => 'status',"value" => $hook->getValue('status')),
    	array("name" => 'status_description',"value" => "HTTP User Agent: ".$_SERVER["HTTP_USER_AGENT"]),
    	array("name" => 'lead_source_description',"value" => "Help Web site: ".$_SERVER["HTTP_HOST"]."\n\rRemote address: ".$_SERVER["REMOTE_ADDR"]),
    	array("name" => 'refered_by',"value" => $_SERVER["HTTP_REFERER"]),
    	array("name" => 'pe_account_type_c',"value" => "Lead"),
    ));
    
    // Always return true
    return true;
    


    Hope this is of use to someone else (and any improvements welcome)

    Kind regards

    James
      • 39649
      • 17 Posts
      Quote from: apathy at Jul 23, 2013, 06:25 PM
      Not being a professional programmer, linking MODX to SugarCRM has not been straight forward. I was previously just using javascript validation which then posted directly to SugarCRM which gave no protection against spammers. Recently the amount of spam has increased so I decided to spend the day coming up with a more robust solution.

      The solution I went for was to use FormItBuilder to help build the forms along with javascript validation and then a custom FormIt hook to send the data via SOAP to SugarCRM. Below is the solution I went with:

      <!--?php
      $snippetName='FormItBuilder_MyContactForm';
      require_once $modx--->getOption('core_path',null,MODX_CORE_PATH).'components/formitbuilder/model/formitbuilder/FormItBuilder.class.php';
      if (function_exists('FormItBuilder_MyContactForm')===false) {
      function FormItBuilder_MyContactForm(modX &$modx, $snippetName) {    
       
      /*--------------------*/
      /*CREATE FORM ELEMENTS*/
      /*--------------------*/
      //Text Fields
      $o_fe_first_name	= new FormItBuilder_elementText('first_name','First Name');
      $o_fe_last_name		= new FormItBuilder_elementText('last_name','Last Name');
      $o_fe_job_title		= new FormItBuilder_elementText('job_title','Job Title');
      $o_fe_company		= new FormItBuilder_elementText('company','Company Name');
      $o_fe_phone		= new FormItBuilder_elementText('phone','Telephone');
      $o_fe_email		= new FormItBuilder_elementText('email_address','Email Address');
      
       
      //Check Boxes
      $o_fe_checkNews     = new FormItBuilder_elementCheckbox('agree_newsletter','Sign me to the newsletter', 'Yes', 'No', true);
      
      //Text area
      $o_fe_notes         = new FormItBuilder_elementTextArea('notes','Additional Comments',8,40,'');
      
      //Hidden Details
      $o_fe_campaign_id	= new FormItBuilder_elementHidden('campaign_id','','PUT YOU CAMPAIGN ID HERE');
      $o_fe_assigned_user_id 	= new FormItBuilder_elementHidden('assigned_user_id','','PUT YOUR ASSIGNED USER ID HERE');
      $o_fe_lead_source 	= new FormItBuilder_elementHidden('lead_source','Web Site','Help Web Site');
      $o_fe_lead_source->showInEmail(true);
      $o_fe_status 		= new FormItBuilder_elementHidden('status','Lead Status','New');
      $o_fe_status->showInEmail(true);
      
      //Form Buttons
      $o_fe_buttSubmit    = new FormItBuilder_elementButton('submit','Submit Form','submit');
      $o_fe_buttReset     = new FormItBuilder_elementButton('reset','Reset Form','reset');
       
       
      /*--------------------*/
      /*SET VALIDATION RULES*/
      /*--------------------*/
      $a_formRules=array();
      //Set required fields
      $a_formFields_required = array($o_fe_notes, $o_fe_first_name, $o_fe_last_name, $o_fe_email);
      foreach($a_formFields_required as $field){
          $a_formRules[] = new FormRule(FormRuleType::required,$field);
      }
      
      $a_formRules[] = new FormRule(FormRuleType::email, $o_fe_email, NULL, 'Please provide a valid email address');
        
        
      /*----------------------------*/
      /*CREATE FORM AND ADD ELEMENTS*/
      /*----------------------------*/
      $o_form = new FormItBuilder($modx,'myContactForm');
      $o_form->setHooks(array('Formit.SugarHook','email','redirect'));
      $o_form->setRedirectDocument(80); //document to redirect to after successful submission 
      $o_form->addRules($a_formRules);
      $o_form->setPostHookName($snippetName);
        
      //specify to and from email addresses, also see replyTo, CC and BCC options
      $o_form->setEmailToAddress('TO EMAIL ADDRESS');
      $o_form->setEmailFromAddress('FROM EMAIL ADDRESS');
      $o_form->setEmailFromName('FROM NAME');
      $o_form->setEmailSubject('Web lead from: [[+first_name]] [[+last_name]] from [[+company]]');
      $o_form->setEmailHeadHtml('<p>This is a response sent by <b>[[+first_name]] [[+last_name]]</b> using the <b>[[+lead_source]]</b> form:</p>');
      $o_form->setJqueryValidation(true);
        
      //add elements to form in preferred order
      $o_form->addElements(
          array(
              new FormItBuilder_htmlBlock('<h2>Personal Information</h2>'),
              $o_fe_first_name, $o_fe_last_name, $o_fe_job_title, $o_fe_company, $o_fe_email, $o_fe_phone,
      	new FormItBuilder_htmlBlock('<hr class="formSpltter"><h2>Your message</h2>'),
      		$o_fe_notes, 
              new FormItBuilder_htmlBlock('<hr class="formSpltter"><h2>Newsletter</h2>'),
      		$o_fe_checkNews, 
              new FormItBuilder_htmlBlock(''),
              $o_fe_buttSubmit,   $o_fe_buttReset,
      		$o_fe_campaign_id, $o_fe_assigned_user_id, $o_fe_lead_source, $o_fe_status
          )
      );
      return $o_form;
      }
      }
      //Run the form construction function above
      $o_form = FormItBuilder_myContactForm($modx,$snippetName);
      if(isset($outputType)===false){
          //this same snippet was called via various other hooks
          return $o_form->processCoreHook($hook, $o_form);
      }else{
          //Final output for form
          return $o_form->output();
      }


      The above code runs 3 hooks, the first called Formit.SugarHook which is makes a SOAP connection to SugarCRM:

      <!--?php
      
      $options = array(
          "location" =--> 'SUGAR URL/soap.php',
          "uri" => 'FULL SUGAR URL',
          "trace" => 1
      );
      
      //user authentication array
      $user_auth = array(
          "user_name" => 'SUGAR USERNAME',
          "password" => MD5('SUGAR PASSWORD'),
          "version" => '.01'
      );
      
      // connect to soap server
      $client = new SoapClient(NULL, $options);
      
      // Login to SugarCRM
      $response = $client->login($user_auth,'test');
      $session_id = $response->id;
      $user_id = $client->get_user_id($session_id);
      
      // Send Data
      $response = $client->set_entry($session_id, 'Leads', array(
      	array("name" => 'first_name',"value" => $hook->getValue('first_name')),
      	array("name" => 'last_name',"value" => $hook->getValue('last_name')),
      	array("name" => 'title',"value" => $hook->getValue('job_title')),
      	array("name" => 'account_name',"value" => $hook->getValue('company')),
      	array("name" => 'phone_work',"value" => $hook->getValue('phone')),
      	array("name" => 'email1',"value" => $hook->getValue('email_address')),
      	array("name" => 'email_opt_in_c',"value" => $hook->getValue('agree_newsletter')),
      	array("name" => 'description',"value" => $hook->getValue('notes')),
      	array("name" => 'campaign_id',"value" => $hook->getValue('campaign_id')),
      	array("name" => 'assigned_user_id',"value" => $hook->getValue('assigned_user_id')),
      	array("name" => 'lead_source',"value" => $hook->getValue('lead_source')),
      	array("name" => 'status',"value" => $hook->getValue('status')),
      	array("name" => 'status_description',"value" => "HTTP User Agent: ".$_SERVER["HTTP_USER_AGENT"]),
      	array("name" => 'lead_source_description',"value" => "Help Web site: ".$_SERVER["HTTP_HOST"]."\n\rRemote address: ".$_SERVER["REMOTE_ADDR"]),
      	array("name" => 'refered_by',"value" => $_SERVER["HTTP_REFERER"]),
      	array("name" => 'pe_account_type_c',"value" => "Lead"),
      ));
      
      // Always return true
      return true;
      


      Hope this is of use to someone else (and any improvements welcome)

      Kind regards

      James
      Very cool. Do you know of an implementation of Register sending signups to Sugar CRM?