Adding an App reference to an item
AnsweredTo 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!
-
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
-
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.
-
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
-
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.
-
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!
-
I'm sorry, but I have to ask you again if you looked at the examples in https://github.com/podio/podio-php/blob/master/examples/items.php ?
There are many good things in that file. Among other things multiple places where multiple values are being saved to a field in one go. Just pass an array of item_ids to set_value and you're golden.
-
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)); -
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;
} -
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 fieldNow 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.
-
Bikramjit, take a look at the .NET library. It might help you: https://developers.podio.com/clients/dotnet
-
As described at the very end up the page I linked you to ( https://developers.podio.com/clients/dotnet ) you can make raw requests to the Podio API if your method isn't built in.
-
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 -
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
-
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?
-
Tried this but it did not work:
$field = new PodioAppItemField(4215258);
$field->set_value(array(2)); // The item to add a reference toPodioItem::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,
))); -
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
Please sign in to leave a comment.
Comments
31 comments