Adding a relationship

hello everyone, I have the following problem, with beans, I can create records, but the relationship with l
the father record goes wrong …


//punto di ingresso per recupero dati..
		//Create a new demo contact
		
		$bean->load_relationship('Pri_PreventivoRighe');
		$contactBean = BeanFactory::newBean('Pri_PreventivoRighe');
		//Unlink the contact from the account - assumes $contactBean is a Contact SugarBean		
		//$contactBean->Pri_PreventivoRighe->delete($contactBean->id,'');

		
                $contactBean->name = $value1['assicurazione'];
		$contactBean->assicurazione_c = $value1['assicurazione'];
		$contactBean->numerorate_c = $value1['numeroRate'];
		$contactBean->taeg_c = $value1['TAEG'];
		$contactBean->tan_c = $value1['TAN'];
		$contactBean->teg_c = $value1['TEG'];
		$contactBean->banca_c = $value1['banca'];
		$contactBean->capitalelordo_c = $value1['capitaleLordo'];
		$contactBean->commissionevenditore_c = $value1['commissioniVenditore'];
		$contactBean->contobanca_c = $value1['contoBanca'];
		$contactBean->importorata_c = $value1['importoRata'];
		$contactBean->nettoricavo_c = $value1['nettoRicavo'];
		$contactBean->premioimpiego_c = $value1['premioImpiego'];
		$contactBean->premiovitae_c = $value1['premioVitae'];
		$contactBean->speseistruttorie_c = $value1['speseIstruttorie'];
		$contactBean->totaleassicurazione_c = $value1['totaleAssicurazione'];
		$contactBean->messaggierrore_c = $value1['mesaggiErrore'];
		$contactBean->save();

		$bean->Pri_PreventivoRighe->add($contactBean);

i have this error??


[Wed Jan 09 12:31:47.859981 2019] [php7:warn] [pid 114192] [client 192.168.1.115:59741] PHP Warning:  mysqli_real_escape_string() expects parameter 2 to be string, array given in /var/www/html/Kredias/include/database/MysqliManager.php on line 290, referer: http://192.168.1.102:8085/Kredias/index.php?action=ajaxui
[Wed Jan 09 12:31:48.048700 2019] [php7:notice] [pid 114192] [client 192.168.1.115:59741] PHP Notice:  Undefined property: prv_GestionePreventivi::$Pri_PreventivoRighe in /var/www/html/Kredias/custom/modules/prv_GestionePreventivi/generaPreventivo.php on line 172, referer: http://192.168.1.102:8085/Kredias/index.php?action=ajaxui
[Wed Jan 09 12:31:48.049765 2019] [php7:error] [pid 114192] [client 192.168.1.115:59741] PHP Fatal error:  Uncaught Error: Call to a member function add() on null in /var/www/html/Kredias/custom/modules/prv_GestionePreventivi/generaPreventivo.php:172\nStack trace:\n#0 /var/www/html/Kredias/include/MVC/View/SugarView.php(818): include_once()\n#1 /var/www/html/Kredias/include/MVC/View/views/view.classic.php(69): SugarView->includeClassicFile('custom/modules/...')\n#2 /var/www/html/Kredias/include/MVC/View/SugarView.php(211): ViewClassic->display()\n#3 /var/www/html/Kredias/include/MVC/Controller/SugarController.php(435): SugarView->process()\n#4 /var/www/html/Kredias/include/MVC/Controller/SugarController.php(375): SugarController->processView()\n#5 /var/www/html/Kredias/include/MVC/SugarApplication.php(113): SugarController->execute()\n#6 /var/www/html/Kredias/index.php(52): SugarApplication->execute()\n#7 {main}\n  thrown in /var/www/html/Kredias/custom/modules/prv_GestionePreventivi/generaPreventivo.php on line 172, referer: http://192.168.1.102:8085/Kredias/index.php?action=ajaxui
[Wed Jan 09 12:31:51.323367 2019] [php7:notice] [pid 112930] [client 192.168.1.115:59742] PHP Notice:  Undefined index: LBL_SUBTHEME_OPTIONS_Kredias in /var/www/html/Kredias/themes/SuiteP/themedef.php on line 79, referer: http://192.168.1.102:8085/Kredias/index.php?module=prv_GestionePreventivi&action=generaPreventivo&record=

someone could give me a hand, thanks in advance to everyone

Make sure you have a loaded $bean of other side’s module in Bean
Like :
$bean = BeanFactory::getBean(“yourcustommodule”,“module-id”);

1 Like

Hi , i have declared… in this way:

$bean = BeanFactory::getBean(‘prv_GestionePreventivi’, $beanId);

Strange! Make sure you have a correct relationship name here.

$bean->load_relationship(‘Pri_PreventivoRighe’);

$bean->Pri_PreventivoRighe->add($contactBean);


<?php 
 

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

ob_start();



//API Url
//Initiate cURL.
$ch = curl_init();

//uRL DI CONNESSIONE ALLA BANCA
$url = 'http://31.169.104.201/WSPreventiviBPRO/WebService/WSPreventivoVeloce.svc/CalcolaPreventiviMultipli/json';

//iD DI PREVENTIVO
$beanId="1e6fc77f-925a-4f3f-1c51-5bf67d5dd4cf";


$bean = BeanFactory::getBean('prv_GestionePreventivi', $beanId);


$tipicontratto1_c = substr($bean -> tipicontratto1_c, -3); 

$annonascita = substr($bean -> datanascita_c, -4); 
$mesenascita = substr($bean -> datanascita_c, 3,2); 
$giornonascita = substr($bean -> datanascita_c, 0,2); 

$annonascita_my = $annonascita . "-".$mesenascita."-".$giornonascita ;


$annoassunzione = substr($bean -> dataassunzione_c, -4); 
$meseassunzione = substr($bean -> dataassunzione_c, 3,2); 
$giornoassunzione = substr($bean -> dataassunzione_c, 0,2); 

$annoassunzione_my = $annoassunzione . "-".$meseassunzione."-".$giornoassunzione ;



$annodecorrenza = substr($bean -> datadecorrenza_c, -4); 
$mesedecorrenza = substr($bean -> datadecorrenza_c, 3,2); 
$giornodecorrenza = substr($bean -> datadecorrenza_c, 0,2); 

$annodecorrenza_my = $annodecorrenza . "-".$mesedecorrenza."-".$giornodecorrenza ;


$stipendionetto_my = str_replace(",","",$bean -> stipendionetto_c);

//The JSON data.
$jsonData = array(
    "username" => "zzzzz",
    "password" => "ssssss",
    'codiceQualifica' => $bean -> qualifiche_c,
    'codiceTipoContratto' => $tipicontratto1_c,
    'codiceTipoImpiego' => $bean ->tipoimpiego_c,
    'numeroRate' => $bean -> numerorate_c,
    'importoRata' => $bean -> importorata_c,
    'percentualeProvvigione' => $bean -> percentualeprovvigione_c,
    'dataNascita' => $annonascita_my,
    'dataAssunzione' => $annoassunzione_my,
    'sesso' => $bean -> sesso_c,
    'stipendioNetto' => '5000.00',
    'dataDecorrenza' => '',
    'convenzione' => '',
    'RinnovoPrestitoPrecedente' => $bean -> rinnovoprestitoprecedente_c,
    'ImportoRataPrestitoPrecedente' => $bean -> importorataprestitoprecedent_c,
    'RateResiduePrestitoPrecedente' => $bean -> rateresidueprestitoprecedent_c,
    'flagIPA' => $bean -> flagipa_c,
    'codiceFormaGiuridica' => $bean -> formegiuridiche_c);




//echo xmlrpc_encode($jsonData);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);


curl_setopt($ch, CURLOPT_URL, "$url");

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));

//impone a curl di inserire il risultato in una stringa 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Execute the request
$result = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);


//echo  var_dump(json_decode($result),true);
$data= json_decode($result, true);

//print_r($data);



foreach ( $data as $key => $value )
            {
                echo $key . ' => ' . $value . '<br />';

	foreach ( $value as $key1 => $value1 )
            {
                echo $key1 . ' => ' . $value1 . '<br />';
		

		//punto di ingresso per recupero dati..
		//Create a new demo contact
		
		$bean->load_relationship('Pri_PreventivoRighe');
		$contactBean = BeanFactory::newBean('Pri_PreventivoRighe');
		//Unlink the contact from the account - assumes $contactBean is a Contact SugarBean		
		//$contactBean->Pri_PreventivoRighe->delete($contactBean->id,'');

		

                $contactBean->name = $value1['assicurazione'];
		$contactBean->assicurazione_c = $value1['assicurazione'];
		$contactBean->numerorate_c = $value1['NumeroRate'];
		$contactBean->taeg_c = $value1['TAEG'];
		$contactBean->tan_c = $value1['TAN'];
		$contactBean->teg_c = $value1['TEG'];
		$contactBean->banca_c = $value1['banca'];
		$contactBean->capitalelordo_c = $value1['capitaleLordo'];
		$contactBean->commissionevenditore_c = $value1['commissioniVenditore'];
		$contactBean->contobanca_c = $value1['contoBanca'];
		$contactBean->importorata_c = $value1['importoRata'];
		$contactBean->nettoricavo_c = $value1['nettoRicavo'];
		$contactBean->premioimpiego_c = $value1['premioImpiego'];
		$contactBean->premiovitae_c = $value1['premioVitae'];
		$contactBean->speseistruttorie_c = $value1['speseIstruttorie'];
		$contactBean->totaleassicurazione_c = $value1['totaleAssicurazione'];
		$contactBean->messaggierrore_c = $value1['mesaggiErrore'];
		$contactBean->save();
		
		$bean->Pri_PreventivoRighe->add($contactBean);
		

            }


            }

?>

is very strange…

Still, I doubt on relationship name. do load_relationship returns true?

If not add me on skype. I need to check your file system.
Our skype id is urdhvatech

Hi, the rows is correct created, and i can select from the list, but i need to add automaticly the rows affected.

ok it work thanks… :slight_smile:


echo "Load:" . $bean->load_relationship('prv_gestionepreventivi_pri_preventivorighe_1');
		
		//Unlink the contact from the account - assumes $contactBean is a Contact SugarBean		
		//$contactBean->Pri_PreventivoRighe->delete($contactBean->id,'');

		

                $contactBean->name = $value1['assicurazione'];
		$contactBean->assicurazione_c = $value1['assicurazione'];
		$contactBean->numerorate_c = $value1['NumeroRate'];
		$contactBean->taeg_c = $value1['TAEG'];
		$contactBean->tan_c = $value1['TAN'];
		$contactBean->teg_c = $value1['TEG'];
		$contactBean->banca_c = $value1['banca'];
		$contactBean->capitalelordo_c = $value1['capitaleLordo'];
		$contactBean->commissionevenditore_c = $value1['commissioniVenditore'];
		$contactBean->contobanca_c = $value1['contoBanca'];
		$contactBean->importorata_c = $value1['importoRata'];
		$contactBean->nettoricavo_c = $value1['nettoRicavo'];
		$contactBean->premioimpiego_c = $value1['premioImpiego'];
		$contactBean->premiovitae_c = $value1['premioVitae'];
		$contactBean->speseistruttorie_c = $value1['speseIstruttorie'];
		$contactBean->totaleassicurazione_c = $value1['totaleAssicurazione'];
		$contactBean->messaggierrore_c = $value1['mesaggiErrore'];
		$contactBean->save();
		
		$bean->  prv_gestionepreventivi_pri_preventivorighe_1->add($contactBean);