Rally Software

 View Only
  • 1.  i want to update a defect in Rally via Rally API.

    Posted Nov 24, 2020 03:09 PM
    I am able to pull/fetch the existing defects from Rally but not able to update the existing defect from Rally. 

    here is the code i am using :

    //Update defect
    System.out.println("\nUpdating defect state...");
    JsonObject updatedDefect = new JsonObject();
    updatedDefect.addProperty("State", "Fixed");
    UpdateRequest updateRequest = new UpdateRequest("DE170187", updatedDefect);
    UpdateResponse updateResponse = restApi.update(updateRequest);
    JsonObject obj = updateResponse.getObject();
    System.out.println(String.format("Updated defect. State = %s", obj.get("State").getAsString()));
    -----------------
    Error i am getting - in this line - UpdateResponse updateResponse = restApi.update(updateRequest);

    Exception in thread "main" java.lang.NullPointerException: key == null
    at com.google.gson.internal.LinkedTreeMap.put(LinkedTreeMap.java:92)
    at com.google.gson.JsonObject.add(JsonObject.java:61)
    at com.rallydev.rest.request.UpdateRequest.getBody(UpdateRequest.java:41)
    at com.rallydev.rest.RallyRestApi.update(RallyRestApi.java:189)
    at com.rallydev.rest.RallyRestApi.update(RallyRestApi.java:185)
    at stepdefinitions.UpdateDefectRally.main(UpdateDefectRally.java:53)

    Any suggestions please.


  • 2.  RE: i want to update a defect in Rally via Rally API.
    Best Answer

    Broadcom Employee
    Posted Nov 24, 2020 03:23 PM
    Hi Anuj.

    Please check out the java CRUD example file here, which is part of the Java toolkit. You need to instantiate the UpdateRequest object with a reference to the object (see the 'ref' variable). In your code you're replacing that with "DE170187". You'll need to locate the defect, fetch it and include its reference to your update request. I'm including a screenshot from that example page showing how it's done.

    Please let us know if that helped.

    Thanks,
    Sagi





  • 3.  RE: i want to update a defect in Rally via Rally API.

    Posted Nov 24, 2020 03:32 PM
    Thanks Sagi, 

    I referred that exact code. Below is the complete code i am using :

    //Create a defect
    System.out.println("Creating defect...");
    JsonObject newDefect = new JsonObject();
    newDefect.addProperty("Name", "Test Defect");
    newDefect.addProperty("Project", "projectRef");
    newDefect.addProperty("State", "Open");
    newDefect.addProperty("ScheduleState", "Pending");
    newDefect.addProperty("Environment", "Test");
    newDefect.addProperty("Severity", "Cosmetic");
    newDefect.addProperty("Priority", "Low");
    newDefect.addProperty("Defect Reported By ", "Internally Reported");
    //newDefect.addProperty("c_DefectReportedBy", "Internally Reported");

    CreateRequest createRequest = new CreateRequest("defect", newDefect);
    CreateResponse createResponse = restApi.create(createRequest);
    System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));

    //Read defect
    String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
    System.out.println(String.format("\nReading defect %s...", ref));
    GetRequest getRequest = new GetRequest(ref);
    GetResponse getResponse = restApi.get(getRequest);
    JsonObject obj = getResponse.getObject();
    System.out.println(String.format("Read defect. Name = %s, State = %s",
    obj.get("Name").getAsString(), obj.get("State").getAsString()));

    //Update defect
    System.out.println("\nUpdating defect state...");
    JsonObject updatedDefect = new JsonObject();
    updatedDefect.addProperty("State", "Fixed");
    UpdateRequest updateRequest = new UpdateRequest(ref, updatedDefect);
    UpdateResponse updateResponse = restApi.update(updateRequest);
    obj = updateResponse.getObject();
    System.out.println(String.format("Updated defect. State = %s", obj.get("State").getAsString()));


    ---------------------------------------
    But still i am getting - CreateResponse createResponse = restApi.create(createRequest);

    {"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Could not read: Object not found for Object ID: null"], "Warnings": ["It is no longer necessary to append \".js\" to WSAPI resources."]}}


  • 4.  RE: i want to update a defect in Rally via Rally API.

    Posted Nov 24, 2020 04:37 PM
    I able to resolve the issue, Thanks Sagi. 
    Here is the code that worked for me.

    // Read defect
    QueryRequest defectRequest = new QueryRequest("defect");
    defectRequest.setQueryFilter(new QueryFilter("FormattedID", "=", defectId));
    defectRequest.setFetch(new Fetch());

    QueryResponse defectQueryResponse = restApi.query(defectRequest);


    for (int i = 0; i < defectQueryResponse.getResults().size(); i++) {
    JsonObject defectJsonObject = defectQueryResponse.getResults().get(i).getAsJsonObject();
    refObject = defectJsonObject.get("_ref");
    String ref1 = Ref.getRelativeRef(refObject.getAsString());

    // Update defect
    System.out.println("\nUpdating defect state...");
    JsonObject updatedDefect = new JsonObject();
    updatedDefect.addProperty("State", "Open");
    updatedDefect.addProperty("Notes", "This comment is added for testing Automation process , please ignore");
    UpdateRequest updateRequest = new UpdateRequest(ref1, updatedDefect);
    UpdateResponse updateResponse = restApi.update(updateRequest);
    JsonObject obj = updateResponse.getObject();
    System.out.println(String.format("Updated defect. State = %s", obj.get("State").getAsString()));
    }