AnsweredAssumed Answered

BeanFactory script aborts while executing

Question asked by Janek Slijkhuis on Feb 6, 2018
Latest reply on Feb 15, 2018 by Francesca Shiekh

Hi, Iam a newbie at customized Sugarcrm scripts and I would appreciate any help. The script should create a course (training) for every product (Artikel) in a array. A course has relationships with a student (cursist), a product (Artikel) and a company (accounts). The first training is created successfully with the correct relationships, the second training is created but the script aborts before any relationship is established and the rest of the courses are not created. There is one log error (on log level warn) 

preprocess_fields_on_save() is deprecated

but this message is not making any sense to me.

 

The php script:

$cursist = new TROLC_CursistenOLC();
$cursist->retrieve($_REQUEST['record']);

$pakket = new Trpa1_Pakketten();
$pakket->retrieve($cursist->trpa1_pakketten_trolc_cursistenolc_1trpa1_pakketten_ida);

if($pakket->id == "") {
    die('Geen pakket gevonden');
}

$bedrijfsnaam = "";
if ($cursist->parent_type == "Accounts" and $cursist->parent_id != "") {
     $account = new Account();
     $account->retrieve($cursist->parent_id);
     $bedrijfsnaam = $account->name;
     $bedrijfsid = $account->id;
}

$db = DBManagerFactory::getInstance();
$sql1 = "SELECT a.name, a.id FROM trolc_artikelenolc AS a
        JOIN trpa1_pakketten_trolc_artikelenolc_c AS b ON a.id = b.trpa1_pakketten_trolc_artikelenolctrolc_artikelenolc_idb
        WHERE b.trpa1_pakketten_trolc_artikelenolctrpa1_pakketten_ida = '" . $pakket->id . "'";
$result1 = $GLOBALS['db']->query($sql1);

$artikelen = array();
$artikelen_id = array();
    while($row = $GLOBALS['db']->fetchByAssoc($result1)) {
        $artikelen[] = $row['name'];
        $artikelen_id[] = $row['id'];
    }

if(empty($bedrijfsnaam)){
$sql2 = "SELECT a.name, a.id FROM accounts AS a
        JOIN accounts_trolc_cursistenolc_1_c AS b ON a.id = b.accounts_trolc_cursistenolc_1accounts_ida
        WHERE b.accounts_trolc_cursistenolc_1trolc_cursistenolc_idb = '" . $cursist->id . "' ORDER BY 'id' ASC";
$result2 = $GLOBALS['db']->query($sql2);

if ($result2->num_rows > 0) {
    while($row = $GLOBALS['db']->fetchByAssoc($result2)) {
            $bedrijfsnaam = $row['name'];
            $bedrijfsid = $row['id'];
        }
}
else {
    echo "<script> alert('Geen bedrijf gekoppeld');</script>";
}
}

$telefoon = $cursist->phone_mobile;
if($telefoon == "") {
    $telefoon = $cursist->phone_home;
}
$art_aantal = count($artikelen_id);

for($art = 0; $art < $art_aantal; $art++) {  
    echo "Training " . $art . " " . $artikelen[$art] . "<br>";
$training = new TROLC_TrainingenOLC();
$training = BeanFactory::newBean('TROLC_TrainingenOLC');
$training->start_date = date("d-m-Y");
$training->date_entered = date("d-m-Y");
$training->status = "optie";
$training->contactbedrijfsnaam = $bedrijfsnaam;
$training->contactaanhef = $cursist->salutation;
$training->contactachternaam = $cursist->last_name;
$training->contactadres = $cursist->primary_address_street;
$training->contactemail = $cursist->email1;
$training->contactpostcode = $cursist->primary_address_postalcode;
$training->contacttelefoon = $telefoon;
$training->contactvoornaam = $cursist->first_name;
$training->contactwoonplaats = $cursist->primary_address_city;
$training->geboortedatum = $cursist->birthdate;
$training->geboorteplaats = $cursist->geboorteplaats;
$training->voorcontactaccount = $bedrijfsnaam;
$training->voorcontactcontact = $cursist->first_name . " " . $contact->last_name;
$training->save();
$record[$art] = $training->id;
    echo "Training " . $art . " set data<br>";

$training_cur = BeanFactory::getBean('TROLC_CursistenOLC', $cursist->id);
$training_cur->load_relationship('trolc_cursistenolc_trolc_trainingenolc_1');
$training_cur->trolc_cursistenolc_trolc_trainingenolc_1->add($record[$art]);
$training_cur->load_relationship('trolc_cursistenolc_trolc_trainingenolc_2');
$training_cur->trolc_cursistenolc_trolc_trainingenolc_2->add($record[$art]);
$training_cur->save();
    echo "Training " . $art . " relationship cursisten established<br>";

$training_art = BeanFactory::getBean('TROLC_ArtikelenOLC', $artikelen_id[$art]);
$training_art->load_relationship('trolc_artikelenolc_trolc_trainingenolc_1');
$training_art->trolc_artikelenolc_trolc_trainingenolc_1->add($record[$art]);
$training_art->save();
    echo "Training " . $art . " relationship Artikelen established<br>";

$training_acc = BeanFactory::getBean('Accounts', $bedrijfsid);
$training_acc->load_relationship('accounts_trolc_trainingenolc_1');
$training_acc->accounts_trolc_trainingenolc_1->add($record[$art]);
$training_acc->load_relationship('accounts_trolc_trainingenolc_2');
$training_acc->accounts_trolc_trainingenolc_2->add($record[$art]);
$training_acc->load_relationship('accounts_trolc_trainingenolc_3');
$training_acc->accounts_trolc_trainingenolc_3->add($record[$art]);
$training_acc->save();
    echo "Training " . $art . " relationship Accounts established<br><br>";
}

Would love to get some feedback. Thanks!

Outcomes