4 Replies Latest reply on Sep 10, 2018 3:50 AM by SDepas

    Script not creating task objects with Create() method

    SDepas Apprentice



      I am writing a script to create a number of tasks associated to a ServiceReq based on an array containing CI.EnterpriseApplication names.

      My issue is that I cannot seem to find why the script is not creating the task.Assignment objects. Normally I provide every required field for the task so there is no apparement reason why it shouldn't work. As the documentation of the create method is pretty limited I am asking for help here. I know the script works for the most part as I am able to update objects, but the create part is the only thing giving me trouble.


      Below is the full script, hope it is clear enough to understand otherwise do not hesitate to ask for more explanation.

      The script is called from a quickaction on an existing servicereq conatining the necessary department parameter.

      The environment is out of the box except for the custom N.2.M relationship between a Department and associated CI.EnterpriseApplication.

      In the debug logs my script run fine to the end without throwing any errors, it iterates through each found CI to (try to) create the task.


      console.debug("Starting Application Task creation script");


      // Add ServiceReq record as variable

      var sr = Get('ServiceReq#','$(RecId)');


      //Getting department from Service Request Parameter

      //Get all Service request parameters

      var deptRecId = null;

      var params = sr.RelatedItems['ServiceReqParam#.'];

      console.debug(" -- Variables defined, starting loop trough params");


      // loop to find DeptRecid param

      while (params.MoveNext()) {

      var obj = params.Current;

      if (obj.Fields.ParameterName == "DepartmentRecID"){

      deptRecId = obj.Fields.ParameterValue;




      console.debug(" -- looping done");


      // Get department object based on recId

      var dept = Get('Department#', deptRecId);

      console.debug(" -- Using Department "+dept.Fields.DepartmentName);


      // Get ApplicationsList from Department object, this is a custom relationship that has been added

      var deptApps = dept.RelatedItems['CI#EnterpriseApplication.ApplicationProfile'];

      console.debug(" -- DepartmentApps loaded, next is creating a task per application");


      // Loop through list and create a Task for each application


      var obj = deptApps.Current;

      console.debug(" -- creating task for "+obj.Fields.Name);

      var task = Create('Task#Assignment');








      console.debug(" -- task created, checking for next one...");



      console.debug(" -- script end");

        • 1. Re: Script not creating task objects with Create() method
          lgtandi Specialist



          not sure if this causes your problems but in the documentation it is stated that you shall ensure that you call the "Close" method, i.e. "params.Close()"  and "deptApps.Close()" unless you did process the entire result set.


          Moreover, make sure that you provide all mandatory fields for an assignment within your "task.Update()" method.


          Hope this helps


          • 2. Re: Script not creating task objects with Create() method
            SDepas Apprentice

            Hi lgtandi,


            Thank you for taking the time to reply.

            Where do you suggest me to add the close() methods ? After (and outside) the While loops ?

            Where did you find this information about the Closed() method ? I noticed it was sometimes in use in some examples but not everywhere so its effect is unknown to me.


            About the required/mandatory fields, can you clarify because for instance the field "RecId" is a mandatory field as well as many other fields out of the box but as those are filled in with initialization rules I do not take them in the scope of my call for Update(). So in my update I only provide the minimum of required fields that are not filled in by a business rule, if I look at create examples in the application doc it is also the case there.

            • 3. Re: Script not creating task objects with Create() method
              lgtandi Specialist

              It is stated as part of the documentation of the "Search" web service method ("https://<your tenant>/help/admin/Content/Configure/ImportExportData/Web%20Service%20Methods.htm#Search") and it should be after (outside) the while loops.


              All fields that are required in the state in which the object is created have to be set, either by providing explicit values within your "update()" method or by business rules. In the latter case check that they are really triggered and work as expected, e.g. it is stated that the CurrentLoginId() functions may not work correctly if run within a service. Thus, maybe it's worth to provide all required fields within yout Update method just to check if the problem is caused by issues in this area.

              • 4. Re: Script not creating task objects with Create() method
                SDepas Apprentice

                Hi Andi,


                The Close() method is what was missing in my script.

                No clue what it's effect is however on the execution of the create method so a bit more explanation about it would have been nice on the part of Ivanti.


                For the required field I eliminated the possibility they were involved by disabling all required rules and that had no effect. Moreover, I was able to create a task outside of the While loop, confirming the close() method required at the end was clue in the right direction.


                Thank you for your help