function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raj R.Raj R. 

How to create a custom picklist field from another picklist using apex class?

Hi,

I was wondering if there is a way to take a picklist and it's values from one object and using apex class, we create another picklist and the same values on another object.

Essentially, i want to take the Campaign Type field (format: picklist) and its various values and create a picklist field on customObj1__c that has the same picklist values. How would this be possible? I am having trouble trying to figure out how I can use apex class to do this. 

Another scenario that would be okay, if we assume that the customObj1__c already has a picklist field fieldPick__c and certain values. Is there a way to take the Campaign type picklist values and save them as the fieldPick__c values on the customObj1__c object?
Best Answer chosen by Raj R.
pigginsbpigginsb
By pulling the Campaign object into Eclipse, you can view the Type field metadata as shown below.
<fields>
        <fullName>Type</fullName>
        <picklist>
            <picklistValues>
                <fullName>Conference</fullName>
                <default>true</default>
            </picklistValues>
            <picklistValues>
                <fullName>Webinar</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Trade Show</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Public Relations</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Partners</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Referral Program</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Advertisement</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Banner Ads</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Direct Mail</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Email</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Telemarketing</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Other</fullName>
                <default>false</default>
            </picklistValues>
            <sorted>false</sorted>
        </picklist>
        <type>Picklist</type>
    </fields>
I think my approach would be to create the new picklist field on the custom object, then also pull that object into Eclipse, and paste only the picklist value metadata from the Type field into the custom field. Then you can save the metadata to server.

Keep in mind that if you are using RecordTypes, then you would still need to manually assign the values to each RecordType.

This does not use an Apex class, but I believe this is more along the lines of what you are looking to accomplish. An Apex class cannot be used to update metadata.
 

All Answers

James LoghryJames Loghry
Are you trying to do this on a repetitive basis, or as a one-off scenario?  You can create picklist fields (and values) utilizing the Tooling API, which is either a SOAP or REST API, but it's quite the effot to get up and running.  If you can get by with doing it manually though, I would do that instead.
pigginsbpigginsb
By pulling the Campaign object into Eclipse, you can view the Type field metadata as shown below.
<fields>
        <fullName>Type</fullName>
        <picklist>
            <picklistValues>
                <fullName>Conference</fullName>
                <default>true</default>
            </picklistValues>
            <picklistValues>
                <fullName>Webinar</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Trade Show</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Public Relations</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Partners</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Referral Program</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Advertisement</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Banner Ads</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Direct Mail</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Email</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Telemarketing</fullName>
                <default>false</default>
            </picklistValues>
            <picklistValues>
                <fullName>Other</fullName>
                <default>false</default>
            </picklistValues>
            <sorted>false</sorted>
        </picklist>
        <type>Picklist</type>
    </fields>
I think my approach would be to create the new picklist field on the custom object, then also pull that object into Eclipse, and paste only the picklist value metadata from the Type field into the custom field. Then you can save the metadata to server.

Keep in mind that if you are using RecordTypes, then you would still need to manually assign the values to each RecordType.

This does not use an Apex class, but I believe this is more along the lines of what you are looking to accomplish. An Apex class cannot be used to update metadata.
 
This was selected as the best answer
Raj R.Raj R.
Hi all,

Thanks for you responses. It seems this has to be done manually as suggested or through API.