You are viewing limited content. For full access, please sign in.

Question

Question

How to convert null fields to "0"

asked on January 11, 2016

I need to make the blank field under "Points Prior" 0 (zero) if there is nothing entered into it. How would I go about converting it from null to zero?

 

 

Thanks,

David

0 0

Answer

SELECTED ANSWER
replied on January 13, 2016 Show version history

Hi David, sorry I have a typo in my previous post, the variable name should be "Points_Prior_to_Infraction" instead of "Points_Prior_to_Incfraction", can you correct it in your path condition textbox and have a try.

1 0

Replies

replied on January 11, 2016

Hi David,

The following custom JavaScript snippet should do the trick:

// Check for null values whenever a change is made
$('document').on('blur','input.number',function(){
	if ($(this).val() == '')    // if the field is blank
		$(this).val(0);           // set it equal to zero
});

Note that the above will enforce the behavior for all number input fields on the form. If only particular fields are desired to have this behavior, you can replace "document" with a more refined selector. For example, if the id is "q36" then replace "document" with "#q36". If you have particular fields throughout the document, you can give them the "makezero" class and replace "document" above with ".makezero".

Hope this helps!

0 0
replied on January 12, 2016 Show version history

Hi James,

For some reason this doesn't seem to be working. I've set all the fields I need to default to 0 when null to the css class "zero". I updated your code to the following:

// Check for null values whenever a change is made
$('.zero').on('blur','input.number',function(){
	if ($(this).val() == '')    // if the field is blank
		$(this).val(0);           // set it equal to zero
});

Is it possible that it is not taking effect because I have these fields as read only, which I also did using JavaScript?

Here is a sample of the code used for the table:

$(document).ready(function () {
    $('.cf-table-block').on('change', 'input', sumtotal);
    if ($('.subtotal').length > 0) {
        $('.cf-table-block').on('change', 'input', rowtotal);
    }
    function sumtotal() {
        var sum = 0;
        $('td.sum5').each(function () {
            var s = 0;
            $(this).find('input').each(function () {
                s += parseNumber($(this).val());
            });
            $(this).find('.subtotal input').val(s);
            sum += s;
        });
        $('.total5 input').val(sum);
    }
    function rowtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0;
            $(this).find('.sum5 input').each(function () {
                s += parseNumber($(this).val());
            })
            $(this).find('.subtotal input').val(s);
            sum += s;
        });
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

$(document).ready(function() {
    $('.sum5 input').attr('readonly', 'True');
});


$(document).ready(function() {
    $('.total5 input').attr('readonly', 'True');
});

and here is a screenshot of the form:

So I have all three fields marked as readonly. It is adding points prior and points added, and putting the total in points after. So I'm trying to make points prior equal to zero if there were no points prior. Right now it just stays blank. My process modeler kicks off a task based on their points. If they have less than 7 points to start, it does something. The problem I am having is that if the field is empty (null) the task is terminated. Perhaps there is a way to work around this in the process modeler that would be easier? You can see below that if their points prior is less than 7 and points after is greater than 7, it needs approval. This is not met if points prior is null:

Any thoughts?

0 0
replied on January 12, 2016

Hi David, 

you can change your path condition in process modeler to following to meet the case when point prior is null. The [not(node())] will match the case when point prior is empty.

(/dataset/variable/Points_Prior_to_Incfraction[not(node())] or number(/dataset/variable/Points_Prior_to_Incfraction)<number("7")) and number(/dataset/variable/Points_after_Incfraction)>=number("7")

 

0 0
replied on January 13, 2016

Xiuhong,

I entered this into my process modeler and am still receiving the same error where it terminates at this decision. Any idea why?

 

0 0
SELECTED ANSWER
replied on January 13, 2016 Show version history

Hi David, sorry I have a typo in my previous post, the variable name should be "Points_Prior_to_Infraction" instead of "Points_Prior_to_Incfraction", can you correct it in your path condition textbox and have a try.

1 0
replied on January 14, 2016

Thank you!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.