Hi all,
I’m still working on importing users, accounts, and contacts from our iSeries into CRM. I can create the module members with no problem, by getting or creating beans and populating their properties, then saving. Relationships, though, don’t seem to be working. I really need a way of detecting errors when working with beans, but I can’t find one. How is this done?
I’m making a user account, then three accounts, then two contacts inside each account. Those two contacts are then related to their account, and the account is related to the user. But this always fails to work, as logging into that user’s account shows nothing under the accounts section. Making this much more difficult is that no errors or warnings are being logged anywhere. I need a way to detect errors, so if I try to load a non-existent relationship, for instance, I’m notified. Is that possible? Here’s the code I’m using in my PHP file to do all this, sans the actual query text, But believe me, the query works fine.
<?php if(isset($_POST["importUser"]) && isset($_POST["repID"])) { //yes, this IS set try { $getRepDetailsQuery = ""; //my query is here $getRepDetailsResults = $conn->prepare($getRepDetailsQuery); $getRepDetailsResults->bindParam(":repID", $_POST["repID"]); $getRepDetailsResults->execute(); $usedRepIDs = array(); //the query gets multiple accounts per user, so I'm avoiding re-creating users with this array while($row = $getRepDetailsResults->fetch(PDO::FETCH_ASSOC)) { if(!in_array($row["REPID"], $usedRepIDs)) { //add or update the rep as a CRM user echo "Have not yet encountered ID " . $row["REPID"] . ".
"; $usedRepIDs[] = $row["REPID"]; //so we don't repeat this user next time through the loop $userBean = BeanFactory::getBean("Users"); $userBean = $userBean->retrieve_by_string_fields(array("ad_salesperson_id_c", $row["REPID"])); if(is_null($userBean)) { $userBean = BeanFactory::NewBean("Users"); echo "No account found for rep " . $row["REPID"] . ". Creating new account.
"; } else { echo "Account found for rep " . $row["REPID"] . ".
"; } $userBean->first_name = $row["REPFIRSTNAME"]; $userBean->last_name = $row["REPLASTNAME"]; $userBean->user_name = $row["REPUSERNAME"]; $userBean->email1 = $row["REPEMAIL"]; $userBean->user_hash = md5("some_default_password"); $userBean->sugar_login = true; $userBean->ad_salesperson_id_c = $row["REPID"]; $userBeanID = $userBean->save(); } //try to add or update the account and its associated contacts $accountBean = BeanFactory::getBean("Accounts"); $accountBean = $accountBean->retrieve_by_string_fields(array("ad_customer_number_c"=>$row["CUSTOMERID"])); if(is_null($accountBean)) { $accountBean = BeanFactory::newBean("Accounts"); echo "Account for " . $row["CUSTOMERNAME"] . " does not exist. Creating it.
"; } $accountBean->ad_customer_number_c = $row["CUSTOMERID"]; $accountBean->name = $row["CUSTOMERNAME"]; $accountBean->load_relationship("assigned_user_link"); //I found this in vardefs, so it should be the right name to use $accountBean->assigned_user_link->add($userBean); $accountBeanID = $accountBean->save(); //we've just saved an account with a relationship to a user, but this never seems to work! //now save the contact(s) for this account $typeStrings = array("purchasing", "accounts payable"); foreach($typeStrings as $typeString) { $contactBean = BeanFactory::getBean("Contacts"); $contactBean = $contactBean->retrieve_by_string_fields(array("ad_customer_number_c"=>$row["CUSTOMERID"], "type_c"=>$typeString)); if(is_null($contactBean)) $contactBean = BeanFactory::newBean("Contacts"); $contactBean->last_name = $row["NAME"]; $contactBean->ad_customer_number_c = $row["ID"]; $contactBean->type_c = $typeString; if($typeString == "purchasing") { $contactBean->phone_work = $row["PURCHASING_PHONE"]; } elseif($typeString == "accounts payable") { $contactBean->work_phone = $row["AP_PHONE"]; } $contactBean->load_relationship("accounts"); $contactBean->accounts->add($accountBean); $contactBean->save(); } } } catch(exception $err) { echo "Error getting this rep's contacts:" . $err->getMessage(); }