Home General Discussion

Can't get dynamic inputs to save.

Eric_EvansEric_Evans Member IT Monkey ✭

I have made a script that will get some values from a template via API and insert them into an activity. However, when I click save none of the dynamic input has been saved. however, if I run the script again and this time make a manual change to the fields (type an extra character, even just insert a space), it will save the fields that were manually altered. Any ideas?

Best Answer

  • Brett_MoffettBrett_Moffett Cireson PACE Super IT Monkey ✭✭✭✭✭
    Accepted Answer

    Hi @Eric_Evans

    It looks to me, without testing the code in my lab, that you are setting the values on the HTML of the page rather than setting the pageForm.viewModel in the background that has the data that the save and apply button use to write to the database.

    You can write directly to the viewModel by using a command llike:

    var vm = pageForm.viewModel;

    vm.set('title', 'New title');

Answers

  • Eric_EvansEric_Evans Member IT Monkey ✭

    A bit more context.... I have altered the Manual Activity html to include a dropdown for "Templates" (see Screenshot)

    Once a template is selected, I have a listener on the drop down that will fire off a script (example below) that will grab the selected value and traverse the MA templates to find one that has the selected value's name. Once it finds the selected value, another API call is made to get the field values set in the template and adds them to the activity. However the problem comes when I try to save (see initial post)


    // Set Time out so the objects are built before the script fires
    
    
      setTimeout(
    
    
    // Add Event listener to Template Field
    
    
        function() {
          document.getElementById("activity-tabstrip").addEventListener("click", function() {
    $('[data-name = "tvTemplate"]').on('click', function(){
    var clicked = $(this).find( '[aria-selected = "true"]').text();
    
    
    // Set variables for activity fields
    
    
    var TitleField = $(this).closest("div.activity-item-form").find("input[name='Title']");
    var DescriptionField = $(this).closest("div.activity-item-form").find("[data-bind='value: Description']");
    var SupportGroupField = $(this).closest("div.activity-item-form").find("input[class='k-input k-ext-dropdown']");
    var Assigned_ToField = $(this).closest("div.activity-item-form").find("input[data-control-bind='AssignedWorkItem']");
    var AreaField = $(Assigned_ToField).closest("div.col-group").find("input[class='k-input k-ext-dropdown']");
    var PriorityField = $(this).closest("div.activity-item-form").find("[data-control-enumid='65a34474-f43d-d880-7Eb0-bad49efa7cf1']").find(':input');
    var StageField = $(this).closest("div.activity-item-form").find("[data-control-enumid='f05ea0f0-bd02-143e-2b74-303609750328']").find(':input');
    var Scheduled_Start_DateField = $(this).closest("div.activity-item-form").find("[data-control-bind='ScheduledStartDate']");
    var Scheduled_End_DateField = $(this).closest("div.activity-item-form").find("[data-control-bind='ScheduledEndDate']");
    var NotesField = $(this).closest("div.activity-item-form").find("[data-bind='value: Notes']");
    
    
    // Call to the API to get all activity templates
    
    
    
    
     $.ajax({
            url: "/api/V3/Template/GetTemplates?classId=7ac62bd4-8fce-a150-3b40-16a39a61383d",
            async:true,
            dataType: 'json',
            success: function(data) {
    
    
    // Look for template name containing selected 
            
    $(data).each(function (data) {
    var tempname = this.Name;
      if (tempname.toLowerCase().includes(clicked.toLowerCase())){
    var tempid = this.Id;
    console.log (tempid);
    
    
    // Second call to the API to get template values from the previous find
       
      $.ajax({
              url: "/api/V3/Projection/CreateProjectionByTemplate?id=" + tempid + "&createdById=9122821c-1797-2c7a-30bf-38bd352cac45",
              type: "GET",
    	  dataType: 'json',
              success:
                function(result) {
    // Break results into keys to reference
    var vals = Object.keys(result).map(function(key) {
        return result[key];
        
    });
    
    
    //Set values from template array
    
    
    var Title = vals[55];
    var Description = vals[26];
    var Scheduled_Start_Date = vals[48];
    var Scheduled_End_Date = vals[46];
    var Area = vals[21];
    var Stage = vals[51];
    var Prioirty = vals[38];
    var Assigned_To = vals[57];
    var Notes = vals[35];
    
    
    // Set activity fields with desired values
    $(TitleField).val(Title);
    $(DescriptionField).val(Description);
    $(Assigned_ToField).val(Object.values(Assigned_To)[5]);
    $(AreaField).val(Object.values(Area)[1]);
    $(PriorityField).val(Object.values(Prioirty)[1]);
    $(StageField).val(Object.values(Stage)[1]);
    $(Scheduled_Start_DateField).val(Scheduled_Start_Date);
    $(Scheduled_End_DateField).val(Scheduled_End_Date);
    $(NotesField).val(Notes);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
              },
              error: function(error) {
                console.log(error);
              }
            });
          }
    
    
    
    
    
    
    
    
    }
    );
    		  },
    		  error: function(error) {
                console.log(error);
              }
    		});
     
    });
    
    
    
    
    
    
    
    
    }); 
    
    
        }, 6000);
    
    
    
  • Brett_MoffettBrett_Moffett Cireson PACE Super IT Monkey ✭✭✭✭✭
    Accepted Answer

    Hi @Eric_Evans

    It looks to me, without testing the code in my lab, that you are setting the values on the HTML of the page rather than setting the pageForm.viewModel in the background that has the data that the save and apply button use to write to the database.

    You can write directly to the viewModel by using a command llike:

    var vm = pageForm.viewModel;

    vm.set('title', 'New title');

  • Eric_EvansEric_Evans Member IT Monkey ✭

    @Brett_Moffett I absolutely cannot thank you enough! I was able to get it to work, and plan to show it once I clean up my code.

Sign In or Register to comment.