Make a doc submit via client scrip

Hello Guys
I created a doc by frappe.db.insert () method.
and I want to make it submittable through a custom script while creating (The created doc is a submittable doc).

Is there any option? Please Help me.

Thanks

Hi @jinsy,

Please check it.

// save form
frm.save();

// submit form
frm.save('Submit');

// cancel form
frm.save('Cancel');

// update form (after submit)
frm.save('Update');

More details for check it, Please.
https://frappeframework.com/docs/v13/user/en/api/form#frmsave

Thank You!

I did this but not working.

This is my code

update_salary:function(frm,cdt,cdn)
{
// msgprint(“Hello”)

    let d = new frappe.ui.Dialog({
title: 'Enter new salary details',
fields: [
     {
        label: 'New Salary',
        fieldname: 'new_salary',
        fieldtype: 'Currency'
    },
    {
        label: 'From Date',
        fieldname: 'from_date',
        fieldtype: 'Date'
    },
   
],
primary_action_label: 'Submit',
primary_action(values) {
    console.log(values);
    frappe.db.insert
                    ({
                     doctype:'Salary Structure Assignment',
                
                      employee:frm.doc.name,
                      status:"Submitted",
                      //employee_name:frm.doc.interview_round,
                      salary_structure:"Webeaz Salary Structure",
                      from_date:values.from_date,
                      base:values.new_salary,
                    })
                    
                    .then(function(doc)
                    { 
                        msgprint(`Salary structure assignment created for ${doc.employee}`);
                    
                    }); 
    //frm.save('Submit');
    //frm.set_value("new_date","values.from_date")
    d.hide();
}

Did you try this?

// for save
frm.doc.save()

// for submit
frm.doc.save('Submit')

Hi @jinsy,

I think submit script does not work so only save script work.

cur_frm.save();

Thanks.

Thanks

I guess a workaround solution could be changing

to

.then(function(doc)
                    { 
                        let current_doc = frm.doc;

                         // use the created document for frm.
                        frm.doc = doc;
                        frm.doctype = doc.doctype;
                        frm.docname = doc.name;


                        frm.save("Submit");


                           // switch back to default document.
                        frm.doc = current_doc;
                        frm.doctype = current_doc.doctype;
                        frm.docname = current_doc.name;
                    }); 
    //frm.save('Submit');
    //frm.set_value("new_date","values.from_date")
    d.hide();

Hi @amadhaji, Thanks for the reply.

This is my code but it shows me an error.

   .then(function(doc)
                { 
                    let current_doc = frm.doc
                   frm.salary_structure_assignment = doc;
                    frm.doctype = doc.doctype;
                    frm.docname = doc.name;


                    frm.save("Submit");
			console.log("salary structure assigment submitted")

                       // switch back to default document.
                    frm.employee = current_doc;
                    frm.doctype = current_doc.doctype;
                    frm.docname = current_doc.name;
                    frm.save
                    console.log("Employee saved")
                });

what is the error?

However

has to be

frm.doc = doc;

You need to change the frm.doc because when submit, that is the frm.doc which submitted.

then switch back using

frm.doc = current_doc;

sorry! My mistake, switching back to default document. need to be after done submitting not after call

                  frm.save("Submit").then(function(){
                    console.log("salary structure assigment submitted")

                       // switch back to default document.
                    frm.doc = current_doc;
                    frm.doctype = current_doc.doctype;
                    frm.docname = current_doc.name;
                    frm.save();
                    console.log("Employee saved");
                  })

Thanks to @amadhaji

for help on this, but it again did not work for me.

Now this is my code

   .then(function(doc)
                    { 
                        msgprint(`Salary structure assignment created for ${doc.employee_name}`);
                    
                    
                    
                        let current_doc = frm.doc.salary_structure_assignment
                        frm.salary_structure_assignment = doc; 
                        frm.doctype = doc.doctype; 
                        frm.docname = doc.name; 
                        frm.save("Submit"); 
                        console.log("salary structure assigment submitted")                  
                    });

show me some permission errors " Insufficient Permission for Employee".

change this to

    frm.doc = doc;

@amadhaji
Thank you for helping me continuously.
It is working, I can submit the salary structure assignment.
But employee doc can’t save after submission of salary structure assignment.

@amadhaji This is my code now

update_salary:function(frm,cdt,cdn)
{
    
    
let d = new frappe.ui.Dialog({
title: 'Enter details',
fields: [
    {
        label: 'New Salary',
        fieldname: 'new_salary',
        fieldtype: 'Currency',
        "reqd": 1,
    },
    
    { 
        label: 'From Date',
        fieldname: 'from_date',
        fieldtype: 'Date',
        "reqd": 1,
    }
],
primary_action_label: 'Submit',
primary_action(values) 
{
    
    frappe.db.insert
                    ({
                     doctype:'Salary Structure Assignment',
                
                      employee:frm.doc.name,
                      salary_structure:"Webeaz Salary Structure",
                      from_date:values.from_date,
                      base:values.new_salary,
                    })
                    
                    .then(function(doc)
                    { 
                        msgprint(`Salary structure assignment created for ${doc.employee_name}`);
                    
                    
                    
                         let current_doc = frm.doc.salary_structure_assignment
                         frm.doc = doc; 
                         frm.doctype = doc.doctype; 
                         frm.docname = doc.name; 
                         frm.save("Submit"); 
                         console.log("salary structure assigment submitted")                  
                    });
      
               
                    
    
    console.log(values);
    frm.set_value("allocated_salary",values.new_salary)
      frm.set_value("effective_from",values.from_date)
    d.hide();
    
    
    var g1=["1","2","3","4","5"];
     var a = ["Basic","HRA","Conveyance","Medical Allowance","Special Allowance","Monthly CTC","Annual CTC"];
     var s1=[];
     var s2=[];
     var s3=[];
     var s6=[];
     
     
      console.log(frm.doc.salary_breakup_details,"----------------------")
      console.log(frm.doc.allocated_salary,"++++++++++++++++")
      
      
       var sum = flt(frm.doc.allocated_salary) * .4
       s1.push(sum)
       
       var hra = flt(sum) * .4
       s1.push(hra)
        
       var con = flt(sum) * .4
       s1.push(con)    
                 
       var med=1250
       s1.push(med)
       
       
      
                    
        var spl = flt(frm.doc.allocated_salary) - flt(sum) - flt(hra) -flt(con) - flt(1250)
        s1.push(spl)
       
       var monthly = flt(frm.doc.allocated_salary)
       s1.push(monthly)
       var annual = flt(frm.doc.allocated_salary) * 12
       s1.push(annual)
       console.log("Anual salary is ",annual)
       console.log(s1,"=======================")
       
     
     if(frm.doc.salary_breakup_details.length===0)
         {
                         cur_frm.clear_table("salary_breakup_details");
                        
                         for(var i=0;i<a.length;i++)
                               
                            {
                                
                                     var child = cur_frm.add_child("salary_breakup_details");
                                     frappe.model.set_value(child.doctype, child.name, "salary_component",a[i]);
                                     frappe.model.set_value(child.doctype, child.name, "revised_amount",s1[i]);
                                     
                            }
                        cur_frm.refresh_field("salary_breakup_details");
                    }
                    
                    
     else
     {
         
         
         $.each(frm.doc.salary_breakup_details, function(i, item) 
                {
         
                  s2[i]=item.salary_component
                  s3[i]=item.revised_amount
                  
                   var y=((s1[i]-s3[i])/s3[i])*100
                   s6.push(y)
                   console.log(s6,"iiiiiiiiiiiiiii")
                  
                   
                }) 
                cur_frm.clear_table("salary_breakup_details");
                
                    for(var j=0;j<s2.length;j++)
                    {
                        
                      
                        var child = cur_frm.add_child("salary_breakup_details");
                        frappe.model.set_value(child.doctype, child.name, "salary_component",s2[j]);
                        frappe.model.set_value(child.doctype, child.name, "previous_amount",s3[j]);
                        frappe.model.set_value(child.doctype, child.name, "revised_amount",s1[j]);
                        
                        if(s6[j]=="0")
                        {
                        frappe.model.set_value(child.doctype, child.name, "salary_increment_in_","0");
                        }
                        else
                        {
                           frappe.model.set_value(child.doctype, child.name, "salary_increment_in_",s6[j]); 
                        }
                        
                       
                        
                        
                    }
                   
                  cur_frm.refresh_field("salary_breakup_details");
                
                 
                
     
     }
    
}


});

 d.show();
    //     cur_frm.save();
     // d.hide();














 }

while clicking a button salary structure assignment is automatically created, and we get the salary breakup details.

I added a code to submit the salary structure assignment and it works.
but it does not update the salary break up details, which means after submitting the salary assignment, the code did not switch back to the employee form back

      // switch back to the default document.
                frm.employee = current_doc;
                frm.doctype = current_doc.doctype;
                frm.docname = current_doc.name;
                frm.save
                console.log("Employee saved")

Where do I add this code?

first change to:

frm.doc = current_doc;
let current_doc = frm.doc
frm.salary_structure_assignment = doc;
frm.doctype = doc.doctype;
frm.docname = doc.name;

frm.save("Submit").finally(function(){
    // switch back to the default document.
       frm.doc = current_doc;
       frm.doctype = current_doc.doctype;
       frm.docname = current_doc.name;



       // <your-code to update employee here>

            console.log(values);
    frm.set_value("allocated_salary",values.new_salary)
      frm.set_value("effective_from",values.from_date)
    d.hide();
    
    
    var g1=["1","2","3","4","5"];
     var a = ["Basic","HRA","Conveyance","Medical Allowance","Special Allowance","Monthly CTC","Annual CTC"];
     var s1=[];
     var s2=[];
     var s3=[];
     var s6=[];
     
     
      console.log(frm.doc.salary_breakup_details,"----------------------")
      console.log(frm.doc.allocated_salary,"++++++++++++++++")
      
      
       var sum = flt(frm.doc.allocated_salary) * .4
       s1.push(sum)
       
       var hra = flt(sum) * .4
       s1.push(hra)
        
       var con = flt(sum) * .4
       s1.push(con)    
                 
       var med=1250
       s1.push(med)
       
       
      
                    
        var spl = flt(frm.doc.allocated_salary) - flt(sum) - flt(hra) -flt(con) - flt(1250)
        s1.push(spl)
       
       var monthly = flt(frm.doc.allocated_salary)
       s1.push(monthly)
       var annual = flt(frm.doc.allocated_salary) * 12
       s1.push(annual)
       console.log("Anual salary is ",annual)
       console.log(s1,"=======================")
       
     
     if(frm.doc.salary_breakup_details.length===0)
         {
                         cur_frm.clear_table("salary_breakup_details");
                        
                         for(var i=0;i<a.length;i++)
                               
                            {
                                
                                     var child = cur_frm.add_child("salary_breakup_details");
                                     frappe.model.set_value(child.doctype, child.name, "salary_component",a[i]);
                                     frappe.model.set_value(child.doctype, child.name, "revised_amount",s1[i]);
                                     
                            }
                        cur_frm.refresh_field("salary_breakup_details");
                    }
                    
                    
     else
     {
         
         
         $.each(frm.doc.salary_breakup_details, function(i, item) 
                {
         
                  s2[i]=item.salary_component
                  s3[i]=item.revised_amount
                  
                   var y=((s1[i]-s3[i])/s3[i])*100
                   s6.push(y)
                   console.log(s6,"iiiiiiiiiiiiiii")
                  
                   
                }) 
                cur_frm.clear_table("salary_breakup_details");
                
                    for(var j=0;j<s2.length;j++)
                    {
                        
                      
                        var child = cur_frm.add_child("salary_breakup_details");
                        frappe.model.set_value(child.doctype, child.name, "salary_component",s2[j]);
                        frappe.model.set_value(child.doctype, child.name, "previous_amount",s3[j]);
                        frappe.model.set_value(child.doctype, child.name, "revised_amount",s1[j]);
                        
                        if(s6[j]=="0")
                        {
                        frappe.model.set_value(child.doctype, child.name, "salary_increment_in_","0");
                        }
                        else
                        {
                           frappe.model.set_value(child.doctype, child.name, "salary_increment_in_",s6[j]); 
                        }
                        
                       
                        
                        
                    }
                   
                  cur_frm.refresh_field("salary_breakup_details");
       // <end-your-code>





         console.log("Employee saved")
});

Better Solution

.then(function(doc)
                    { 
                         // Salary Structure Assignment doc has inserted.

                        frappe.call({
                            method: "frappe.desk.form.save.savedocs",
                            args: {
                                doc: doc,            // doc to be saved
                                action: "Submit",
                            },
                            callback: function(r){
                                // Salary Structure Assignment doc has submitted.
                                // I guess your code to update employee should be here.

                                // console.log(values);
                                // frm.set_value("allocated_salary",values.new_salary)
                               // ....
                            }
                        });
                    });