Hello all,
I’m using PHP to import user, account, and contact data from our iSeries. A key part of this is updating, not always inserting, data. If a rep’s name or territory changes, or a company gets a new phone number, I don’t want to duplicate the account but rather update it. Yet, I just can’t get this to work at all. I get new copies of everything each time I run the script, and I don’t know why.
In the below code, ad_sales_rep_id_c is a field I made (SC added the ‘_c’ as it does). I can query the database and find that this field is properly populated, so the data I’m checking for does seem to exist. Why, then, does my script not pick it up? The developer guide says retrieve_by_string_fields returns a populated bean if a match is found, or null. Is that no longer the case? Am I approaching all of this wrong? I feel like one key might be the difference between newBean and getBean, but I don’t know what that difference is. Both return a bean, but getBean has the option to populate a variable with the bean’s ID. Yet neither have any way to specify which bean you want, apart from the module, so how does getBean know the ID? Wouldn’t it and newBean really do the same thing, in the end? I’ve tried using both in my code, but it doesn’t seem to matter which I go with. Still, maybe it’s important?
Here’s the part to insert the users, but there’s more after that to handle accounts and contacts. If I can get users to work, though, I should be able to get the rest working. $row comes from a query that is definitely working correctly. Oh, and I’ve also tried this without the “users_cstm” part, but that didn’t matter.
while($row = $getRepDetailsResults->fetch(PDO::FETCH_ASSOC)) {
$userBean = BeanFactory::newBean("Users");
$userBean = $userBean->retrieve_by_string_fields(array("users_cstm.ad_sales_rep_id_c", $row["REPID"]));
if(is_null($userBean)) {
//add the rep as a CRM user
echo "<p>No account found for rep " . $row["REPID"] . ". Creating new account.</p>";
$GLOBALS["log"]->debug("Creating new user account for " . $row["REPFIRSTNAME"] . " " . $row["REPLASTNAME"]);
$userBean = BeanFactory::newBean("Users");
$userBean->first_name = $row["REPFIRSTNAME"];
$userBean->last_name = $row["REPLASTNAME"];
$userBean->user_name = $row["REPUSERNAME"];
$userBean->email1 = $row["REPEMAIL"];
$userBean->user_hash = md5("3fcxg12m");
$userBean->sugar_login = true;
$userBean->ad_sales_rep_id_c = $row["REPID"];
$userBeanID = $userBean->save();
}