Closed mohit-address closed 11 years ago
<UpdateMetadata>
<currentName>lead.picklist1__c</currentName>
<metadata xsi:type="CustomField">
<fullName>lead.dependet_picklist__c</fullName>
<label>dependet_picklist</label>
<picklist>
<controllingField>lead.picklist__c</controllingField>
//-----------------------------// <picklistValues xsi:type="PicklistValue">
<fullName>first_dependent</fullName>
<controllingFieldValues>first</controllingFieldValues>
<default>false</default>
</picklistValues>
<sorted>false</sorted>
</picklist>
<type>Picklist</type>
</metadata
</UpdateMetadata>'
check long line , it should not add xsi:picklistvalue , as per the metadata api document I suppose .. What you think ? any issue here . or I am missing somthing
Maybe this is the issue try, first_dependent.type = null; this will stop it sending the xsi:type. You don't show what the error is though, have you checked that? If you dump out the ID returned via results[0].id you can then issue some Anonymous Apex calls to checkAsync to get the result and error message. Unless you have implemented in your page an actionPoller to do this for you.
Here is the example for Creating picklist field , with controlling field , and controlling field values
public static void createPicklistField()
{
MetadataService.MetadataPort service = createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'lead.dependent_picklist__c';
customField.label = 'Dependent Picklist';
customField.type_x = 'Picklist';
metadataservice.Picklist pt = new metadataservice.Picklist(); // creat picklist metadata
pt.sorted= false;
pt.controllingField = 'Controlling_Picklist__c'; // Define controlling field only name . not object name, if you are creating Dependent field , else dont include line : if field dependency doesnot exist , it will create new one
metadataservice.PicklistValue one = new metadataservice.PicklistValue(); // create picklist values metadata
one.fullName= 'Dependent 0';
one.default_x=false ;
one.controllingFieldValues = new list {'Control Odd number'}; // assign controlling field values
metadataservice.PicklistValue two = new metadataservice.PicklistValue();
two.fullName= 'Dependent 1';
two.default_x=false ;
two.controllingFieldValues = new list {'Control Odd number','Control Even number'}; // assign controlling field values
pt.picklistValues = new list{one,two}; // assign picklist values
customField.picklist = pt ; // assign Picklist
MetadataService.AsyncResult[] results = service.create (new List { customField });
}
and here is the code to update picklist field
public static void UpdatePicklistField()
{
MetadataService.MetadataPort service = createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'lead.dependent_picklist__c';
customField.label = 'Dependent Picklist';
customField.type_x = 'Picklist';
metadataservice.Picklist pt = new metadataservice.Picklist();
pt.sorted= false;
pt.controllingField= 'Controlling_Picklist__c'; //***** name of controlling field . if picklist has contrlloing field , this must be asssigned . else field dependency will be deleted
metadataservice.PicklistValue first_dependent = new metadataservice.PicklistValue();
first_dependent.fullName= 'Dependent 2';
first_dependent.default_x=false ;
first_dependent.controllingFieldValues = new list {'Control Even number'}; // list of names of controlling field Picklist value . dont put this line , if you dont want any controlling picklist value
pt.picklistValues = new list{first_dependent};
customField.picklist = pt ;
MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata();
ut.currentName='lead.dependent_picklist__c';
ut.metadata= customField;
MetadataService.AsyncResult[] results = service.updateMetadata(new List {ut});
}
Thanks for posting your solution code here! I've updated the example Apex class with it. Sounds like you had fun (?) working it out! If you can, you should blog more generally about what it is your doing. From the looks of this code, looks kinda interesting! ;-)
Hi , Thanks for the encouragement .. really appreciated . YEs , I m having lot of fun with it . working on metadata api inside sfdc , is opening whole new scope of possibilities . thanks for that .. But I recently hit an bump with record types . i also opened one issue here with name : -- Request : need some help on record type , Picklist field Describe call
I dont know whether you had time to look at it , but if you can just check it out once , that would be great , --its shows limitation on metadata api from apex , or metadata APi in general , I also raised same question with salesforce.com , but haven't heard back from them . :(
That is frustrating. I think I have answered this question though, Issue 4? https://github.com/financialforcedev/apex-mdapi/issues/4
Hi , i been working on creating picklist , updating and assign to controlling field and i came across an issues . that might interest you .
so .. 1) I can Create new picklist field via this method :
2) I can update / add new values via :
and now i write code to insert new value: "first_dependent" in dependent picklist
code is saving and running fine
But it WONT INSERT any value in dependent picklist cause : its sending update metadata as follows :
Please do let me know your thoughts on this