Item Price Created based on Item

Dear Community,

When i create Item, Item Price should create automatically and make price list as “Standard Buying”

Am using server script as After Save

my issue is code works fine until it save if i update the rate item price rate not update and also i need to update valid from date when the update happens

# Triggered after an Item is created or updated
if doc.valuation_rate:
    # Check if an Item Price already exists for this item and price list
    existing_price = frappe.db.get_value('Item Price', {
        'item_code': doc.item_code,
        'price_list': 'Standard Buying'
    }, 'name')

    if existing_price:
        # If an Item Price exists, update the Rate if needed
        item_price = frappe.get_doc('Item Price', existing_price)
        item_price.price_list_rate = doc.valuation_rate  # Update with new valuation rate
        item_price.save(ignore_permissions=True)  # Save changes with permission bypass
        frappe.msgprint(f"Item Price for {doc.item_code} updated successfully.")
    else:
        # If no Item Price exists, create a new one
        item_price = frappe.get_doc({
            'doctype': 'Item Price',
            'item_code': doc.item_code,
            'price_list': 'Standard Buying',
            'price_list_rate': doc.valuation_rate,
            'valid_from': doc.creation
        })
        item_price.insert(ignore_permissions=True)  # Insert without permissions for automation
        frappe.msgprint(f"Item Price created successfully for {doc.item_code}.")

Kindly help to solve the issue
Thank you

Please apply the code and check it.

if doc.valuation_rate:
    existing_price = frappe.db.get_value('Item Price', {
        'item_code': doc.item_code,
        'price_list': 'Standard Buying'
    }, 'name')

    if existing_price:
        item_price = frappe.get_doc('Item Price', existing_price)
        item_price.price_list_rate = doc.valuation_rate
        item_price.valid_from = frappe.utils.nowdate()
        item_price.save(ignore_permissions=True)
        frappe.msgprint(f"Item Price for {doc.item_code} updated successfully.")
    else:
        item_price = frappe.get_doc({
            'doctype': 'Item Price',
            'item_code': doc.item_code,
            'price_list': 'Standard Buying',
            'price_list_rate': doc.valuation_rate,
            'valid_from': frappe.utils.nowdate()
        })
        item_price.insert(ignore_permissions=True)
        frappe.msgprint(f"Item Price created successfully for {doc.item_code}.")
1 Like