Home Analyst Portal

Convert to New SR

James_JohnsonJames_Johnson Customer Adept IT Monkey ✭✭

I'm trying to make a task that will create a new SR based on the current one using a template. Here's the code I have and it mostly works but I can't get the UserInput to copy over. Any ideas on what I'm doing wrong here?

$.ajax({
            url: "/api/V3/Projection/CreateProjectionByTemplate",
            data: {id: tempid, createdById: uid},
            type: "GET",
            success: function (data) {
                //Get the Incident information and copy it to the new Service Request
                data.Title = pageForm.viewModel.Title;
                data.Description = pageForm.viewModel.Description;
                data.ContactMethod = pageForm.viewModel.ContactMethod;
                
                //UserInput part here
                data.UserInput = pageForm.viewModel.UserInput.toJSON();

                data.RequestedWorkItem = [{
                    "ClassTypeId": pageForm.viewModel.RequestedWorkItem.ClassTypeId,
                    "BaseId": pageForm.viewModel.RequestedWorkItem.BaseId,
                    "DisplayName": pageForm.viewModel.RequestedWorkItem.DisplayName
                }];
                data.CreatedWorkItem = [{
                    "ClassTypeId": "eca3c52a-f273-5cdc-f165-3eb95a2b26cf",
                    "BaseId": session.user.Id,
                    "DisplayName": session.user.Name
                }];
                data.RelatesToWorkItem = [{
                    "ClassTypeId": pageForm.viewModel.ClassTypeId,
                    "BaseId": pageForm.viewModel.BaseId,
                    "Id": pageForm.viewModel.Id
                }];
                data.NameRelationship = [{
                    "Name": "RequestedWorkItem",
                    "RelationshipId": "DFF9BE66-38B0-B6D6-6144-A412A3EBD4CE"
                },
                {
                    "Name": "RelatesToWorkItem",
                    "RelationshipId": "cb6ce813-ea8d-094d-ee5a-b755701f4547"
                },
                {
                    "Name": "CreatedWorkItem",
                    "RelationshipId": "df738111-c7a2-b450-5872-c5f3b927481a"
                }]; 
                var srid = data.Id;
                var strData = { "formJson":{"current": data }}
                $.ajax({
                    url: "/api/V3/Projection/Commit",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(strData) ,
                    success: function (data2) {
                        IR2SR_setIRresolved(pageForm.viewModel);
                        app.lib.mask.apply('Converting to SR...');
                        window.location.href = window.location.protocol + "//" + window.location.hostname + "/ServiceRequest/Edit/" + srid
                    }
                });                                 
            }
        });

Best Answer

  • Justin_WorkmanJustin_Workman Cireson Support Super IT Monkey ✭✭✭✭✭
    Accepted Answer

    Or you could just re-fetch the projection that would still have the UserInput property untransformed:

    var getCriteria = function(proj, classId, displayName) {

        var crit = {

          Id: proj,

          Criteria: {

            Base: {

              Expression: {

                SimpleExpression: {

                  ValueExpressionLeft: {

                    Property: "$Context/Property[Type='" + classId + "']/DisplayName$" 

                  },

                  Operator: "Equal",

                  ValueExpressionRight: {

                    Value: displayName

                  }

                }

              }

            }

          }

        }

      return crit;

    }


    $.ajax({

      url: "/api/V3/Projection/GetProjectionByCriteria", 

    dataType: "json",

    data: JSON.stringify(getCriteria(session.user.ServiceRequestFormProjectionId, pageForm.viewModel.ClassTypeId, pageForm.viewModel.DisplayName)),

    success: function(result, status) {

    console.log(result[0].UserInput)

    },

    error: function(result, status, error) {

    console.log(getCriteria(session.user.ServiceRequestFormProjectionId, pageForm.viewModel.ClassTypeId, pageForm.viewModel.DisplayName));

    },

    type: "POST",

    contentType: "application/json; charset=utf-8",

    })

Answers

  • Justin_WorkmanJustin_Workman Cireson Support Super IT Monkey ✭✭✭✭✭

    @James_Johnson - I think the problem you're running into is that the UserInput property is converted to json when the form loads. The actual format of the UserInput (typically) is XML. So what's likely happening is you're dumping the translated JSON into the UserInput property of the new work item and when its form loads, the portal is trying to translate that JSON from XML to JSON.

  • James_JohnsonJames_Johnson Customer Adept IT Monkey ✭✭

    Thanks @Justin_Workman for the answer. So if I'm understanding this right I need to convert the UserInput from the page back to an xml string before creating the new work item?

  • Justin_WorkmanJustin_Workman Cireson Support Super IT Monkey ✭✭✭✭✭
    Accepted Answer

    Or you could just re-fetch the projection that would still have the UserInput property untransformed:

    var getCriteria = function(proj, classId, displayName) {

        var crit = {

          Id: proj,

          Criteria: {

            Base: {

              Expression: {

                SimpleExpression: {

                  ValueExpressionLeft: {

                    Property: "$Context/Property[Type='" + classId + "']/DisplayName$" 

                  },

                  Operator: "Equal",

                  ValueExpressionRight: {

                    Value: displayName

                  }

                }

              }

            }

          }

        }

      return crit;

    }


    $.ajax({

      url: "/api/V3/Projection/GetProjectionByCriteria", 

    dataType: "json",

    data: JSON.stringify(getCriteria(session.user.ServiceRequestFormProjectionId, pageForm.viewModel.ClassTypeId, pageForm.viewModel.DisplayName)),

    success: function(result, status) {

    console.log(result[0].UserInput)

    },

    error: function(result, status, error) {

    console.log(getCriteria(session.user.ServiceRequestFormProjectionId, pageForm.viewModel.ClassTypeId, pageForm.viewModel.DisplayName));

    },

    type: "POST",

    contentType: "application/json; charset=utf-8",

    })

  • James_JohnsonJames_Johnson Customer Adept IT Monkey ✭✭

    That did the trick, thanks Justin!

  • James_JohnsonJames_Johnson Customer Adept IT Monkey ✭✭

    @Justin_Workman would it be possible to do attachments this way too or is that more complicated? I tried to set the attached items of the new object equal to the re-fetched object but it didn't seem to like that.

    Thanks!

    James

Sign In or Register to comment.