0

I have choice value dropdown for priority with value - High, Normal, low. If Priority is "high" , Duedate= Current date + 2; If Priority is "Normal" , Duedate= Current date + 3; If Priority is "Low" , Duedate= Current date + 4;

I need to do this using javascript /Jquery as I am working on Sharepoint Online. I can't use Sharepoint designer or infopath.

This is the code I have written.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script>

_spBodyOnLoadFunctionNames.push("dropdownvalue");

function dropdownvalue() {

    //add an onchange event to the dropdown
    var theSelect = getTagFromIdentifierAndTitle("select","DropDownChoice","Priority").onchange = function() {ChangeEvent()};
}

function ChangeEvent()
{   
    var submittedDate= $("input[title='Submitted Date']").val();
    //var submittedDate = new Date(ctx.CurrentItem.SubmittedDate);
    //get the dropdown
    var dropDown = getTagFromIdentifierAndTitle("select","DropDownChoice","Priority");
     var dueDate = new Date(ctx.CurrentItem.DueDate);
    //get the selected value
    var priority= dropDown.options[dropDown.selectedIndex].text;
    var now = new Date();
    //var nowPlus = new Date();


    switch(priority) {
        case "High (<3 days)": 
            dueDate.setDate(now.getDate()+2);
            break; 
        case "Normal (3 days)": 
            dueDate.setDate(now.getDate()+3);
            break; 
        case "Low (>3 days)": 
            dueDate.setDate(now.getDate()+4);
            break;
    default:
        date = null;
    }
}
</script>
2
  • Is this in a list? Can you make the Duedate column a calculated field?
    – user67793
    Jun 13, 2018 at 20:43
  • Yes I have created a calculated column but For New form we cannot use calculated column now as its deprecated in sharepoint online. Calculated column will be visible only in allitems page but not in New item form.
    – Shraddha
    Jun 13, 2018 at 20:57

2 Answers 2

1

You can use the following code for your requirement:

<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var today = new Date();   
    var todayDay = today.getDate();   
    var todayMon = today.getMonth() + 1;   
    var todayYear = today.getYear(); 

    $("select[title='Priority'").change(function(){

    var value = $(this).find("option:selected").text();

    if(value== 'High'){
    Day=todayDay+2;
    DueDate=todayMon + "/" + Day + "/" + todayYear;
    $("input[title='DueDate']").val(DueDate);
    }

    if(value== 'Normal'){
    Day=todayDay+3;
    DueDate=todayMon + "/" + Day + "/" + todayYear;
    $("input[title='DueDate']").val(DueDate);
    }

    if(value== 'Low'){
    Day=todayDay+4;
    DueDate=todayMon + "/" + Day + "/" + todayYear;
    $("input[title='DueDate']").val(DueDate);
    }

    });


})
</script>

The result is: enter image description here

Note: You need to change the choice column and date column to yours.

1
  • Hi.. Thanks for your response and solution: As also needed to keep in mind business working days.. written below code..
    – Shraddha
    Aug 29, 2018 at 18:21
1
<script language="javascript" type="text/javascript">

var today = new Date();

$(function(){

$("select[title='Priority Required Field']").change(function(){

var value = $(this).find("option:selected").text();

if(value == 'High (<3 days)'){
var slaDate = addBusinessDays(today, 2);    
var DateCreated = new Date(Date.parse(slaDate)).format("MM/dd/yyyy");
$("input[title='Due Date Required Field']").val(DateCreated);
}
else if (value == 'Normal (3 days)')
{          
var slaDate = addBusinessDays(today, 3);
var DateCreated = new Date(Date.parse(slaDate)).format("MM/dd/yyyy");
$("input[title='Due Date Required Field']").val(DateCreated);
}
else if (value == 'Low (>3 days)')
{
var slaDate = addBusinessDays(today, 4);
var DateCreated = new Date(Date.parse(slaDate)).format("MM/dd/yyyy");
$("input[title='Due Date Required Field']").val(DateCreated);
}
else
{
var DateCreated = new Date(Date.parse(today)).format("MM/dd/yyyy");
$("input[title='Due Date Required Field']").val(DateCreated);
}

});
});

function addBusinessDays(date,NoD) 
 {
    d = new Date(date.getTime());

    var day = d.getDay();

   d.setDate(d.getDate() + NoD);

   var dayNumber = d.getDay();

   d = new Date(date.getTime());

   if(dayNumber == 6)
   {
    d.setDate(d.getDate() + (NoD + 2))
   }
   else if(dayNumber == 0)
   {
    d.setDate(d.getDate() + (NoD + 1))
    }
   else
    {
    d.setDate(d.getDate() + NoD);
    }

    return d;
     }


 </script>
3
  • You will need to review the code formatting. I tried to format it correctly, but it does not appear to be syntactically correct Aug 29, 2018 at 23:45
  • Yes.. I have tried to format the code but didn't understand how to do it.
    – Shraddha
    Aug 30, 2018 at 16:29
  • Thanks for your feedback.. I did the formatting so that it will be useful for others too. Let me know if anything is missing.
    – Shraddha
    Aug 30, 2018 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.