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