Custom field as select type but i need to add options as doctype records

In Opportunity, I added a custom field with select type but I need to add Sales Stage records as options for that and it must be displayed on options because if it doesn’t show in the Kanban board, it will show noting. please help in this

Yaswanth,
You can proceed with two ways.

  1. Select Field: In select field you have to hard-code the values you want to show in list. This can be added in options field.
  2. Link Field: In this you can add a reference to another doctype instead of hard coding your values.

Depending on you use case use one of the following.
For more details refer: Frappe Field Types

Hi @Sandeep_Kakde

I added it by creating a custom field and it’s working fine this is the code

py file:

import frappe
import subprocess
import json
import erpnext



def update_opportunity_sales_stage_1():
    lists = frappe.get_all("Sales Stage",as_list=True)
    flattened_list = [item for sublist in lists for item in sublist]
    return flattened_list
   

def migrate():
    command = ["bench", "migrate"]

    # Run the command
    try:
        result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
        print("Command output:", result.stdout)
    except subprocess.CalledProcessError as e:
        print("Command failed with error:", e)
        print("Error output:", e.stderr)



@frappe.whitelist(allow_guest=True) 
def write_options(doc="",method=""):
    ind = 0
    file_path= '/home/yash/Demohr/apps/hyde_app/hyde_app/hyde/custom/opportunity.json'
    new_options = update_opportunity_sales_stage_1()
    if doc:
        item_index = new_options.index(doc.name)
        if item_index != -1:
            new_options.pop(item_index)
    new_options ="\n".join(new_options)
    # Load the JSON content from the file
    with open(file_path, 'r') as json_file:
        data = json.load(json_file)
    
    for i in range(len(data["custom_fields"])):
        if data["custom_fields"][i]["fieldname"] == "custom_test":
          ind = i
          break

    data['custom_fields'][ind]['options'] = new_options
    with open(file_path, 'w') as json_file:
        json.dump(data, json_file,indent=1)
    
    migrate()

and I created js file in public/js path and added in hooks.py

js file:

frappe.ui.form.on("Sales Stage", {
    after_save: function(frm){
        frappe.call({
            method: "hyde_app.www.test.write_options",
            args:{}
        }).done((r)=>{
            console.log(r)
        })
    }
});

Here I need your help with the Sales Stage doctype there is only one file and someone added a custom field but I pulled that exported file now that the sales stage doctype is not working it’s not deleting and not adding any records it’s showing like this

Don’t add field using code. Use Customize Form for adding the custom fields.

@Sandeep_Kakde Hello, Sandeep in the Py file I am not adding the field I am updating the options values, it’s working but in the server, there is issue while migrating and I found another approach with pyhton

import frappe
def update_custom_select_field():
    sales_stages = frappe.get_all("Sales Stage", filters={}, fields=["stage_name"])
    stage_names = [stage.get("stage_name") for stage in sales_stages]
    # Ensure each option is on a separate line with no extra whitespace
    options_string = "\n".join(stage_names)
    
    opportunity_meta = frappe.get_meta("Opportunity")
    custom_select_field = opportunity_meta.get_field("custom_test")
    
    custom_select_field.options = options_string
    opportunity_meta.save()

it’s not updating in the options what is the issue in this?

Why not use link field here? What’s your exact use case?