Widget is not updating the date selected in the Widget on the target record

jonathangilbert
Kilo Sage

Morning, I am trying to create a widget that wil allow an end user to update a date field on a RITM. I have based the widget using script from the OOB "Update Watchlist" widget, but I cannot get the date entered in the widget to update the target record. By way of a test, I did add in a line of code just to enter the words "Script ran" , which does update after the button is pressed. So it just seems to be the date value that is not getting passed accross,

 

Here is my Script

 

HTML:-

<div class="panel panel-primary b">
<div class="panel-heading">
<h3 class="panel-title pull-left">
${Colleagues Leave Date}
</h3>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<div class="text-center text-italic text-muted">
<sp-date-picker field="u_colleagues_leaving_date_and_time"sn-include-time="true"></sp-date-picker>
</div>
</div>
<button type="button" class="btn btn-warning btn-block" ng-click="c.updatedate('updatedate')">Update Leave Date</button>
</div>

 

 

 

 

CSS:-

h1, h2{
  color:#f7989e;
}
 
 
.panel {
    margin-bottom: 20px;
    background-color: #fff;
    border: 0px solid transparent;
    border-radius: 0px;
    -webkit-box-shadow: 0 0px 0px rgba(0, 0, 0, .05);
    box-shadow: 0 0px 0px rgba(0, 0, 0, .05);
  border-right:1px solid #ddd;
}
 
.attachments .panel {  
  border-right:0px solid #ddd;
}
 
 
.panel-only-right {
border-top: 0;
  border-bottom: 0;
  border-left: 0;
  box-shadow:none;
}
 
.list-group-item {
border: 0;
  box-shadow: none;
  padding-left: 0;
  margin-bottom: 10px;
}
 
.comment {
color: #000;
  font-weight: bold;
}
 
.font-weight-bold{
font-weight:bold;}

 

 

 

CLIENT SCRIPT:-

function($scope, spUtil) 
{
var c = this;
$scope.u_colleagues_leaving_date_and_time = 
{   
displayValue: c.data.displayValue,   
value: c.data.value,
name: 'u_colleagues_leaving_date_and_time'   
}; 
 
c.updatedate = function(action) 
{
c.data.action = action;
c.server.update().then(function() 
{
c.data.action = undefined;
})
}
 
$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'u_colleagues_leaving_date_and_time')
c.data.updatedate = parms.newValue;
});
}
 
 
 
SERVER SCRIPT:-
 
(function() 
 {
var table = $sp.getParameter('table')
var sys_id = $sp.getParameter('sys_id')
var gr = new GlideRecord(table);
if(gr.get(sys_id))
{
data.displayValue = gr.getDisplayValue('u_colleagues_leaving_date_and_time');
data.value = gr.getValue('u_colleagues_leaving_date_and_time');
}
 
if (input && input.action) 
{
var action = input.action;
if (action == 'updatedate') 
{
gr.u_colleagues_leaving_date_and_time= input.updatedate;
gr. u_uipath_comment = "script ran";
gr.update();
 
gs.addInfoMessage("Leave Date updated ");
 
}
}
 
})();
 
 
 
Any help would be much appreciated
2 REPLIES 2

Kieran Anson
Kilo Patron

Hey,

When posting code on the community, it'll be easier for us to read if you wrap the content in a code sampe (image below)

KieranAnson_0-1714032647845.png

 

In regards to your issue, a couple code-smells to tidy up to avoid possible collisions:

  1. Try to avoid using "gr" as a variable name. Variables are global and it's more than likely that SN have used "gr" to store a value which you could overwrite
  2. Use getters and setters rather than directly assigning a value. Although SN tries to be helpful and understand your intent, it can lead you to running into some unexpected behaviour
  3. When setting date/time field values, you either need to to set it using the system date/time format or the users (the latter requiring the use of setDisplayValue() rather than setValue())
(function() {
    var table = $sp.getParameter('table')
    var sys_id = $sp.getParameter('sys_id')
    var currentRecordcurrentRecordGR = new GlideRecord(table);
    if (currentRecordGR.get(sys_id)) {
        data.displayValue = currentRecordGR.getDisplayValue('u_colleagues_leaving_date_and_time');
        data.value = currentRecordGR.getValue('u_colleagues_leaving_date_and_time');
    }

    if (input && input.action) {
        var action = input.action;
        if (action == 'updatedate') {
            currentRecordGR.setValue('u_colleagues_leaving_date_and_time' , input.updatedate);
            currentRecordGR.setValue('u_uipath_comment' , "script ran");
            currentRecordGR.update();

            gs.addInfoMessage("Leave Date updated ");

        }
    }

})();

Change your usage of the spDatePicker directive to directly bind the value such as 

<sp-date-picker ng-model="data.u_colleagues_leaving_date_and_time" sn-include-time="true"></sp-date-picker>

 

Hi Kieran

 

Thanks for your response, Even following your advice and script, the Widget still does not update the date field