How to detect errors when working with beans

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(); }

You can find some sample code here

https://suitecrm.com/forum/suitecrm-7-0-discussion/16182-basics-of-importing-accounts-contacts-users#55170

From a quick look, I’d say your problem is you should be saving the new bean first, and only then adding the relationship.

1 Like

Thanks, saving first was the key. I figured I would set the properties, set the relationships, and do everything else and only then save. But I guess relationships need existing data to work with, so if I don’t save a bean, there’s nothing to relate?

More generally, is there a way to detect errors? One example: when I was working out how to create users, before I found the studio > module > fields to list all the field names, I was trying to use email_address. That’s obviously the wrong field name, but my code ran. Another example: I made a user, but neglected to include an email address at all, not realizing it is a required field. The user got created despite missing necessary information. Users can be created with duplicate usernames, too, all without any warning I can pick up on in my code. The log may have had something, but can I not catch an exception, examine some kind of SC error object, or do something else to detect errors in PHP?

I am originally a C/C++ and yes, I find PHP very sloppy and loose.

Anyway, part of the issue here is that at the bean level, not much validation is supposed to happen. It’s meant to be a “freer” level of manipulation. The programmer needs to take care. I don’t know if the REST API offers more validation, but I don’t know, I never used it.

Things you can do to mitigate:

  • work from code samples, if you can find them

  • list bean class members directly from PHP with var_dump or print_r

  • check the return of functions, sometimes a null will tell you it didn’t work.

  • increase logging level to DEBUG in Admin / System Settings