Home General Discussion

Show added by API relationship without saving record

Olena_PrychynaOlena_Prychyna Customer IT Monkey ✭

We are adding affected items to New incident with API call.
Added related records will become visible only after pressing “Apply” button (or by using commit API call:  /api/V3/Projection/Commit).
Is it possible to make them show up before saving the record?
Incident should stay in “New” status.

Cireson Portal version v7.1

Best Answer

  • Olena_PrychynaOlena_Prychyna Customer IT Monkey ✭
    Answer ✓

    I figured out how to do it!

    Need to initialize the array properly and retrieve each CI record with API call before adding it to the array.

    var addAffectedItem = function (baseId ) {
        $.getJSON('/ConfigItems/GetAffectedItem', { id: baseId }, function (json) {
          var item = {
           BaseId: baseId,
           DisplayName: json.DisplayName,
           Path: json.Path,
           AssetStatus: { Name: !_.isUndefined(json.AssetStatus) ? json.AssetStatus : "" },
           Status: { Name: !_.isUndefined(json.Status) ? json.Status : "" }
           };
          boundArray.push(item);
         });
        } 


    if (_.isUndefined(incident.HasRelatedWorkItems)) {
                    incident.set('HasRelatedWorkItems', new kendo.data.ObservableArray([]));  
                }
       var boundArray = incident.get('HasRelatedWorkItems');
       
        for(var i=0; i < data.HasRelatedWorkItems.length; i++){
         addAffectedItem(data.HasRelatedWorkItems[i].BaseId);
        };

Answers

  • Geoff_RossGeoff_Ross Cireson Consultant Super IT Monkey ✭✭✭✭✭
    Olena,

    What API call are you using to add them? I think you'll be better just pushing the object into the AffectedItems array in the view model and then it will appear.

    Let me know if you need more explanation on that.

    Geoff
  • Olena_PrychynaOlena_Prychyna Customer IT Monkey ✭

    Geoff, I don't use any API call to add them. I have them in the array, but they do not appear without "Apply". var incident = pageForm.viewModel;
    incident.NameRelationship.push({RelationshipId: "B73A6094-C64C-B0FF-9706-1822DF5C2E82", Name: "HasRelatedWorkItems" });
    incident.HasRelatedWorkItems=[];
    for(var i=0; i < data.HasRelatedWorkItems.length; i++){
          incident.HasRelatedWorkItems.push(
         {     ClassTypeId:               data.HasRelatedWorkItems[i].ClassTypeId,
                BaseId:                       data.HasRelatedWorkItems[i].BaseId
         });
    I see the other fields though (like:      incident.set("Source",data.Source);    )Thanks

  • Geoff_RossGeoff_Ross Cireson Consultant Super IT Monkey ✭✭✭✭✭
    I hadn't tested in my lab but I've done something similar recently. I think just push the whole CI object into incident.HasRelatedWorkItems. If data contains more than one CI object, then loop through it and push each one.

    (var i=0; i < data.HasRelatedWorkItems.length; i++){
          incident.HasRelatedWorkItems.push(data.HasRelatedWorkItems[i]);
    }

    If no luck, can you show me how you get 'data' so I can fully replicate.

    Geoff
  • Olena_PrychynaOlena_Prychyna Customer IT Monkey ✭

    I've tried with whole CI - same result.

    data - is the template. Template has Affected CIs. We are trying to mimic Apply Template functionality:

    app.custom.formTasks.add('Incident', "Apply Template", function (formObj, viewModel) { 
      var classid = "a604b942-4c7b-2fb2-28dc-61dc6f465c68";
        $.ajax({
            url: "/api/V3/Template/GetTemplates",
            data: {classId: classid},
            type: "GET",
            success: function (data) {
                loadTemplates(data);
            }
        });
        
      function loadTemplates (templateData){
            //use require js to load the HTML template first
            require(["text!/CustomSpace/customtasks.applytemplate.html"],
                function (htmlTemplate) {
                    //make a jQuery obj
                    templateObj = $(htmlTemplate);

                    //create a view model to handle the UX
                    var _vmWindow = new kendo.observable({
                        dropDownData: templateData,
                        valueChanged : function(e) {
                            var dataItem = e.sender.dataItem();
                            console.log (dataItem.Id)
                        },
                        okClick: function () {
          
                            var templateId = $("#templateselected option:selected").val();
          //var tempName = $("#templateselected option:selected").text();
          var tempIndex = $("#templateselected option:selected").index();
          
          var selectedTemp = templateData[tempIndex];
           updateIncident(selectedTemp);
                            customWindow.close();
                        },
                        cancelClick: function () {
                            customWindow.close();

                       }
                    });
                
                    //create the kendo window
                    customWindow = templateObj.kendoWindow({
                        title: "Apply Template",
                        resizable: false,
                        modal: true,
                        viewable: false,
                        width: 500,
                        height: 300,
                        close: function () {
                
                        },
                        activate: function () {
                            //on window activate bind the view model to the loaded template content
                            kendo.bind(templateObj, _vmWindow);
                        }
                    }).data("kendoWindow");
                
                    //now open the window
                    customWindow.open().center();
                }
            );
        }
     
     function updateIncident(selectedTemp) {
      //selectedTemp is a json object
      var incident = pageForm.viewModel;
      //get template content and save every property into pageForm
      
      $.ajax({
            url: "/api/V3/Projection/CreateProjectionByTemplate",
            data: {id: selectedTemp.Id, createdById: session.user.Id},
            type: "GET",
            success: function (data) {
                
    //Copy properties from Projection into current IR
                incident.set("Title",data.Title);
       
       var oldDescr='';
       if(incident.Description){oldDescr = incident.Description + '\n' ;}
       var newDescr = oldDescr + data.Description;
       incident.set("Description",newDescr);
       
       incident.set("Source",data.Source);
       incident.set("TierQueue",data.TierQueue);
       incident.set("HasCreatedKnowledgeArticle",data.HasCreatedKnowledgeArticle);
       incident.set("Classification",data.Classification);
       incident.set("CauseCode",data.CauseCode);
       incident.set("Impact",data.Impact);
       incident.set("Urgency",data.Urgency);
       
       //add relationships
       
       //incident.NameRelationship.push({RelationshipId: "a860c62e-e675-b121-f614-e52fcbd9ef2c", Name: "AppliesToTroubleTicket" });
       //incident.NameRelationship.push({RelationshipId: "B73A6094-C64C-B0FF-9706-1822DF5C2E82", Name: "HasRelatedWorkItems" });
                incident.HasRelatedWorkItems=[];
       var strAffectedCI = "";
       for(var i=0; i < data.HasRelatedWorkItems.length; i++){
       incident.HasRelatedWorkItems.push(  data.HasRelatedWorkItems[i])
    //   incident.HasRelatedWorkItems.push( { ClassTypeId: data.HasRelatedWorkItems[i].ClassTypeId,
    //     BaseId:   data.HasRelatedWorkItems[i].BaseId })   
       }; 
      } 
            });
     }

       });

  • Olena_PrychynaOlena_Prychyna Customer IT Monkey ✭

    customtasks.applytemplate.html  :

    <div> <!--Make sure you have an outer div if you are putting this in a kendo window-->
        <div id="commentHTML" class="form-horizontal">
                <div>
                    <div class="col-group">
                        <div class="inline-spacing">
                            <label>Please select a template to apply:</label>
                        </div>
                        <div class="inline-spacing">
                            <select id="templateselected" data-role="dropdownlist" data-bind="source: dropDownData, events: { change: valueChanged, dataBound: valueChanged }" data-text-field="Name" data-value-field="Id"/>
                        </div>
                    </div>
                    <div class="window-buttons">
                        <button data-role="button"
                                class="btn btn-primary"
                                data-bind="enabled: okEnabled, events: { click: okClick }">
                            OK
                        </button>
                        <button data-role="button"
                                class="btn btn-primary"
                                data-bind=" events: { click: cancelClick }">
                            Cancel
                        </button>
                    </div>
     
                </div>
        </div>
    </div>

  • Olena_PrychynaOlena_Prychyna Customer IT Monkey ✭
    Answer ✓

    I figured out how to do it!

    Need to initialize the array properly and retrieve each CI record with API call before adding it to the array.

    var addAffectedItem = function (baseId ) {
        $.getJSON('/ConfigItems/GetAffectedItem', { id: baseId }, function (json) {
          var item = {
           BaseId: baseId,
           DisplayName: json.DisplayName,
           Path: json.Path,
           AssetStatus: { Name: !_.isUndefined(json.AssetStatus) ? json.AssetStatus : "" },
           Status: { Name: !_.isUndefined(json.Status) ? json.Status : "" }
           };
          boundArray.push(item);
         });
        } 


    if (_.isUndefined(incident.HasRelatedWorkItems)) {
                    incident.set('HasRelatedWorkItems', new kendo.data.ObservableArray([]));  
                }
       var boundArray = incident.get('HasRelatedWorkItems');
       
        for(var i=0; i < data.HasRelatedWorkItems.length; i++){
         addAffectedItem(data.HasRelatedWorkItems[i].BaseId);
        };

Sign In or Register to comment.