How to fetch table column in javascript and compare it with String values

Hi there,
I am trying to implement validation for Job applicant. I have created a child table in Job Applicant named “Job Interview” and want to fetch applicant status valuefrom it and compare it with String value Cleared i.e. If the applicant has cleared all the interview and applicant status in Job Interview is set to Cleared then the Applicant will be eligible for making a Job Offer.
So my problem is i am unable to fetch column values of Applicant Status from table and compare it with String Cleared

Maybe you can do something like below,something like that in python would work.

class JobApplicant(Document):
def validate(self):

            for jobinterview in self.job_interview:
                ""do your validation here""

For JS,maybe you could try frappe.ui.form

2 Likes

@Ujjawal

frappe.ui.form.on('Job Applicant', {
   function(cdt, cdn, doc){
     for (interview in cdt.job_interview) {
       if (interview.status === cleared) {
        // Your logic goes overe here...
     }
    }
   }
  }
);

Hope this helps you… :slight_smile:

1 Like

Still Unable to fetch data to compare the Values.
I want it in Javascript to compare.
the for loop which you provided wont work in javascript.

@Ujjawal,

Can you share your script ?

frappe.ui.form.on(“Job Applicant”, {
refresh: function(frm,cdt) {
if (!frm.doc.__islocal) {
if (frm.doc.__onload && frm.doc.onload.offer_letter) {
frm.add_custom_button(
(“Offer Letter”), function() {
if(frm.doc.status==‘Replied’){
frappe.set_route(“Form”, “Offer Letter”, frm.doc.__onload.offer_letter);
}
else {
frappe.msgprint(“Change the status to Replied to view the offer letter”);
}
},
__(“View”));

        } else {
            frm.add_custom_button(__("Offer Letter"), function() {
            for (var i=0; i<len(cdt.job_document);i++) {
          if (frm.job_document.applicant_status == "Cleared") {
                    if(frm.doc.status=='Replied'){
                    frappe.route_options = {
                        "job_applicant": frm.doc.name,
                        "applicant_name": frm.doc.applicant_name,
                        "designation": frm.doc.job_opening,
                    };
                    frappe.new_doc("Offer Letter");
                }

                    else{
                        frappe.msgprint("Change the status to Replied to make the offer letter");
                    }
                }
                else {
                    frappe.msgprint("Applicant has Failed in one of the Interview Still Want to make him an Offer Letter");
                }
            }
                }, __("Make"));
                cur_frm.page.set_inner_btn_group_as_primary(__("Make"));
        }
    }

}

});

For condition doesnt work.!

refresh trigger only takes frm as argument

assuming the child table field name is job_document, please replace the cdt by frm.doc
try for(interview in frm.doc.job_document) instead.

the code still Doesnt Work
@Sangram can u have a look at it.
Let me know what i am doing wrong
Code is below

else {
frm.add_custom_button(__(“Offer Letter”), function() {
for (var i=0; i<len(frm.doc.job_document);i++) {
if (frm.job_document.applicant_status == “Cleared”) {
if(frm.doc.status==‘Replied’){
frappe.route_options = {
“job_applicant”: frm.doc.name,
“applicant_name”: frm.doc.applicant_name,
“designation”: frm.doc.job_opening,
};
frappe.new_doc(“Offer Letter”);
}

                    else{
                        frappe.msgprint("Change the status to Replied to make the offer letter");
                    }
                }
                else {
                    frappe.msgprint("Applicant has Failed in one of the Interview Still Want to make him an Offer Letter");
                }
            }
                }, __("Make"));
                cur_frm.page.set_inner_btn_group_as_primary(__("Make"));
        }

@Ujjawal,

please check the browser console for error logs.

len is not defined

len is python method for javascript use the frm.doc.job_document.length, though better alternative would to use the

$.each(frm.doc.job_document, function(idx, obj){
// your code
})

job_document is a table Doctype so will the length function work for it?

Hi there @makarand_b
I am still unable to perform the task.
Can u please look into my code what may be the issue.
Console doesnt show any error.
and also my if condition is not working it is unable to fetch the data.
It is continuously going under the else condition.
Please throw some light to it.!

frappe.ui.form.on(“Job Applicant”, {
refresh: function(frm) {
if (!frm.doc.__islocal) {
if (cur_frm.doc.status==‘Replied’) {
if (frm.doc.__onload && frm.doc.onload.offer_letter) {
frm.add_custom_button(
(“Offer Letter”), function() {
frappe.set_route(“Form”, “Offer Letter”, frm.doc.__onload.offer_letter);
}, (“View”));
} else {
frm.add_custom_button(
(“Offer Letter”), function() {
for(var i=0;i<cur_frm.doc.job_interview.length;i++){
if(frm.doc.job_interview.applicant_status===‘Cleared’){
frappe.route_options = {
“job_applicant”: frm.doc.name,
“applicant_name”: frm.doc.applicant_name,
“designation”: frm.doc.job_opening,
};
frappe.msgprint(“Applicant has cleared all the interview”);
frappe.new_doc(“Offer Letter”);
}
else {
frappe.msgprint(“Applicant has failed cannot generate offer letter.”);
}
}
}, (“Make”));
cur_frm.page.set_inner_btn_group_as_primary(
(“Make”));
}
}
}
}
});

Can u ellaborate the usage of $.each i mean how can i use the it in my script.!

Thanks done!
Thank you @makarand_b