Post

1 follower Follow
0
Avatar

Problem with contacts

Hi,

i have written a script that uses my account auth to change the contact on a couple of customers. Strangely i get the following error:

 

The user with id 1009728 does not have the right view on profile with id 1037122

 

Even though i can change the manually myself, what am i doing wrong?

Carl Cedergren

Please sign in to leave a comment.

18 comments

0
Avatar

It looks like this:

 

$customers = PodioItem::filter(2382740,array('limit'=>50,'offset'=>$i*50));

$customers = $customers['items'];

foreach ($customers as $customer) {

  $sales=trim(strip_tags($customer->field('sales')->attributes['values'][0]['value']));

  $sales_id=$users[$sales];

}

$customer->fields=array(new PodioTextItemField('sales-agent'));

$customer->field('sales-agent')->set_value($sales_id);

$customer->save();

Carl Cedergren 0 votes
0
Avatar

i just realised that PodioTextItemField is wrong, do i really have to create it since its empty, i can't tell podio to include empty fields in the response?

Carl Cedergren 0 votes
0
Avatar

What is it you're trying to do? I'm pretty sure your code will not do whatever you're trying to do. You have the final three lines outside of your loop so each customer item will not be saved. And on the third-to-last line you override all field values on the item -- are you trying to add a field to the item?

Are you migrating values from a text field into a contact field?

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Yes, i am trying to migrate data from a text field to a contact field, the last lines are inside the loop, it was a cut n paste error. I am adding the sales-agent field since its currently empty and therefor not included in the respins

Carl Cedergren 0 votes
0
Avatar

I'm in California so I sleep when you are awake :)

The sales_id -- you are certain this is a profile_id? In Podio users have both a user_id and a profile_id. The contact field only accepts profile_ids.

A few other random tips:

  • You don't have to access 'attributes' like that. In your case you'll get the exact same result from: $sales=trim($customer->field('sales')->humanized_value());
  • If you want to save some bytes and keep things simpler you can call save() directly on the ItemField. Then only that field value will be sent to the API and you don't have to worry about accidentally updating something you didn't intend to: $customer->field('sales-agent')->save();
Andreas Haugstrup Pedersen 0 votes
0
Avatar

Hi,

 

thanks for the pointers, i have switched to the profile_id and i use the humanized_value. Stil when i do:

$customer->field('sales-agent')->set_value($sales_id);

$customer->field('sales-agent')->save();

the second line throws:

The user with id 1009728 does not have the right view on profile with id 1037122

Carl Cedergren 0 votes
0
Avatar

Hi Carl

It is very hard to figure out what you are doing wrong. We use the API from our frontend, mobile apps and so on, so we know 100% sure that it works on our end.

Christian

Christian Holm 0 votes
0
Avatar

Could you perhaps try the code? It looks like this (sales-agent is a contact field and sales is a text field):

 

require_once '../podio-php/PodioAPI.php';

Podio::$debug = true;

Podio::setup(CLIENT_ID, CLIENT_SECRET);

Podio::authenticate('password', array('username' => MY_USERNAME, 'password' => MY_PASSWORD));

$users=array();

try {

  $org=PodioOrganization::get_all();

  $org_id=$org[0]->org_id;

  $contacts=PodioContact::get_all();

  foreach ($contacts as $contact) {

    $users[$contact->name]=$contact->profile_id;

  }

  echo count($users)." users loaded\n";

  for ($i=0;$i<50;$i++) {

    $customers = PodioItem::filter(MY_APP_ID,array('limit'=>50,'offset'=>$i*50));

    $customers = $customers['items'];

    foreach ($customers as &$customer) {

      $sales=trim(strip_tags($customer->field('sales')->humanized_value()));

      if (array_key_exists($sales,$users)) {

        $sales_id=$users[$sales];

        $customer->field('sales-agent')->set_value($sales_id);

        $customer->field('sales-agent')->save();

      }

    }

  }

} catch (PodioError $e) {

  print $e->body['error_description'];

}

Carl Cedergren 0 votes
0
Avatar

Hi Carl

It does look like it is the wrong profile_id, but I can't really understand why that would be the case. The alleged profile_id is indeed a user_id for a user in the same organization as you. Can you check that it is failing with the exact same error message, and not on another id?

 

Christian

Christian Holm 0 votes
0
Avatar

This still fails though, on every entry where sales-agent is blank: 

$customer->field('sales-agent')->set_value($sales_id);

Carl Cedergren 0 votes
0
Avatar

Hey Carl,

$sales_id is probably a blank string, so you're trying to save a blank string into a place where a numeric id would go. On your end you should make sure that $sales_id is not a blank string. I have it on my list to make the client a little smarter when it comes to null values, but for now you have to handle it.

/Andreas

Andreas Haugstrup Pedersen 0 votes