read values selected on ui page

jayasri6
Tera Contributor

Hi Guys,

I have a ui page with 2 attributes in multiple rows as shown in below snapshot.. I want for each attachment selected(checkbox selected), what is the category selected as well. Example: If I select bsnap2 & csnap5, only the categories selected with those attachment should be retrieved in ui page code. With below code provided I am able to achieve that , however if someone changes category again before clicking on Okay, things are getting messed up. Could someone help with fixing this issue.

 

jayasri6_0-1699938298191.png

 

html:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:evaluate var="jvar_sysId" expression="RP.getWindowProperties().target_sys_id" />
    <g:evaluate var="jvar_gr" object="true" jelly="true">
        var test = new GlideRecord('sys_attachment');
        test.addQuery('table_sys_id' , jelly.jvar_sysId);
        test.addQuery('table_name' , 'sn_hr_core_case_payroll');        
        test.query();
        test;
    </g:evaluate>
   
   
    <span style='color: red;' id="error_msg" class="outputmsg_text"></span>
    <h4><font color="#8b8000">Select which attachments to automatically save into the Employee File.</font></h4>
    <h6><font color="#ff0000">Attachments that are selected require a category.</font></h6>
    <style type="text/css">
.phrases.flexbox {
    display: flex;
    flex-flow: row wrap;
    background-color: #ccc; /* For demo purposes */
   
   
   
}

.phrases.flexbox li {
    list-style-type: none;
    flex-basis: calc( 50% - 10px);
    text-align: right;
}

.phrases.flexbox li:nth-of-type(2n+1) {
    background-color: #ccc; /* For demo purposes */
    text-align: left;
   
}
        </style>
   <g:ui_form>
       <h4>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Attachment:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Category:</h4>
       
        <div class="row">
            <div class="col-md-12">
                <ul  id="checkedItems" class="phrases flexbox">
                    <j:while test="${jvar_gr.next()}">
                            <li>
                            <g:ui_checkbox name="${jvar_gr.getValue('file_name')}" id="${jvar_gr.getValue('sys_id')}" /> ${jvar_gr.getValue('file_name')}
                             </li>
                        <li>
                          <select name="number" id="num" onchange="yesnoCheck(this);" >
                            <option value="0">-----NONE----</option>  
                            <option value="cat1">cat1</option>
                            <option value="cat2">cat2</option>
                            <option value="cat3">cat3</option>
                           
                          </select>
                           
                     </li>
                    </j:while>
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="pull-right">
                    <g:dialog_buttons_ok_cancel ok_id="submitData" ok="return continueOK()" ok_type="button" ok_text="${gs.getMessage('Okay')}" ok_style_class="btn btn-primary" cancel_type="button" cancel_id="cancelData" cancel_style_class="btn btn-default" cancel="return continueCancel()" />
                </div>

            </div>
        </div>
    </g:ui_form>
</j:jelly>
 
Client Script:
 
var select_ids = [];

function save() {
    var sys_ids = [];
    $j("#checkedItems li input:checked").each(function() {
        var _sysID = $j(this).attr('id') ? $j(this).attr('id').toString().toLowerCase().replace('ni.', '') : undefined;
        if (_sysID) {
            sys_ids.push(_sysID);
        }
    });
   // alert(sys_ids);
}


function yesnoCheck(that) {

    select_ids.push(that.value);
}

function continueOK() {
    // alert('im in continue okay');
    //alert('select_ids ' +select_ids);
    var gdw = new GlideDialogWindow.get();
    var sys_id = gdw.getPreference('target_sys_id');

    var file_ids = [];
    var cat_ids = [];
    var dat_ids = [];
    var sys_ids = [];
    $j("#checkedItems li input:checked").each(function() {
        var _sysID = $j(this).attr('id') ? $j(this).attr('id').toString().toLowerCase().replace('ni.', '') : undefined;
        if (_sysID) {
            file_ids.push(_sysID);
        }

    });
    // alert(file_ids);
    //alert(cat_ids);
    for (var i = 0; i < file_ids.length; i++) {
        // alert('im in file_ids');
        var att = new GlideRecord('sys_attachment');
        att.addQuery('table_sys_id', sys_id);
        att.addQuery('file_name', file_ids[i]);
        att.query();
        if (att.next()) {
            // alert('found attach record');
            sys_ids.push(att.sys_id);
        }

    }
    //alert(sys_ids);
   // alert("file_ids " + file_ids.length);
   // alert("select_ids " + select_ids.length);

    if (file_ids.length != select_ids.length) {
        $j('#error_msg').html(getMessage('It is mandatory to associate Category to each Attachment selected'));
        gel('error_msg').show();
        return false;
    }
var ns;
    var ga = new GlideAjax('updateSysIDsforOpenText');
    ga.addParam('sysparm_name', 'listOfSysids');
    ga.addParam('sysparm_id', sys_id);
    ga.addParam('sysparm_sysids', sys_ids);
    ga.addParam('sysparm_selectids', select_ids);
    ga.getXMLAnswer(function(ans) {
        ns = ans;
    });
     
    GlideDialogWindow.get().destroy();

}

function continueCancel() {
   // alert('im in continue Cancel');
    GlideDialogWindow.get().destroy();

}

 

 

 

0 REPLIES 0