Pedro Dusso
Pedro Dusso

Reputation: 2140

Rally GetByReference returning null

I am querying for a PortfolioItem/Mmf. This works fine:

new Request("PortfolioItem/Mmf")
            {
                ProjectScopeUp = false,
                ProjectScopeDown = true,
                Fetch = new List() { "Name", "Description", "FormattedID", "LastUpdateDate", "Owner", "Children" },
                Query = new Query("FormattedID", Query.Operator.Equals, _formattedID)
            };

But when I query for the ref address (which I can open on my browser and check the json perfectly) like this:

//_childFetch contains the same Fetch string list from the previous query
var childObject = m_rallyApi.GetByReference(_childRef, _childFetch);

it returns null.

Why does this not work? Both Queries work when it is a hierarchical requirement.

Edit Full code of the method using the GetByReference()

private HierarchicalRequirement GetUserStoryByReference(string _childRef, string[] _childFetch)
        {
            HierarchicalRequirement userStory = null;

            var childObject = m_rallyApi.GetByReference(_childRef, _childFetch);

            if (childObject["Children"].Count == 0)
            {
                userStory = new HierarchicalRequirement(childObject);
            }
            else
            {
                if (childObject["Children"].Count > 0)
                {
                    userStory = new HierarchicalRequirement(childObject);

                    foreach (var child in childObject["Children"])
                    {
                        userStory.Children.Add(GetUserStoryByReference(child["_ref"], _childFetch));
                    }
                }
            }
            return userStory;
        }

Upvotes: 1

Views: 297

Answers (1)

nickm
nickm

Reputation: 5966

I submitted a defect. I get

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.

when using

String featureRef = queryResults.Results.First()._ref; 
Console.WriteLine(featureRef); //prints correctly
DynamicJsonObject feature = restApi.GetByReference(featureRef, "Name");
String name = feature["Name"];

The last line is where it chokes. Identical code with UserStories and its Children works.

If I avoid GetByReference it works with PortfolioItems. Here is the code:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Rally.RestApi;
using Rally.RestApi.Response;

namespace FindTFchildren
{
    class Program
    {
        static void Main(string[] args)
        {
            RallyRestApi restApi;
            restApi = new RallyRestApi("[email protected]", "secret", "https://rally1.rallydev.com", "v2.0");

            String projectRef = "/project/12352814790";     //replace this OID with an OID of your project


            Request fRequest = new Request("PortfolioItem/Feature");
            fRequest.Project = projectRef;
            fRequest.Workspace = workspaceRef;
            fRequest.Fetch = new List<string>() { "FormattedID", "Name", "UserStories"};
            fRequest.Query = new Query("FormattedID", Query.Operator.Equals, "F3");
            QueryResult queryResults = restApi.Query(fRequest);


            foreach (var f in queryResults.Results)
            {
                Console.WriteLine("FormattedID: " + f["FormattedID"] + " Name: " + f["Name"]);
                Console.WriteLine("Collection ref: " + f["UserStories"]._ref);
                Request childrenRequest = new Request(f["UserStories"]);
                QueryResult queryChildrenResult = restApi.Query(childrenRequest);
                foreach (var c in queryChildrenResult.Results)
                {
                    Console.WriteLine("FormattedID: " + c["FormattedID"] + " Name: " + c["Name"]);
                }

            }
        }
    }
}

Upvotes: 1

Related Questions