Post

3 followers Follow
0
Avatar

Adding an App reference to an item

To add a new reference to another item, what function do I use?

Am I using:

PodioItemField::update( $item_id, $field_id, $attributes = array(), $options = array());

If so, can you help me figure out the structure of the attributes? Am I manually entering in the referenced items values?

Or is it:

PodioItem::update_reference( $item_id, array('type'=$referenced_item_app_id, 'id'=>$referenced_item_id));

This doesn't seem to work, but not sure if I'm using it correctly.

Thanks!

Davin Green Answered

Please sign in to leave a comment.

31 comments

0
Avatar

Maybe I can clarify...

Put simply, what's the best way to app an app reference entry to an item via php? Can you please point me to an example?

Davin Green 0 votes
0
Avatar

Hi Davin,

You have two options:

1) If you already have the item you want to add the field to you can create an instance of PodioAppItemField and then save the item. See https://github.com/podio/podio-php/blob/master/examples/items.php for a handful of basic examples with the item-related classes.

2) You can call the API directly using PodioItemField::update($item_id, $field_id, array($item_id_to_reference));

There's a tutorial on how to work with items here: https://developers.podio.com/examples/items -- this is where you can see the formatting for the $attributes array.

Does that help?

/Andreas

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Hey Andreas,

Thanks for the reply. Here's what is confusing me:

It makes the most sense that I would update the item field that relates to the app reference with a new referenced item ID like a relational database. 

Creating an instance of PodioAppItemField suggest that I have to mimic the entire structure of the item I am referencing in a new object and then update the item with that object as an attribute of the field value.

Whew, mouthful. Maybe I can just describe what I'm trying to do for some guidance:

 

We have a google apps script that takes any email we put in a specific label, and it sends that email to the email dropbox for our 'Interactions' app. The result is an app with recent emails as items, with the email header and body as one text field. The webhook script we're attempting to write gets that text field's value for each new item, looks for email addresses within the value, then compares those addresses with associated google contacts for items in the 'Organizations' app. We would then like it to associate the 'Interaction' item with the found 'Organization' item by creating an app reference to the 'Organization' item in the 'Interaction' item.

So far, we have the new 'Interaction' item's id, the 'Organization' items that have associated contact's ID, but we're lacking a way to connect the two.

The api documentation doesn't have any examples of adding an item reference to an app reference field. I think just looking at one would probably help us immensely. 

 

Thanks! We're all strugglers until jugglers.

Davin Green 0 votes
0
Avatar

BTW, we think this could be incredibly valuable for anyone using Podio and Google apps trying to make Podio a viable CRM. We plan on sharing our script on our blog as soon as it's workign for us.

Davin Green 0 votes
0
Avatar

Did you see the examples/items.php file I linked to above? It has many examples on how to work with instances of PodioItem and PodioItemField. I have taken care to write nice methods for setting values on item fields because the json format is a little complicated.

Item fields can still be a little tricky to work with since they require a link to the item they belong to in order to be saved. I'll make it nicer in the future, but it's still not complicated. If you have an app reference field with the id of '123' you literally just have to do:

$field = new PodioAppItemField(123);

$field->set_value(456); // Provided the item_id you want to add a reference to is 456

PodioItemField::update($your_item_id, 123, $field->as_json(false));

You don't need to fetch or build the entire item you are referencing since all you need to create the reference is the item_id.

/Andreas

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Perfect, Andreas. 

Thank you for taking the time to give an example. That's the format I suspected, but wasn't quite sure how to execute it.

I'd love to share our script with when we're done to showcase or use to help others with similar needs. 

Davin Green 0 votes
0
Avatar

Using the script you gave, I'm getting an error I can't resolve.

Fatal error: Uncaught exception 'PodioBadRequestError' with message 'Invalid field 24659305 ('value'): Invalid value 34145652L: Must be basestring

Here's the code (minus auth)

$item_id = 31681034;

$field_id = 24659305;

$field = new PodioAppItemField($field_id);

$field->set_value($organization->item_id); // The item to add a reference to

PodioItemField::update($item_id, $field_id, $field->as_json(false));

 

Is it telling me that I gave it a bad item_id? Cause I've double checked that. It looks like it's not expecting an object, but only a string value.... not sure.

Davin Green 0 votes
0
Avatar

Hey Andreas,

You were absolutely right. I was calling the wrong field_id.

The script is now working, but I've noticed that using the set_value function of ItemFields will overwrite app references. I've been trying to explore a way to add the values of any app references in the field value to my updated values, or append the value (to create multiple app item references).

The script creates a list of 'Opportunities' that the 'Interaction' is related to, the creates the app references in the 'Related Opps' field of 'Interations'. As it works now, only the last app reference to iterate is saved, implying the previous iterations are being overwritten.

How can we create multiple app references with the set_value function?

 

Thanks!

Davin Green 0 votes
0
Avatar

Hey Andreas,

Passing just the item ID's in an array worked fine. I have been referencing the item examples, just trying to clarify them. The examples require a lot of assumptions if you aren't doing exactly what they represent. perhaps it would be helpful to have more permutations of them using different use-cases. Either way, your input has been invaluable in understanding the examples.

 

Why did I create a PodioItemField object if all I needed was the id's?

Ex:

$org_ref []= $organization->item_id;

PodioItemField::update($item_id, 24659306, $org_ref);

VS

$field = new PodioAppItemField($field_id);

$field->set_value($organization->item_id);

PodioItemField::update($item_id, $field_id, $field->as_json(false));

Davin Green 0 votes
0
Avatar

Because the fields will be returned as PodioItemField instances when you pull it out of Podio. If you get in the habit of using the classes you get a unified experience when working with the API. :)

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Ah, so those objects are actually embedded in the item they are referenced by? I assumed the objects were pulled separately based on the item id within the reference field. 

Davin Green 0 votes
0
Avatar

There are many nice things about these classes. You can see in the source for the PodioAppItemField class that it has a method called 'item' which will return an array of PodioItem instances of the current values: https://github.com/podio/podio-php/blob/master/models/PodioItemField.php#L318-L335

So you can do things like:

// Get an item

$item = PodioItem::get($item_id);

// If the item has an app reference field with an external_id of 'reference-field' we get the field

$field = $item->field('reference-field');

// Now we work on the array of referenced items. E.g. loop over them and print the title

foreach ($field->items() as $reference) {

// $reference is an instance of PodioItem so you have many properties and methods available

print $reference->title;

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Hi Davin,

I want to add app in podio. I am using C#. Please help on this.

Currently I am using RestSharp, but no luck.

 

Thanks

Bikramjit Singh 0 votes
0
Avatar

Davin,

I am also using GAS to communicate with podio.

 I am also updating a field with new app references in GAS like this:

fieldIDmx[1] is a variable containing the external_id, I have made a getField function which looks in the ["fields"] array for a field with a certain ID and returns the corresponding array KEY. Podio API doesn't return empty fields, so when nothing is filled in referencing to a KEY by number will go wrong bigtime. 

We are adding an app reference to a field when a certain mail is sent.

 

var newsentmails = new Array();

//CHECK IF THE APP REFERENCE ALREADY EXISTS

var mfiel = getField(podioresponse["fields"],fieldIDmx[1]); // Kijk of M-veld reeds bestaat

var checkm = false;

if (mfiel) {

var sentmails = participants[p]["fields"][getField(response["fields"],fieldIDmx[1])]["values"]; // if it exists, get all values of that field

Now we create a new array with the existing values:

for (var s=0; s<sentmails.length; s++) {

newsentmails.push(new_referenceId);

}

 

You can then use newsentmails.push(new_referenceId) to add new fields to the array, and POST this array to podio. Now the old fields are overwritten, but overwritten with the same values so in practice, this works.

 

 

 

Callserver 0 votes
0
Avatar

Davin,

I am using the same .net library, but I does not have any method to create an app. That is why I switched to rest calls, but still no luck to create.

Please help me on this.

 

Thanks

Bikramjit Singh 0 votes
0
Avatar

Hey Andreas,

I know I need to switch to the newer api, but just a quick question... 

Is this API class only available in the new PHP Api?

PodioAppItemField

thanks... 

Patrick Steil

http://www.podiotools.com 

Patrick Steil 0 votes
0
Avatar

Yes, only in the new version. Wanting to implement it was the entire motivation for the rewrite as I couldn't implement that functionality in the old setup (so I wouldn't try to backport it...)

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Andreas,

Related to the topics in this discussion...  having trouble with this code:

In one function previous to the following code, I do an App Auth to my "Organizations" App, then I later call a function that does the following: ( I have checked my debug log and this is the only Auth happening before I run this code).

$field = new PodioAppItemField($field_id);

$field->set_value(array($reference_to_item_id)); // The item to add a reference to

$obj = PodioItemField::update($item_id, $field_id, $field->as_json(false));

When I run this code for me, all works well, but when a user who is not a member of the workspace where my "Organizations" app is runs this code, it chokes with a message like:

The user with id 964532 does not have the right view on space with id 642820

The thing is I am using App Auth, so I wouldn't have thought it should care abou the user... any ideas on what I am doing wrong?

Thanks,

Patrick

Patrick Steil 0 votes
0
Avatar

Well, to be more specific, I am creating a reference between two apps in two different workspaces... 

I added an Auth so that I Auth both Apps... is that the right way to do this?

Need to test for a user in another Org... 

Patrick Steil 0 votes
0
Avatar

I cannot get this to work for users who are not in my Org... 

What is the proper way to provide Auth when adding a reference to another App in another workspace?

Thanks... 

Patrick Steil 0 votes
0
Avatar

Hi Patrick,

While the error message mentions a user, it's the same error message when you have authenticated as an app and you're trying to access data the app doesn't have access to (in that case the "user" refers to the app, which is acting as a user more or less).

Can you create an absolute minimal, standalone example that illustrates what you're trying to do? I know you have a rather complex auth setup and it would be nice to be able to rule out the trivial case where you are authenticated as a user and trying to reference an item that user doesn't have access to.

/Andreas

Andreas Haugstrup Pedersen 0 votes
0
Avatar

Do I have to do update to add a reference to an item. What if I'm creating a new record using:

PodioItem::create(5498322, array('fields' => array(

"title" => "SEO package2",

"date-of-sale" => array("start" => "2011-05-06 11:27:20", "end" => "2014-05-09 11:27:20"),

"value" => array("value" => "123.00","currency"=>"USD"),

"amount-paid" => array("value" => "66.00","currency" => "USD"),

"product-or-service" => array(1, 2),

)));

Where product-or-service is an app reference field to another app. How can I just create a new record and have the app reference value be  a part of that new record creation?

Kapil Pershad 0 votes
0
Avatar

Tried this but it did not work:

 

$field = new PodioAppItemField(4215258);

$field->set_value(array(2)); // The item to add a reference to

 

PodioItem::create(5498322, array('fields' => array(

"title" => "SEO package2",

"date-of-sale" => array("start" => "2011-05-06 11:27:20", "end" => "2014-05-09 11:27:20"),

"value" => array("value" => "123.00","currency"=>"USD"),

"amount-paid" => array("value" => "66.00","currency" => "USD"),

"product-or-service" => $field,

)));

 

 

Kapil Pershad 0 votes
0
Avatar

Hi Kapil,

You first example of just using an array is correct. However, you must remember that the ids you use should be item_ids and not the much shorter app_item_id from the browser's URL. To get the item_id of an item you must go to that item and click on the little cloud icon on the right hand side of the grey bar at the top of the item.

All the best,

Andreas

Andreas Haugstrup Pedersen 0 votes