Post

4 followers Follow
0
Avatar

PHP How to get all active task for user, which assigned as resource

Hi, All!

I make PHP application with clarizen integration. And i try to get all tasks for user, which assigned as resource for this tasks. But i cant find information how i can do that. Where i can find example query like this? I read documentation and forums topics - but cant find any example for that.

Please, help me.

Yury Answered

Please sign in to leave a comment.

19 comments

0
Avatar

Hi Yuri.

Basically you have 2 options:

  1. Use EntityQuery on the RegularResourceLink with an appropriate condition on the Resource field.
  2. Use WorkItemQuery specifying UserIds.

Option 1 is available in all flavors of the API (SOAP, REST and CZQL).

For similar 'how to's you may want to consult the Clarizen data model (http://usermanual.clarizen.com/15-4-3-1-data-model).

The 'Settings -> Configure' page in Clarizen lists all Clarizen entities with API field names (unfortunately the API entity names are not present. You may use the various Metadata API calls to get detailed information).

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

Hi Ophir.

Thank you for your answer.

But link do not work :-(. And .. can you give me any example for option 2.  I would like create only one query for get all Task fields for current User. For example ( 'Name','Project.name','Priority','TrackStatus','Importance','TaskType',

'State','Manager.DisplayName','C_ReviewStatus','StartDate','DueDate')

I was try get Resource as field, but api say "this field do not exist".

How i can set relation field for condition? I use PHP and REST or SOAP.

Thank you for your attention.

Yury 0 votes
0
Avatar

Here is my request:

Array

(

    [Requests] => Array

        (

            [0] => Array

                (

                    [url] => data/entityQuery

                    [method] => POST

                    [body] => Array

                        (

                            [typeName] => RegularResourceLink

                            [fields] => Array

                                (

                                    [0] => Workitem.Name

                                    [1] => Workitem.Project.name

                                    [2] => Workitem.Priority

                                    [3] => Workitem.TrackStatus

                                    [4] => Workitem.Importance

                                    [5] => Workitem.TaskType

                                    [6] => Workitem.State

                                    [7] => Workitem.Manager.DisplayName

                                    [8] => Workitem.C_ReviewStatus

                                    [9] => Workitem.StartDate

                                    [10] => Workitem.DueDate

                                )

                            [where] => Array

                                (

                                    [_type] => And

                                    [conditions] => Array

                                        (

                                            [0] => stdClass Object

                                                (

                                                    [_type] => Compare

                                                    [LeftExpression ] => stdClass Object

                                                        (

                                                            [_type] => FieldExpression

                                                            [FieldName] => Resource

                                                        )

                                                    [Operator] => In

                                                    [RightExpression] => stdClass Object

                                                        (

                                                            [_type] => QueryExpression

                                                            [Query] => stdClass Object

                                                                (

                                                                    [_type] => EntityQuery

                                                                    [TypeName] => User

                                                                    [Where] => stdClass Object

                                                                        (

                                                                            [_type] => Compare

                                                                            [LeftExpression] => stdClass Object

                                                                                (

                                                                                    [_type] => FieldExpression

                                                                                    [FieldName] => DisplayName

                                                                                )

                                                                            [Operator] => Contains

                                                                            [RightExpression] => stdClass Object

                                                                                (

                                                                                    [_type] => ConstantExpression

                                                                                    [Value] => stdClass Object

                                                                                        (

                                                                                            [_type] => EntityId

                                                                                            [TypeName] => State

                                                                                            [Value] => fedyay

                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

 

I was send it  by CURL. And get the next error message:

 

{#233 ▼

  +"responses": array:1 [▼

    0 => {#234 ▼

      +"statusCode": 500

      +"body": {#235 ▼

        +"errorCode": "Internal"

        +"message": "An internal error has occurred, the error was logged and will be examined."

        +"referenceId": "pZpWtNZEOwmA0azSNaFG9"

      }

      +"headers": []

    }

  ]

}

Can you help me?

 

Yury 0 votes
0
Avatar

Hi Yuri,

The inner right expression doesn't make sense. If you use a_ Contains_ operator the right expression should be a string, not an EntityId.

Usually if you need to filter for specific resources you're better off using the resource ID's instead. If you have those, you don't even need the In condition, you can use the Resource field directly. 

The data model link should be in http://usermanual.clarizen.com/data-model (they must have changed it since I first posted)

As for Option #2 (WorkItemsQuery), I think the resource itself is not returned, it's just a filter, so the resource link entity query may be a better option for you.

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

Hi Ophir,

Here is my modified query:

{

    "Requests": [

        {

            "url": "data\/entityQuery",

            "method": "POST",

            "body": {

                "typeName": "RegularResourceLink",

                "fields": [

                    "Workitem.Name",

                    "Workitem.Project.name",

                    "Workitem.Priority",

                    "Workitem.TrackStatus",

                    "Workitem.Importance",

                    "Workitem.TaskType",

                    "Workitem.State",

                    "Workitem.Manager.DisplayName",

                    "Workitem.C_ReviewStatus",

                    "Workitem.StartDate",

                    "Workitem.DueDate",

                    "Resource.DisplayName"

                ],

                "where": {

                    "_type": "and",

                    "conditions": [

                        {

                            "LeftExpression ": {

                                "_type": "FieldExpression",

                                "FieldName": "Resource"

                            },

                            "Operator": "Equal",

                            "RightExpression": {

                                "_type": "ConstantExpression",

                                "Value": {

                                    "_type": "EntityId",

                                    "TypeName": "User",

                                    "Value": "4ad8ab8b-f2cc-48be-83c2-db93bbebd22d"

                                }

                            }

                        }

                    ]

                }

            }

        }

    ]

}

I was sent it to "bulk/execute";

And .. receive the next message:

 

{#231 ▼

  +"statusCode": 500

  +"body": {#232 ▼

    +"errorCode": "Internal"

    +"message": "An internal error has occurred, the error was logged and will be examined."

    +"referenceId": "24DJx5GBMWJzvKQB9SJlDs"

  }

  +"headers": []

}

What i was done incorrect?

 

Can i view anywhere  detail description of request error? By referenceId for example.

And may be exist some PHP library  for work with clarizen? :-)

Thank you for you attention.

With best regards, Yuri.

Yury 0 votes
0
Avatar

When i remove any condition - all work properly. please help me build correct condition.

Yury 0 votes
0
Avatar

Hi Yuri,

You didn't specify the condition as a Compare condition. (_type=Compare). This is what caused the error.

By the way, you don't have to use the And condition if you have only 1 condition, the where can be a simple Compare condition directly. 

Some API errors results will be returned with a specific message, but a wrongly composed REST message may cause an exception. In general, details of exceptions only appear in Clarizen's internal logs, a common security practice. 

We don't have specialized Clarizen PHP libraries.

As all REST APIs are inherently freeform, one can construct REST messages incorrectly.

I supposed with some practice and assistance if required you get the hang of it and it becomes a non issue.

Other technologies may be more assistive as far as message constructing is concerned:

  1. Microsoft.NET + SOAP API, builds a proxy class with strongly typed classes and properties, which makes it a lot easier.
  2. As Eyal suggested, CZQL offers a powerful and well formed query language you may find easier to use.

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

Thanks for your answers. I stil can`t create query for select task only for one user :-)

Id like use CZQL but i cant set session Id coockie :-(. After login i was receive session Id nad user id. And try set session id

as coockie. What the name of this coockie i must set?

 

 

Here is my PHP code:

 

 $vars =   [

            'q' => 'SELECT userName, email, firstName, lastName FROM Users WHERE id = '. $query->id,

            'parameters' => array(),

            'paging' => array()

            ];

        $json = is_array($vars) ? json_encode($vars) : $vars;

        $curl_handler = curl_init( "https://api.clarizen.com/V2.0/services/data/query" );

        $options = array(

            CURLOPT_RETURNTRANSFER => true,     // return web page

            CURLOPT_FOLLOWLOCATION => true,     // follow redirects

            CURLOPT_ENCODING       => "",       // handle all encodings

            CURLOPT_AUTOREFERER    => true,     // set referer on redirect

            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect

            CURLOPT_TIMEOUT        => 120,      // timeout on response

            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects

            CURLOPT_CUSTOMREQUEST  => "POST",

            CURLOPT_POSTFIELDS     => $json,    // some post fields

        CURLOPT_COOKIE        => "SUSID=".$this->sessionId.";",

            CURLOPT_HTTPHEADER     => array(

                'Content-Type: application/json',

                'Content-Length: ' . strlen( $json )

            ),

        );

        curl_setopt_array( $curl_handler, $options );

        $response = curl_exec( $curl_handler );

        curl_close( $curl_handler ); // GC

        return json_decode( $response );

 

 

 

 

Yury 0 votes
0
Avatar

Thanks a lot!

Everything works well! I use rest  API and CZQL.

I very grateful for your answers.

With best regards, Yuri

Yury 0 votes
0
Avatar

Yuri,

Would you mind sharing your CZQL query for this? I'm trying to do the same thing now. I just got my PHP REST 2.0 code to connect and pull Tasks, but I haven't gotten it to do it for only a single user.

Thanks!
Jeff

Marketing Mojo IT 0 votes
0
Avatar

Hi Jeff,
You're welcome to post your current code and issue separately and we'll be happy to assist if Yuri doesn't get ahead of us...
Ophir

Ophir Kenig 0 votes
0
Avatar

Ophir,

I actually got it solved late yesterday. I took your advice some above and queried the resource link instead of the task or user. Here's the CZQL I'm using:

$select = "SELECT Work, Units, WorkItem.Parent, WorkItem.Parent.Project.Name, "
. "WorkItem.State, WorkItem.StartDate, WorkItem.DueDate, "
. "WorkItem.Name, WorkItem.EntityType, WorkItem.SYSID, "
. "Resource.Name "
. "FROM RegularResourceLink "
. "WHERE Resource IN (SELECT Name FROM User WHERE Name IN ('John Doe')) "
. "AND WorkItem IN (SELECT ID FROM WorkItem WHERE State = 'Active' AND SYSID LIKE 'T-%') "
;

Thanks!

Jeff

Marketing Mojo IT 0 votes
0
Avatar

Hi Jeff,
Thanks for sharing that.
You could do some optimization on the conditions:
1. Use the Resource ID ('user/<external id of user>') instead of searching for them by their name (Resource = <id> instead of Resource in ...) .
2. The tasks filter you have could negatively impact the performance, as it would retrieve all active tasks on your account for the filter. You may want to try filtering the results for active tasks within your code instead.
Hope this helps,
Ophir

Ophir Kenig 0 votes
0
Avatar

Hi, Jeff!
Here is my query:
SELECT WorkItem.Name, WorkItem.Project.name, WorkItem.Priority, WorkItem.TrackStatus, WorkItem.Importance
, WorkItem.TaskType, WorkItem.State, WorkItem.Manager.DisplayName, WorkItem.C_ReviewStatus, WorkItem
.StartDate, WorkItem.DueDate, WorkItem.Description, WorkItem.Work, WorkItem.ActualEffort, WorkItem.RemainingEffort
, WorkItem.TimeTrackingEffort, WorkItem.BudgetedHours, WorkItem.Duration, WorkItem.ETC, ActualRegularEffort
, RemainingEffort, WorkItem.EntityType FROM RegularResourceLink WHERE Resource = '/User/3407fde7-dce5-44c7-975d-518605f67f43'

It works good.

Yury 0 votes
0
Avatar

And i have one question.
When i came to clarizen under my account, i can view few "Cases". But when i try to get my cases by query - i get nothing!

Here is my query for getting Cases:

SELECT Title, PlannedFor.name, Priority, OriginatedFrom, Severity, EntityType,
State, PlainText, AssignedTo FROM Case WHERE (AssignedTo = '/User/4ad8ab8b-f2cc-48be-83c2-db93bbebd22d'
)

/User/4ad8ab8b-f2cc-48be-83c2-db93bbebd22d - this is my user id.

Could you clarification this problem for me?

Yury 0 votes
0
Avatar

Hi Yuri,
Thanks for sharing you CZSQL!
If you have new questions, please send them in a new post, so it's easier to track for us all.

As for the Cases, dumb questions: Are you sure the cases you see in Clarizen are actually Assigned to you?
They don't have to be to appear in your Cases view.

Hope this helps,
Ophir

Ophir Kenig 0 votes
0
Avatar

Yuri and Ophir,

Thanks for the code and help! I got it working and I can't tell you how much happier I am using CZQL.

Jeff

Marketing Mojo IT 0 votes