0

I have a script which is going throw users account and and pushing the value of field mobile phone into array. But how can I prevent pushing into array an empty value when the mobile phone field is empty ?

So instead something like this : '123,,,123,123,,123,,123' i want to achive this: [123,123,123,123,123].

This is the main part of code which is checking if value is empty or not:

if (!smsUsers.user.mobile_phone.nil() || 
     smsUsers.user.mobile_phone != null || 
     smsUsers.user.mobile_phone != '' || 
     smsUsers.user.mobile_phone.length != 0 || 
     smsUsers.user.mobile_phone ||
     smsUsers.user.mobile_phone.toString().toLowerCase() != 'false'){

       notSccUserPhone.push(smsUsers.user.mobile_phone);

     }

This is how my whole code look like now:

function getMobileNumber(param) {  
    var sccUserPhone = [];
    var notSccUserPhone = [];                          

    var smsGroup = new CheckServices().getServiceParms("Major Incident", current, true);
    if(smsGroup.next()){
        var smsGroupNumber = smsGroup.u_parm_2;
        gs.log('Group of sms targets ' + smsGroupNumber);

        var smsUsers = new GlideRecord("sys_user_grmember");
        smsUsers.addQuery("group.name", smsGroupNumber);
        smsUsers.query();
        if(param == 'true'){
            while (smsUsers.next()) {
                if(smsUsers.user.email.indexOf('scc') == -1){
                    if (!smsUsers.user.mobile_phone.nil() || smsUsers.user.mobile_phone != null || smsUsers.user.mobile_phone != '' || smsUsers.user.mobile_phone.length != 0 || smsUsers.user.mobile_phone || smsUsers.user.mobile_phone.toString().toLowerCase() != 'false'){
                        notSccUserPhone.push(smsUsers.user.mobile_phone);

                    }
                }       
            }

            gs.log('Not SCC mobile namber array is ' + notSccUserPhone); 
            template.print(notSccUserPhone);
        } else if(param == 'false'){
            while (smsUsers.next()) {
                    if(smsUsers.user.email.indexOf('scc') != -1){
                        if (!smsUsers.user.mobile_phone.nil() || smsUsers.user.mobile_phone != null || smsUsers.user.mobile_phone != '' || smsUsers.user.mobile_phone.length != 0 || smsUsers.user.mobile_phone || smsUsers.user.mobile_phone.toString().toLowerCase() != 'false'){
                            sccUserPhone.push(smsUsers.user.mobile_phone);

                        }

                }
            }

            gs.log('SCC mobile namber array is ' + sccUserPhone);
            template.print(sccUserPhone);
        }
    }

}
3
  • 2
    Is not this ruby code? smsUsers.user.mobile_phone.nil() Commented Jan 21, 2016 at 18:27
  • 1
    You haven't tested this when mobile_phone is null. Commented Jan 21, 2016 at 18:33
  • Possible duplicate of How can I parse a CSV string with Javascript? Commented Jan 21, 2016 at 18:38

1 Answer 1

2

You can try to use regex like this:

if (smsUsers.user.mobile_phone != null &&
 smsUsers.user.mobile_phone.match(/\S/) &&
 smsUsers.user.mobile_phone.toString().toLowerCase() != 'false')
{
    notSccUserPhone.push(smsUsers.user.mobile_phone);
}

Also you need to change your ||'s to &&'s.

And as djechlin pointed out, you're not checking for null early enough; I've moved it to the top of the checks above.

Sign up to request clarification or add additional context in comments.

2 Comments

.mobile_phone != null &&, .mobile_phone != '' &&, and .mobile_phone .length != 0 && can all be condensed to simply .mobile_phone &&.
E.g., the whole thing can be if (mobile_phone && mobile_phone.match(/\S/) && mobile_phone.toLowerCase() != 'false')

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.