1

I've created a Logic Hook within the Leads module to save to a field in the database. The problem is that when I save the Lead first it displays a duplicate message:

Database failure error check SuiteCRM logs.

If I comment out this code the Lead is then saved.

LogicHook:

<?php

class LeadData
{
    public function leadSaveData(&$bean, $events, $arguments)
    {
        $stateId = $bean->state_c;

        if ($stateId != "") {
            $beanst_state = BeanFactory::getBean('s1_state', $stateId); 
            $StateName = $beanst_state->name;
            $bean->resstate_c = $StateName;
        } else {
            $bean->resstate_c = "Punjab";
        }

        if (!empty($bean->mediatype_c)) {
            $mediaType = $bean->mediatype_c;
            $callstatus = $bean->callstatus_c; //for follow up
            if ($mediaType == 'Selectmedia' && $mediaType != '') {
                $bean->refrence_c = 'null';
            } else if ($mediaType == 'Refrence') {
                $bean->eventname_c = 'null';
            } else {
                $bean->mediatype_c = 'null';
                $bean->refrence_c = 'null';
                $bean->eventname_c = 'null';
            }

            if ($callstatus != 'followup') {
                $bean->calllater_c = '0000-00-00 00:00:00';
            }
            $bean->save();
        }
    }
}
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
Anuradha
  • 69
  • 8
  • 1
    Presumably you followed the recommendation to check the logs. Perhaps including that information might help someone solve the problem? – miken32 Jan 29 '16 at 04:04

1 Answers1

2

Please follow these steps in your code to make this error correct or working. Hope so it will helps you.

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

if (!defined('sugarEntry') || !sugarEntry)
    die('Not A Valid Entry Point');

class saveextradata_logic_hooks_class
{
    static $already_ran = false;

    function saveextradata_after_save_method(&$bean, $events, $arguments)
    {
        global $db;

        if (self::$already_ran == true) return;
        self::$already_ran = true;

        $lead_id = $bean->id;

        $stateId = $_POST['state_c'];
        $sql = "select name from s1_state where id='$stateId'";
        $query = $db->query($sql);
        $result = $db->fetchByAssoc($query);
        $statename = $result[name];

        if ($stateId != "") {
            $statename;
        } else {
            $statename = "Punjab";
        }
        if (!empty($_POST['mediatype_c'])) {
            $mediaType = $_POST['mediatype_c'];

            if ($mediaType == 'Selectmedia' && $mediaType != '') {
                $mediatype = 'Selectmedia';
                $reference = 'null';
                $eventname = $_POST['eventname_c'];
            } else if ($mediaType == 'Refrence') {
                $mediatype = 'Refrence';
                $reference = $_POST['refrence_c'];
                $eventname = 'null';
            } else {
                $mediatype = 'null';
                $reference = 'null';
                $eventname = 'null';
            }
        }

        $callstatus = $_POST['callstatus_c'];//for follow up
        if ($callstatus != 'followup') {
            $calllater = '0000-00-00 00:00:00';
        }

        /*$bean->eventname_c = $eventname;
        $bean->refrence_c = $reference;
        $bean->mediatype_c = $mediatype_c;
        $bean->calllater_c = $calllater;
        $bean->resstate_c = $statename;
        $bean->save();*/

        $leadupdate = "update leads_cstm set resstate_c='$statename',eventname_c='$eventname',refrence_c='$reference',mediatype_c='$mediatype',calllater_c='$calllater' WHERE id_c = '$lead_id'";
        $resultProductmasterdetail = $db->query($leadupdate);
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • 1
    I have analyzed that there is a problem in leads module when we are saving the data using bean it makes the data duplicate after insertion so you can follow the step to update the data according to particular lead.. Hope so it will be working for you.. – arun sharma Feb 05 '16 at 06:02
  • Hello Arun, Thank you so much to solve this database error. yes, This is working and resolved my error. – Anuradha Feb 05 '16 at 06:08