cannot detect when account already exists using PHP

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

I think the way you’re building the “retrieve by string fields” array is wrong, compare with this working code here

So you’re not really fetching the existing userBeans…

When you post code here on the forums, put it inside the “code” tags, otherwise parts of it can be suppressed, namely brackets.

Thanks. I uncovered a couple serious problems, all of which could have been avoided if SC’s bean system included decent errors.

First, I used a comma at one point where I needed =>. I was therefore passing a two-value array to retrieve_by_string_fields where each value was null, rather than a single element including the value I wanted.

Second, my data type for ad_customer_number_c was, I thought, fine. However, SC kept inserting a number equal to the highest-possible value for a signed 32-bit integer. Converting that field to varchar fixed that problem.

Importing is still taking so long I had to extend the PHP timeout to over 20 minutes, but existing module member detection is at least working correctly. Thanks for continuing to offer suggestions. I’m sure I’ll be back with new problems soon, but I’m getting more comfortable with how beans work and what I have to watch for manually rather than relying on exceptions or errors to catch for me.