Custom Script Populate Document

Say I have the following custom script:

frappe.call({
    method: "app.app.doctype.sales_invoice.sales_invoice.onload",
    args: {
        self: frm.doc
    },
    freeze: true,
    callback: function(r) {
        if (r.message) {
            cur_frm.doc = r.message;
        }
    }
});

the onload method returns the entire document. So essentially, r.message contains all the data of the Sales Invoice document after some manipulation. How can I get the data to be shown on the current form? I tried (as above) cur_frm.doc = r.message and even followed by refresh_fields but the form doesnt get updated. I understand probably set_value would work, but that would mean that I have to do it for every single field. Is there an easier way?

FYI the document is also local (i.e. never saved even once).

you will need to iterate the r.message and set the value for each field using set_value

$.each(r.message, function(field, value){
	frm.set_value(field, value)
})

@makarand_b Thanks! works for the main form. Would you have anything to recommend to fill the child tables? This doesnt work well with them.

Use
frappe.model.set_value(doctype, name, field_name, value_to_be_set)

@tundebabzy thanks!