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
Mohammed ArshadMohammed Arshad 

How to pass a value of one object to a picklist on another object

Can anyone help me know on how to acheive the below scenario:
1) I have two Objects called BooksDealer__c & BooksDistributor__c 
2) BooksDealer__c has the below fields:-
    a) Name(Text)
    b)LicenseNumber__c(Text)
    c)ActiveFrom__c(Number)
3)BooksDistributor__c has the below fields:-
   a)Dealers__c(Picklist)

So now the requirement is whenever there is a new BooksDealer__c is created then the 'Name' field value should be added in the Dealers__c picklist field.

Note:- Name is a Text field in BooksDealer__c object  and Dealers__c is a Picklist field in BooksDistributor__c Object.
Also these two objects doesn't have any relationships between them.
Waqar Hussain SFWaqar Hussain SF
Hi Arshad,

You will have to use Salesforce MetaData API to add picklist values dynamically. So basically you will have to develop a trigger on BooksDistributor__c object. So whenevet a new bookDistributor record will be created, using meta data api a new picklist value will be added. 

See
https://salesforce.stackexchange.com/questions/11937/how-can-i-have-a-dynamic-picklist-that-gets-updated-when-the-new-value-is-absent
Mohammed ArshadMohammed Arshad
Thanks for your response Waqar!!!!
Objects:
(A)BooksDealer__c
(B)BooksDistributor__c

When BooksDealer__c record is created then the value of this object's field need to be added in another object i.e BooksDistributor__c picklist field(Dealers__c).
I am bit confused here..... Trigger on A or B ?
Can you please share the Trigger & MetaData API Code here for the appropriate object..
Waqar Hussain SFWaqar Hussain SF
Sorry the after insert trigger needs to be created on BooksDealer__c object. 

Setp 1:
Deploy the metadata api classes to you salesforce instance using deploy to Salesforce button by below URL
https://github.com/financialforcedev/apex-mdapi

Step 2:
Then from the apex trigger call the metadata api to add values to the custom picklist field. 
See example 
https://salesforce.stackexchange.com/questions/65774/how-can-i-add-a-new-value-to-a-picklist-using-apex
 
Trigger BooksDealerTrigger on BooksDealer__c (after insert){
	
	for(BooksDealer__c bd : trigger.new){
	
		MetadataService.MetadataPort service = createService();             

		// Read Custom Field
		MetadataService.CustomField customField = 
			(MetadataService.CustomField) service.readMetadata('CustomField', 
				new String[] { 'BooksDistributor__c.Dealers__c' }).getRecords()[0];       

		// Add pick list values
		metadataservice.PicklistValue val = new metadataservice.PicklistValue();
		val.fullName= bd.Name;
		val.default_x=false;
		customField.picklist.picklistValues.add(val);

		// Update Custom Field
		handleSaveResults(
			service.updateMetadata(
				new MetadataService.Metadata[] { customField })[0]); 
	}
}
t

 
Mohammed ArshadMohammed Arshad
Thanks Waqar...

I have deployed the metadata api to my Salesforce instance and then added the above trigger on the object.

When i am adding the values and trying to save the record in BooksDealer__c object it gives me out the below error...
Error: Apex trigger BooksDealerTrigger caused an unexpected exception, contact your administrator: BooksDealerTrigger: execution of AfterInsert caused by: System.CalloutException: Callout from triggers are currently not supported.: Class.MetadataService.MetadataPort.readMetadata: line 11578, column 1

Awaiting for your prompt response..
 
Waqar Hussain SFWaqar Hussain SF
Hi Arshad, 

Basically the api callouts from apex trigger are not supported. You can create a future method for api calls. See below

Create an apex class 
public class BooksDealerTriggerController{

	@future (callout = true)
	public static void AddValueToDealers(string fieldValue){
	
		MetadataService.MetadataPort service = createService();             

		// Read Custom Field
		MetadataService.CustomField customField = 
			(MetadataService.CustomField) service.readMetadata('CustomField', 
				new String[] { 'BooksDistributor__c.Dealers__c' }).getRecords()[0];       

		// Add pick list values
		metadataservice.PicklistValue val = new metadataservice.PicklistValue();
		val.fullName= fieldValue;
		val.default_x=false;
		customField.picklist.picklistValues.add(val);

		// Update Custom Field
		handleSaveResults(
			service.updateMetadata(
				new MetadataService.Metadata[] { customField })[0]); 
	}
}

Then update you trigger as below
Trigger BooksDealerTrigger on BooksDealer__c (after insert){
	
	for(BooksDealer__c bd : trigger.new){
		BooksDealerTriggerController.AddValueToDealers(bd.Name);		
	}
}