none
Calculated value is view only.

    Question

  • Hi everyone,

    I'm new in sharepoint development, and need some help.

    I have sharepoint list for vacation applications. And have javascript code which calculating working days and next working day for employees. Script is working fine, only problem is that sharepoint showing calculation result in column, but when I click save the field is empty. Looks like its view only.

    Thanks to everyone who can help

    Here is my code.

    <script type="text/javascript" language="javascript">
    
    document.addEventListener("DOMContentLoaded", function(event) {
        var from = document.getElementById('Element ID');
        var to = document.getElementById('Element ID');
        var days_count = document.getElementById('Element ID');
        var next_working_day = document.getElementById('Element ID');
        var start_date, end_date, vacation_days = 0;
     
        function setNextWorkingDay() {
            initEndtDate();
            if(end_date.getDay() == 5) {
                end_date.setDate(end_date.getDate()+3);
                next_working_day.value = end_date.getDate() + '.' + (end_date.getMonth() + 1) + '.' + end_date.getFullYear();
                return false;
            }
    
            if(end_date.getDay() == 6) {
                end_date.setDate(end_date.getDate()+2);
                next_working_day.value = end_date.getDate() + '.' + (end_date.getMonth() + 1) + '.' + end_date.getFullYear();
                return false;
            }
            end_date.setDate(end_date.getDate()+1);
            console.log('next ',end_date);
            next_working_day.value = end_date.getDate() + '.' + (end_date.getMonth() + 1) + '.' + end_date.getFullYear();
            
        }
    
        function dateDiff( date1, date2 ) {
          
          var one_day=1000*60*60*24;
    
          
          var date1_ms = date1.getTime();
          var date2_ms = date2.getTime();
    
          var difference_ms = date2_ms - date1_ms;
            
          return Math.round(difference_ms/one_day); 
        }
        function calculate_vacation_days() {
            
            if(typeof start_date === "undefined" || typeof end_date === "undefined") {
                return false;
            }
    
            if(dateDiff(start_date, end_date) < 0) {
                console.log('wrong dates');
                return false;
            }
    
            console.log(start_date, end_date);
           
            while (start_date_x = iterable_start_date) {
                
    
                if (start_date_x.toLocaleDateString() === end_date.toLocaleDateString()) {
                    if (start_date_x.getDay() !== 0 && start_date_x.getDay() !== 6) {
                        vacation_days++
                    } 
                    break;
                }
     
                if (start_date_x.getDay() !== 0 && start_date_x.getDay() !== 6) {
                    vacation_days++
                } 
     
                iterable_start_date.setDate(iterable_start_date.getDate() + 1);
            }
     
            days_count.value = vacation_days;
            console.log(vacation_days)
            setNextWorkingDay();
        }
    
        function initStartDate() {
             var start_date_array = from.value.split('.');
            if(typeof start_date_array[0] === "undefined" || typeof start_date_array[1] === "undefined" || typeof start_date_array[2] === "undefined" ) {
                return false;
            }
    
            start_date = new Date(start_date_array[2], parseInt(start_date_array[1]) - 1, parseInt(start_date_array[0]));
            iterable_start_date = new Date(start_date_array[2], parseInt(start_date_array[1]) - 1, parseInt(start_date_array[0]));
            iterable_end_date = new Date(start_date_array[2], parseInt(start_date_array[1]) - 1, parseInt(start_date_array[0]));
            temp_start_date = new Date(start_date_array[2], parseInt(start_date_array[1]) - 1, parseInt(start_date_array[0]));
        }
    
        function initEndtDate(){
            var end_date_array = to.value.split('.');
            if(typeof end_date_array[0] === "undefined" || typeof end_date_array[1] === "undefined" || typeof end_date_array[2] === "undefined" ) {
                return false;
            }
            end_date = new Date(end_date_array[2], parseInt(end_date_array[1] - 1), parseInt(end_date_array[0]));
        }
     
        from.addEventListener('blur', function(e) {
            e.stopPropagation();
            console.log("from event fired");
            vacation_days = 0
           
            days_count.value = '';
            
            initStartDate()
           
            initEndtDate();
           
            calculate_vacation_days();
     
        });
     
        to.addEventListener('blur', function(e) {
            e.stopPropagation();
            console.log("to event fired");
            vacation_days = 0;
           
            days_count.value = ''
            
            initStartDate();
           
            initEndtDate();
    
            calculate_vacation_days();
        });
     
        days_count.addEventListener('change', function(e) {
            e.stopPropagation();
            console.log("days_count event fired");
            var days_to_increment = days_count.value;
            
            initStartDate();
            
            // start from 1 as first day is already in "iterable_end_date"
            for (var i = 1; i < days_to_increment; i++) {
                console.log(i);
                console.log(iterable_end_date.getDay());
               
                if (iterable_end_date.getDay() == 6) {
                    console.log('sat');
                    iterable_end_date.setDate(iterable_end_date.getDate() + 1);
                }
    
                if (iterable_end_date.getDay() == 0) {
                    console.log('sun');
                    iterable_end_date.setDate(iterable_end_date.getDate() + 1);
                }
               
                console.log(iterable_end_date.getDay());
                
                iterable_end_date.setDate(iterable_end_date.getDate() + 1);
                
                temp_start_date.setDate(temp_start_date.getDate() + 1);
                
                 if (iterable_end_date.getDay() == 6) {
                    console.log('sat');
                    iterable_end_date.setDate(iterable_end_date.getDate() + 1);
                }
                
                if (iterable_end_date.getDay() == 0) {
                    console.log('sun');
                    iterable_end_date.setDate(iterable_end_date.getDate() + 1);
                }
    
                console.log(iterable_end_date,temp_start_date);
            }
            
            to.value = iterable_end_date.getDate() + '.' + (iterable_end_date.getMonth() + 1) + '.' + iterable_end_date.getFullYear();
    
            setNextWorkingDay();
        });
     
    });
    </script>


    Friday, November 2, 2018 1:09 PM

Answers

  • Hi,

    As a workaround, you can add the code into PreSaveAction function.

    https://social.technet.microsoft.com/wiki/contents/articles/31330.sharepoint-using-presaveaction-function-on-custom-list-forms.aspx

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    • Unmarked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    • Marked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    Monday, November 12, 2018 10:11 AM
    Moderator
  • Hi.

    I found solution, I have added focus and blur after input, and now form is recognizing value.

    Thanks, for all answers.

            if(end_date.getDay() == 6) {
                end_date.setDate(end_date.getDate()+2);
                next_working_day.value = end_date.getDate() + '.' + (end_date.getMonth() + 1) + '.' + end_date.getFullYear();
                return false;
            }
            end_date.setDate(end_date.getDate()+1);
            console.log('next ',end_date);
            next_working_day.value = end_date.getDate() + '.' + (end_date.getMonth() + 1) + '.' + end_date.getFullYear();
            next_working_day.focus();
    	next_working_day.blur();
        }
    
        function dateDiff( date1, date2 ) {
          
          var one_day=1000*60*60*24;
    
          
          var date1_ms = date1.getTime();
          var date2_ms = date2.getTime();
    
          var difference_ms = date2_ms - date1_ms;
            
          return Math.round(difference_ms/one_day); 
        }
        function calculate_vacation_days() {
            
            if(typeof start_date === "undefined" || typeof end_date === "undefined") {
                return false;
            }
    
            if(dateDiff(start_date, end_date) < 0) {
                console.log('wrong dates');
                return false;
            }
    
            console.log(start_date, end_date);
           
            while (start_date_x = iterable_start_date) {
                
    
                if (start_date_x.toLocaleDateString() === end_date.toLocaleDateString()) {
                    if (start_date_x.getDay() !== 0 && start_date_x.getDay() !== 6) {
                        vacation_days++
                    } 
                    break;
                }
     
                if (start_date_x.getDay() !== 0 && start_date_x.getDay() !== 6) {
                    vacation_days++
                } 
     
                iterable_start_date.setDate(iterable_start_date.getDate() + 1);
            }
     
            days_count.value = vacation_days;
    	days_count.focus();
    	days_count.blur();
            console.log(vacation_days)
            setNextWorkingDay();
        }


    • Marked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    Tuesday, November 13, 2018 5:51 AM
  • Hi,

    Glad to hear that you solve this issue, thanks for your sharing.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Armen_S Tuesday, November 13, 2018 11:24 AM
    Tuesday, November 13, 2018 7:00 AM
    Moderator

All replies

  • Hi Armen, first I'd like to say that jQuery may prove to be very helpful for you in the future. If you're going to be coding, it will shorten a lot of your lines here.

    That said, I'm also unsure of where it is in your code where you're setting the value of the field? Is this code in the New/Edit form page for the list in question? Or where is this code stored/run?


    Please remember to mark as answer if you found this to be helpful. Andy Wessendorf | Belcan

    Friday, November 2, 2018 2:51 PM
  • Hi Andrew,

    thank you for reply.

    Code is running on form web parts, default new form.

    Saturday, November 3, 2018 5:16 AM
  • Hi,

    Please provide all the field names and field types and some screenshots for further research.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, November 5, 2018 2:46 AM
    Moderator
  • Fields names are in Armenian, in code I use element ID.

    First and second fields type is date, third field is number, fourth  field is single line of text.

    As you can see in below screenshot, working day count and next working day is calculated, but form still showing that required field is empty.




    • Edited by Armen_S Monday, November 5, 2018 5:53 AM
    Monday, November 5, 2018 5:44 AM
  • Hi,

    I tested your code in my custom list, I add the code into default newform.aspx page, and it working fine.

    Please create a new custom list with the fields, and check if it works.

    Or did you use InfoPath form?

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, November 5, 2018 6:38 AM
    Moderator
  • Hi,

    Would you please provide us with an update on the status of your issue?

    Best Regards,
    Dennis

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, November 8, 2018 8:55 AM
    Moderator
  • Hi

    Thank you for your help, sorry for being late, I have business trip.

    Yes I'm using InfoPath form.

    And InfoPath not not recognising calculated value.

    As I understand, info path needs mouse click in item for fixing value as imputed, I can't find how to simulate mouse click.
    • Edited by Armen_S Friday, November 9, 2018 1:27 PM
    Friday, November 9, 2018 1:21 PM
  • Hi,

    As a workaround, you can add the code into PreSaveAction function.

    https://social.technet.microsoft.com/wiki/contents/articles/31330.sharepoint-using-presaveaction-function-on-custom-list-forms.aspx

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    • Unmarked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    • Marked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    Monday, November 12, 2018 10:11 AM
    Moderator
  • Hi.

    I found solution, I have added focus and blur after input, and now form is recognizing value.

    Thanks, for all answers.

            if(end_date.getDay() == 6) {
                end_date.setDate(end_date.getDate()+2);
                next_working_day.value = end_date.getDate() + '.' + (end_date.getMonth() + 1) + '.' + end_date.getFullYear();
                return false;
            }
            end_date.setDate(end_date.getDate()+1);
            console.log('next ',end_date);
            next_working_day.value = end_date.getDate() + '.' + (end_date.getMonth() + 1) + '.' + end_date.getFullYear();
            next_working_day.focus();
    	next_working_day.blur();
        }
    
        function dateDiff( date1, date2 ) {
          
          var one_day=1000*60*60*24;
    
          
          var date1_ms = date1.getTime();
          var date2_ms = date2.getTime();
    
          var difference_ms = date2_ms - date1_ms;
            
          return Math.round(difference_ms/one_day); 
        }
        function calculate_vacation_days() {
            
            if(typeof start_date === "undefined" || typeof end_date === "undefined") {
                return false;
            }
    
            if(dateDiff(start_date, end_date) < 0) {
                console.log('wrong dates');
                return false;
            }
    
            console.log(start_date, end_date);
           
            while (start_date_x = iterable_start_date) {
                
    
                if (start_date_x.toLocaleDateString() === end_date.toLocaleDateString()) {
                    if (start_date_x.getDay() !== 0 && start_date_x.getDay() !== 6) {
                        vacation_days++
                    } 
                    break;
                }
     
                if (start_date_x.getDay() !== 0 && start_date_x.getDay() !== 6) {
                    vacation_days++
                } 
     
                iterable_start_date.setDate(iterable_start_date.getDate() + 1);
            }
     
            days_count.value = vacation_days;
    	days_count.focus();
    	days_count.blur();
            console.log(vacation_days)
            setNextWorkingDay();
        }


    • Marked as answer by Armen_S Tuesday, November 13, 2018 5:51 AM
    Tuesday, November 13, 2018 5:51 AM
  • Hi,

    Glad to hear that you solve this issue, thanks for your sharing.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by Armen_S Tuesday, November 13, 2018 11:24 AM
    Tuesday, November 13, 2018 7:00 AM
    Moderator