Problem with contacts

Comments

18 comments

  • Carl Cedergren

    Same problem/error if i auth as the app that owns the items?

    0
    Comment actions Permalink
  • Andreas Haugstrup Pedersen

    What API calls are you making?

    0
    Comment actions Permalink
  • Carl Cedergren

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

    0
    Comment actions Permalink
  • Carl Cedergren

    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?

    0
    Comment actions Permalink
  • Andreas Haugstrup Pedersen

    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?

    0
    Comment actions Permalink
  • Carl Cedergren

    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

    0
    Comment actions Permalink
  • Carl Cedergren

    Anybody?

    0
    Comment actions Permalink
  • Andreas Haugstrup Pedersen

    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();
    0
    Comment actions Permalink
  • Carl Cedergren

    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

    0
    Comment actions Permalink
  • Andreas Haugstrup Pedersen

    Did you see my comment above about user_ids versus profile_ids?

    0
    Comment actions Permalink
  • Carl Cedergren

    Yes, i have changed to using the profile_id, i still get the error.

    0
    Comment actions Permalink
  • Carl Cedergren

    Still can't get this to work, please advice.

    0
    Comment actions Permalink
  • Christian Holm

    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

    0
    Comment actions Permalink
  • Carl Cedergren

    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'];

    }

    0
    Comment actions Permalink
  • Christian Holm

    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

    0
    Comment actions Permalink
  • Carl Cedergren

    Solved it, my default case used profile_id instead of user_id, now it works!

    0
    Comment actions Permalink
  • Carl Cedergren

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

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

    0
    Comment actions Permalink
  • Andreas Haugstrup Pedersen

    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

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk