Child List - filter doesn't work

Hi,

The goal is to implement a work around to filtering Child List with lookup field where all items without lookup field set are shown in any new Parent Form.

I have followed the article about filtering Child List with CAML, but unfortunately it doesn't work.
Explanation:
My Child List Form has 3 fields "DocumentType", "RequestNumber" - which is lookup and "RequestNumberTMP" where I write Parents requestNumber - so even if I add an document to child list and close parent without saving I will be able to clean child list with event receiver (at night on daily basis) or so.

My code:

function filterAtt(){
    var attfilter = "<Eq><FieldRef Name='RequestNumberTMP'/><Value Type='Text'>" + rrnumber +"</Value></Eq>";
    dt.filter = attfilter;
    dt.refresh();
 }

fd.spRendered(function () {

    rrnumber = generateIdentifier(); //this returns custom ID = text
    fd.field('RequestNumber').value = rrnumber; //this is later read in Child form and child's field RequestNumberTMP is set
    fd.field('RequestNumber').disabled;//so user can not change it.
       
    var dt = fd.control('Attachment');
    dt.ready(function() { 
       filterAtt(); 
    });
 }

I'm wonder what is wrong here.

This is a bit strange...

Hello @Marcin,

How do you create the custom ID? Please share the code for the generateIdentifier() function.

Here it is:

function generateIdentifier() {
    var now = new Date();
    var rnumber = "PZ_" + now.getFullYear() + ("0" + (now.getMonth() + 1)).slice(-2) + ("0" + now.getDate()).slice(-2) + "_" + ("0" + now.getHours()).slice(-2) + ("0" + now.getMinutes()).slice(-2) + ("0" + now.getSeconds()).slice(-2);
    //fd.field('RequestNumber').value = rnumber;        
    //fd.field('RequestNumber').disabled;
};

Hello @Marcin,

If you want to set the variable value like this, the function must return value. Otherwise the variable is empty and you see the item with the empty field:

function generateIdentifier() {
    var now = new Date();
    var rnumber = "PZ_" + now.getFullYear() + ("0" + (now.getMonth() + 1)).slice(-2) + ("0" + now.getDate()).slice(-2) + "_" + ("0" + now.getHours()).slice(-2) + ("0" + now.getMinutes()).slice(-2) + ("0" + now.getSeconds()).slice(-2);
    //function returns value       
    return rnumber;  
};

Also, if you want to disable the field use this code:

fd.field('RequestNumber').disabled = true;

Hi @mnikitina,
Thank you for this advise. I applied your suggestions but still in the same place. Child list is not filtered.
I found one interesting fact in my filterAtt() in dt.ready() function. It doesn't executes. I placed some console.log commands inside the filterAtt() so you can see them working but the one in dt.ready() doesn't executes.

function filterAtt(rrnumber){
    console.log('In filterAtt:');
    console.log(rrnumber);
    var dt = fd.control('Zalaczniki');
    if (fd.control('Zalaczniki')){console.log('Control exists!');};
    dt.ready(function() { 
        var attfilter = "<Eq><FieldRef Name='RequestNumberTMP'/><Value Type='Text'>" + rrnumber +"</Value></Eq>";
        console.log(attfilter); //this line doesdn't return value of attfilter
        dt.filter = attfilter;
        dt.refresh();
    });
 }

image

What is your opinion?

@Marcin,

Please share the complete code you are using on the form and the screenshot of all errors from the browser console:

Hi,
Please find the code of both forms:

/////
///// PARENT FORM
/////

window.fd = fd;
window.$ = $;
window.pnp = pnp;

window.SendForApproval = function(){
    //alert(fd.field('status').value);
    if (fd.isValid) {
        console.log('In SendForApproval - form is valid');
        console.log(fd.field('status').value);
        if (fd.field('status').value === 'Rejestracja') {fd.field('status').value = 'AprobataL1';}
        else if (fd.field('status').value === 'AprobataL1') {fd.field('status').value = 'AprobataL2';}
        else if (fd.field('status').value === 'AprobataL2') {fd.field('status').value = 'AprobataL3';}
        else if (fd.field('status').value === 'AprobataL3') {fd.field('status').value = 'Zatwierdzone';}
        else if (fd.field('status').value === 'Zatwierdzone') {fd.field('status').value = 'Sprawdzone';}
        else if (fd.field('status').value === 'Sprawdzone') {fd.field('status').value = 'Zamkniete';}
        fd.save();
    }
    else {alert('Formularz niepoprawny!');}
}

function filterAtt(rrnumber){
    console.log('In filterAtt:');
    console.log(rrnumber);
    var dt = fd.control('Zalaczniki');
    if (dt){console.log('Control exists!');};
    dt.ready(function() {
        console.log('In dt.ready');
        var attfilter = '<Eq><FieldRef Name=\'RequestNumberTMP\'/><Value Type=\'Text\'>' + rrnumber +'</Value></Eq>';
        console.log(attfilter); //this line doesdn't return value of attfilter
        dt.filter = attfilter;
        dt.refresh();
    });
    console.log('End of filterAtt function - out of dt.ready()');
 }

function getOrg(org){
    pnp.sp.web.lists.getByTitle("Organizacje").items.select("Title", "Ksiegowe", "Team_x0020_Leader",
"Glowny_x0020_Ksiegowy","Koordynator_x0020_Uzywane","Dyrektor_x0020_Regionu", "Prezes" ,
            "Nazwa_x0020_office", "Dyrektor_x0020_Salonu")
            .filter("Title eq " + org).getAll().then(function(result) {
            fd.field('nazwaLokalizacji').value = result[0].Nazwa_x0020_office;
            console.log('NazwaOffice:');
            console.log(result[0].Nazwa_x0020_office);
            fd.field('dyrektorSalonu').value = result[0].Dyrektor_x0020_Salonu;
            fd.field('dyrektorRegionu').value = result[0].Dyrektor_x0020_Regionu;
            fd.field('prezes').value = result[0].Prezes;
            fd.field('koordynatorUzywane').value = result[0].Koordynator_x0020_Uzywane;
            fd.field('ksiegowe').value = result[0].Ksiegowe;
            fd.field('teamLeader').value = result[0].Team_x0020_Leader;
            fd.field('glownyKsiegowy').value = result[0].Glowny_x0020_Ksiegowy;
    });
};

function updateCurrentUserInfo() {
    pnp.sp.profiles.myProperties.get().then(function(result) {
        var props = result.UserProfileProperties;
        for (var i = 0; i < props.length; i++) {
            switch (props[i].Key) {
                    case 'PreferredName':
                    fd.field('zglaszajacy').value = props[i].Value;
                    console.log(props[i].Value);
                    fd.field('zglaszajacy').ready().then(function(field) {
                        if(field.value){
                          field.disabled = true;
                        }  
                    });
                    break;
                case 'Manager':
                    fd.field('personUserManager').value = props[i].Value;
                    console.log(props[i].Value);
                    break;
                case 'SAPNumber':
                    fd.field('userSAPNumber').value = props[i].Value;
                    console.log(props[i].Value);
                    getOrg(props[i].Value);
                    break;
                }
            }
    });
};

function generateIdentifier() {
        var teraz = new Date();
        var numer = "PZ_" + teraz.getFullYear() + ("0" + (teraz.getMonth() + 1)).slice(-2)
            + ("0" + teraz.getDate()).slice(-2) + "_" + ("0" + teraz.getHours()).slice(-2)
            + ("0" + teraz.getMinutes()).slice(-2) + ("0" + teraz.getSeconds()).slice(-2);
        console.log('NumerZlecenia: ');
        console.log(numer);
        return numer;
};

///////////////////////////////////////////////////
////             Running form code             ////
///////////////////////////////////////////////////

fd.spRendered(function() {
    //$('.attCount').hide();
    fd.messages.PlumsailForm_CorrectErrors = 'Uwaga! Formularz uzupełniony błędnie. Sprawdź poniższe błędy: ';
    var type2att = ['Proforma', 'Uzywane', 'Polisy'];
    var type3att = ['Zwrot do TU', 'Zwrot nadplaty', 'CEPIK'];   

    ///////////////////////////////////////////////////
    ///   Fields initially disabled and required   ////
    ///////////////////////////////////////////////////    

    fd.field('numerZlecenia').disabled = true;
    fd.field('status').disabled = true;
    fd.control('organisation').disabled = true;
    fd.field('rodzaj').required = true;   

    ///////////////////////////////////////////////////
    ////       Form behaviour on Status value      ////
    ///////////////////////////////////////////////////

    if (fd.field('status').value === 'Rejestracja') {
        var rrnumber = generateIdentifier();
        fd.field('numerZlecenia').value = rrnumber;
        updateCurrentUserInfo();
        console.log('Before filterAtt:');
        console.log(rrnumber);        
        filterAtt(rrnumber);
        fd.control('organisation').disabled = false;
        fd.control('btnSend').onclick = 'SendForApproval()';
        fd.control('btnClose').onclick = 'fd.close()';
    }   

    if (fd.field('status').value === 'AprobataL1') {
        //remember to enable required fields for this stage
        fd.control('btnSend').text = 'Zatwierd┼║';
        fd.control('btnClose').text = 'Odrzu─ç';
        fd.control('btnSend').onclick = 'SendForApproval()';
        fd.control('btnClose').onclick = 'fd.close()';
    }

    if (fd.field('status').value === 'AprobataL2') {
        //remember to enable required fields for this stage
        fd.control('btnSend').text = 'Zatwierd┼║';
        fd.control('btnClose').text = 'Odrzu─ç';
        fd.control('btnSend').onclick = 'SendForApproval()';
    }

    if (fd.field('status').value === 'AprobataL3') {
        //remember to enable required fields for this stage
        fd.control('btnSend').text = 'Zatwierd┼║';
        fd.control('btnClose').text = 'Odrzu─ç';
        fd.control('btnSend').onclick = 'SendForApproval()';
    }

    if (fd.field('status').value === 'Zatwierdzone') {
        //remember to enable required fields for this stage
        fd.control('btnSend').text = 'Sprawdzone';
        fd.control('btnClose').text = 'Odrzu─ç';
        fd.control('btnSend').onclick = 'SendForApproval()';
    }

    if (fd.field('status').value === 'Sprawdzone') {
        //remember to enable required fields for this stage
        fd.control('btnSend').text = 'Zweryfikowane';
        fd.control('btnClose').text = 'Odrzu─ç';
        fd.control('btnSend').onclick = 'SendForApproval()';
    }

    fd.field('rodzaj').$on('change', function(value){
        fd.field('Title').value = value;
        console.log(value);
        if (fd.field('rodzaj').value == ''){alert('brak wybranego rodzaju')}
    });
   
    fd.control('organisation').$on('change', function(value){
        fd.field('selectedOrg').value = value.Title;
        getOrg(value.Title);      
    });
   
    fd.control('Zalaczniki').$on('change', function(changeData) {
       if (changeData.type === 'add'){
             fd.field('attCount').value += 1 ;
             //alert(changeData.itemId);
             }
        if (changeData.type === 'delete' && fd.field('attCount').value > 0){
             fd.field('attCount').value -= 1 ;
           }      
    });
   
    fd.validators.push({
        name: 'TransferType',
        error: "Brakuje za┼é─ůcznik├│w!",
        validate: function(value) {
            if ((type2att.includes(fd.field('rodzaj').value) && fd.field('attCount').value < 2) || (type3att.includes(fd.field('rodzaj').value) && fd.field('attCount').value < 3)){ return false;}
        return true;
            }
    });
});

/////
///// CHILD FORM with lookup to Parent on 'numerZlecenia' field
/////

window.fd = fd;
window.$ = $;
fd.spRendered(function () {
    $(fd.field('NumerZleceniaTMP').$parent.$el).hide();
    fd.field('Attachments').required = true;
    var parentForm = window.top.fd;
    if (parentForm){
        //alert(parentForm.field('RequestNumber').value);
        fd.field('NumerZleceniaTMP').value = parentForm.field('numerZlecenia').value;
    }

    fd.field('Attachments').validators.push({
        name: 'AttachmentValidator',
        error: "Za┼é─ůcznik do tego dokumentu ju┼╝ jest dodany, prosz─Ö dodac kolejny dokument!",
        validate: function(value) {
            if (fd.field('Attachments').value.length > 1 ) {
                return false;
            }
            else {
                return true;
            }
        }
    });
});

And console screenshots:

Hello @Marcin,

Try updating the code like this:

function filterAtt(rrnumber){
    console.log('In filterAtt:');
    console.log(rrnumber);
    var dt = fd.control('Zalaczniki');
    if (dt){console.log('Control exists!');};
    dt.ready().then(function(dt) { //updated line
        console.log('In dt.ready');
        var attfilter = '<Eq><FieldRef Name=\'RequestNumberTMP\'/><Value Type=\'Text\'>' + rrnumber +'</Value></Eq>';
        console.log(attfilter); //this line doesdn't return value of attfilter
        dt.filter = attfilter;
        dt.refresh();
    });
    console.log('End of filterAtt function - out of dt.ready()');
 }

Hi,
It works great.
Thank you!

1 Like