+ Start a Discussion
PS81PS81 

Metadata API : dynamic picklist update - picklistvalue in a loop

I had retrieved the list of user names that is to be updated via metadata api to the custom picklist in the sobj 'contact'. Question is how do i update the picklist values in the loop?
 
MetadataAPIService.MetadataPort service = createService();
    service.SessionHeader.sessionId = sessionId;
    MetadataAPIService.CustomField customField = new MetadataAPIService.CustomField();
    customField.fullName = 'Contact.Test__c'; 
    customField.label = 'Test';
    customField.type_x = 'Picklist'; //customField.type = 'Picklist';
    metadataAPIservice.Picklist sown = new metadataAPIservice.Picklist();
    sown.sorted= false;
    metadataAPIservice.PicklistValue pval = new metadataAPIservice.PicklistValue();
    pval.fullName= 'SHP Picked';
    pval.default_x=false ;
    // Get the user list
    list<user> users = getUserList();
    for (User usr:users){
        //what to do in to insert the list into picklistValues?
    }
    sown.picklistValues = new List<MetadataAPIService.PicklistValue>{pval};

 
Best Answer chosen by PS81
scottbcovertscottbcovert
Hi Prabhu,

I believe you can define an empty list of type MetadataAPIService.PicklistValue prior to the for loop like this:
 
list<metadataAPIservice.PicklistValue> dynamicPVals = new list<metadataAPIservice.PicklistValue>();
Then on line 15 you'd basically be copying lines 9-11:
 
matadataAPIservice.PicklistValue temp = new metadataAPIservice.PicklistValue();
temp.fullName = usr.Name;
temp.default_x = false;
dynamicPVals.add(temp);

After the for loop you can add pval to the dynamicPVals list and update line 17 to use this list:
 
dynamicPVals.add(pval);
sown.picklistValues = dynamicPVals;

Hope that helps,
Scott

 

All Answers

scottbcovertscottbcovert
Hi Prabhu,

I believe you can define an empty list of type MetadataAPIService.PicklistValue prior to the for loop like this:
 
list<metadataAPIservice.PicklistValue> dynamicPVals = new list<metadataAPIservice.PicklistValue>();
Then on line 15 you'd basically be copying lines 9-11:
 
matadataAPIservice.PicklistValue temp = new metadataAPIservice.PicklistValue();
temp.fullName = usr.Name;
temp.default_x = false;
dynamicPVals.add(temp);

After the for loop you can add pval to the dynamicPVals list and update line 17 to use this list:
 
dynamicPVals.add(pval);
sown.picklistValues = dynamicPVals;

Hope that helps,
Scott

 
This was selected as the best answer
PS81PS81
Thanks Scott , this solved the issue. Though i had a similiar kind of code earlier I was running into field_integrity issues for dupicate value. I think  the below line within the loop made the difference.
 
matadataAPIservice.PicklistValue temp = new metadataAPIservice.PicklistValue();

thanks again!
scottbcovertscottbcovert
Great; glad I could help!