Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Picklist with controlling field and controlling value #2

Closed
mohit-address opened this issue Oct 20, 2012 · 6 comments
Closed

Comments

@mohit-address
Copy link

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 :
  public static void createPicklistField()
{
    MetadataService.MetadataPort service = createService();     
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'lead.picklist__c';
    customField.label = 'picklist';
    customField.type_x = 'Picklist';

    metadataservice.Picklist pt = new metadataservice.Picklist();
    pt.sorted= false;

    metadataservice.PicklistValue one = new metadataservice.PicklistValue();
    one.fullName= 'first'; 
    one.default_x=false ;

    pt.picklistValues = new list<metadataservice.PicklistValue>{one};
    customField.picklist = pt ;
    MetadataService.AsyncResult[] results = service.create (new List<MetadataService.Metadata> { customField });


}
  1. I can update / add new values via :
public static void UpdatePicklistField()
{
    MetadataService.MetadataPort service = createService();     
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'lead.picklist__c';
    customField.label = 'picklist';
    customField.type_x = 'Picklist';


    metadataservice.Picklist pt = new metadataservice.Picklist();
    pt.sorted= false;
      metadataservice.PicklistValue two = new metadataservice.PicklistValue();
        two.fullName= 'second';
        two.default_x=false ;

        metadataservice.PicklistValue three = new metadataservice.PicklistValue();
        three.fullName= 'third';
        three.default_x=false ;
    pt.picklistValues = new list<metadataservice.PicklistValue>{two,three};
    customField.picklist = pt ;

    MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata();
    ut.currentName='lead.picklist__c';
    ut.metadata= customField;

    MetadataService.AsyncResult[] results = service.updateMetadata(new List<MetadataService.updateMetadata> {ut});


}
  1. Now issues arises when i decided to add values in any depend picklist field :
    suppose there is a relation : controlling field : picklist..c : values : first, second, third
    and then dependent picklist : dependent_picklist..c , values : -none-

and now i write code to insert new value: "first_dependent" in dependent picklist

code is saving and running fine

  public static void update_dependent_PicklistField()
{
    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= 'lead.picklist__c';
       metadataservice.PicklistValue first_dependent = new metadataservice.PicklistValue();
         first_dependent.fullName= 'first_dependent'; 
         first_dependent.default_x=false ;
         first_dependent.controllingFieldValues = new list <string>{'first'};
     pt.picklistValues = new list<metadataservice.PicklistValue>{first_dependent};
     customField.picklist = pt ;


     MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata();
    ut.currentName='lead.picklist1__c';
    ut.metadata= customField;
   MetadataService.AsyncResult[] results = service.updateMetadata(new List<MetadataService.updateMetadata> {ut});

}

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

@mohit-address
Copy link
Author

 
<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

@afawcett
Copy link
Contributor

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.

@mohit-address
Copy link
Author

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});
         
    }

@afawcett
Copy link
Contributor

afawcett commented Nov 4, 2012

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! ;-)

@mohit-address
Copy link
Author

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 . :(

@afawcett
Copy link
Contributor

afawcett commented Nov 5, 2012

That is frustrating. I think I have answered this question though, Issue 4? #4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants