Clarity

  • 1.  POST multiple projects via. REST API

    Posted 05-26-2017 01:54 PM

    So, I'm trying to create a sample script using JS.

     

    It fails with bad request 400. I think it has to do with how I'm call SEND() request.

    xhr.send(JSON.stringify(myProjects));

     

    If I use REST API documentation url and try to execution action for multiple project it works. 

     

    Now I do not know if I need to send only 1 project request at a time using LOOP or it would allow me pass full-array and API will handle rest. Any one has tried posting multiple projects using JS?

     

    Here is full-script:

    <script>
    var cappm_apiURL = "http://clarity123.com/ppm/rest/v1";
    var username = "username@clarity123.com"; // Update username
    var password = "password*"; // Update password
    var URL = cappm_apiURL + "/projects";
    var myheader = btoa(username + ":" + password);

    var myProjects = {"_results": [{"code":"ABC1000009","name":"This is My Project Name","scheduleStart": "2017-01-01T08:00:00","scheduleFinish": "2017-12-31T17:00:00","description": "This is Project Description"},{"code":"ABC1000007","name":"This is My Project Name","scheduleStart": "2017-01-01T08:00:00","scheduleFinish": "2017-12-31T17:00:00","description": "This is Project Description"}]};

    var xhr = new XMLHttpRequest();
    xhr.open("POST", URL, true);

    //Send the proper header information along with the request
    xhr.setRequestHeader ("Authorization", "Basic " + myheader);
    xhr.setRequestHeader ("Content-type", "application/json");

    xhr.onreadystatechange = function() {//Call a function when the state changes.
    if(xhr.readyState == 4 && xhr.status == 200) {
    alert("Projects are being created...");
    }
    }
    alert("All Records:" + JSON.stringify(myProjects));
    xhr.send(JSON.stringify(myProjects));
    </script>



  • 2.  Re: POST multiple projects via. REST API

    Posted 05-27-2017 02:16 AM

    Hi DJ.V 

     

    Your pay load for Project Post seems to be incorrect, Bad Request 400 is due to that reason.

    Please refer PPM REST API documentation link https://docops.ca.com/ca-ppm/14-3/en/reference/rest-apis 

     

    Pay load should look like, i have only added basic attribute required for project to be created. 

    Request Pay Load:

    {
    "d": [
             {
                "code": "PRJ001",
                "name": "PPM Project 1"
             },
          {
                "code": "PRJ002",
                "name": "PPM Project 2"
            }
       ]
    }

    My Response payload:

    {
    "_self": "http://XXXXXXXXXXXXXXXXXXX/ppm/rest/v1/projects",
    "_results": [
    {
    "_internalId": 5017001,
    "code": "PRJ001",
    "_self": "http://XXXXXXXXXXXXXXXXXXX/ppm/rest/v1/projects/5017001"
    },
    {
    "_internalId": 5017002,
    "code": "PRJ002",
    "_self": "http://XXXXXXXXXXXXXXXXXXX/ppm/rest/v1/projects/5017002"
    }
    ]
    }

     

    Let me know if this helps.

    Regards,

    Prashank Singh



  • 3.  Re: POST multiple projects via. REST API

    Posted 05-30-2017 03:09 PM

    If you review code snippet I provided, I'm using same format but not working.

     

    {
     "_results": [
      {
       "code":"ABC1000009",
       "name":"This is My Project Name",
       "scheduleStart": "2017-01-01T08:00:00",
       "scheduleFinish": "2017-12-31T17:00:00"
       ,"description": "This is Project Description"
      },
      {
       "code":"ABC1000007",
       "name":"This is My Project Name",
       "scheduleStart": "2017-01-01T08:00:00",
       "scheduleFinish": "2017-12-31T17:00:00",
       "description": "This is Project Description"
      }
     ]
    };



  • 4.  Re: POST multiple projects via. REST API

    Posted 05-30-2017 09:37 PM

    Hi Dharmesh,

     

    Please replace _results with d as payload accepted by ca ppm rest for multiple project is an array variable 'd'. Your code is correct but payload request is not.

     

    Once you replace it with right attribute i.e. d instead of _results it should work.

     

    Let me know if it work after changing attribute name.

     

    Regards,

    Prashank



  • 5.  Re: POST multiple projects via. REST API

    Posted 06-07-2017 04:02 PM

    Prashank.Singh One more question? How do I pass lookup based values? I'm getting error.

     

    { "resourceId": null, "httpStatus": "400", "errorMessage": "CMN-0009: Lookup value is invalid for attribute 'npio_status'.", "errorCode": "validation.lookupValueInvalid" }

     

    There is something in syntax that I probably do not know. Tried various guess but none worked yet.

     

    For Ex. Financial Status

    "financialStatus": {
    "displayValue": "Hold",
    "_type": "lookup",
    "id": "H"
    }

     

     

    Ex.

    {
    "code":"ABC1000018",
    "stPeopleSoftProjetId":"1000018",
    "name":"This is My Project Name",
    "scheduleStart": "2017-01-01T08:00:00",
    "scheduleFinish": "2017-12-31T17:00:00",
    "description": "This is Project Description",
    "financialStatus": {
    "displayValue": "Hold",
    "_type": "lookup",
    "id": "H"
    } }



  • 6.  Re: POST multiple projects via. REST API

    Posted 06-07-2017 04:09 PM

    Sorry found it. It's simple, not sure why I tried not this. "financialStatus": "O" or "financialStatus": "H" worked.



  • 7.  Re: POST multiple projects via. REST API
    Best Answer

    Posted 06-07-2017 03:22 PM

    Thanks Prashank.Singh. Replace dataset with d worked.

     

    {
     "d": [
      {
       "code":"ABC1000009",
       "name":"This is My Project Name",
       "scheduleStart": "2017-01-01T08:00:00",
       "scheduleFinish": "2017-12-31T17:00:00"
       ,"description": "This is Project Description"
      },
      {
       "code":"ABC1000007",
       "name":"This is My Project Name",
       "scheduleStart": "2017-01-01T08:00:00",
       "scheduleFinish": "2017-12-31T17:00:00",
       "description": "This is Project Description"
      }
     ]
    };