Create Mapped DocumentUtilize the Frappe get_mapped_doc API server-side to automate document mapping from source to target DocTypes with custom field maps and value setters.
In ERPNext development, automating the creation of documents based on existing records accelerates business workflows and reduces errors. This snippet demonstrates using Frappe's get_mapped_doc function in a server script to map fields from one DocType (such as Meeting Schedule or Sauda Details) to another (e.g., Meetings or Sales Order), handling different field names and custom child table data. This approach is essential when implementing custom buttons or triggers that transform data according to unique business logic within ERPNext.
1 // Import below library 2 from frappe.model.mapper import get_mapped_doc 3
4 @frappe.whitelist() 5 def make_meeting(source_name, target_doc=None): 6 7 doclist = get_mapped_doc("Meeting Schedule", source_name, { 8 "Meeting Schedule":{ 9 "doctype": "Meetings", 10 "field_map": { 11 "meeting_from": "scheduled_from", 12 "meeting_to": "scheduled_to" 13 } 14 } 15 }, target_doc) 16 17 return doclist 18
19 // Another Example for mapping different field names 20 @frappe.whitelist() 21 def make_so(source_name, target_doc=None): 22 def set_missing_values(source, target): 23 ignore_errors=True 24 target.append("items", { 25 "item_code": source.item, 26 "item_name": source.item_name, 27 "description": source.item_name, 28 "uom": source.uom, 29 "stock_uom": source.stock_uom, 30 "qty":source.available_qty, 31 "rate":source.rate 32 }) 33 doclist = get_mapped_doc("Sauda Details", source_name, { 34 "Sauda Details": { 35 "doctype": "Sales Order", 36 "field_map": { 37 "name": "sauda_ref", 38 "shipment_to": "delivery_date", 39 } 40 }, 41 }, target_doc, set_missing_values) 42 return doclist
Automates creation of a new DocType document by mapping fields from an existing document using get_mapped_doc. Handles custom field mappings and allows custom logic to set additional values.
Use in server-side triggers such as @frappe.whitelist methods called from buttons or API endpoints when data from one DocType needs to be transformed and saved as another.
Important ideas to understand in this code
Frappe API to map and create new documents based on a source document, specifying field mappings and custom callbacks.
Learn moreDecorator to expose server-side Python methods for client-side calls or via REST API.
Learn moreFollow along to understand how this code works
Import the helper method from frappe.model.mapper to utilize its powerful document mapping capabilities.
from frappe.model.mapper import get_mapped_docCreate a whitelisted function that takes a source document name and optionally a target document to populate or create.
@frappe.whitelist()
def make_meeting(source_name, target_doc=None):Configure the from-to field mapping in a dictionary with the source DocType as key and a mapping with the target DocType and any necessary field maps.
doclist = get_mapped_doc("Meeting Schedule", source_name, {"Meeting Schedule": {"doctype": "Meetings", "field_map": {"meeting_from": "scheduled_from", "meeting_to": "scheduled_to"}}}, target_doc)For complex transformations like mapping child tables or setting missing values, define a callback function that modifies the target document.
def set_missing_values(source, target):
target.append("items", {
"item_code": source.item,
"item_name": source.item_name,
"description": source.item_name,
"uom": source.uom,
"stock_uom": source.stock_uom,
"qty": source.available_qty,
"rate": source.rate
})Return the mapped document list so it can be saved or further processed.
return doclistTroubleshoot problems you might encounter