+ Start a Discussion

Create package issue - can't add Price Book Entry Field Set to the package

I have a price book entry field set and I wanted to add it as a component to my package, for some reasons, it's not showing up in the "Add To Package" view with the Component type set to "Field Set". Any idea why?
Mitesh SuraMitesh Sura
Glad somebody cares. Please vote for this idea: https://success.salesforce.com/ideaView?id=08730000000lAatAAE 

We created case and tier 3 confirmed this is not a feature they currently have. This is so depressing, because there is no other way to include the field sets. 

One workaround would be dynamic field set. Then ask customer to create those manually before package installation. Not a good solution at all. 

Schema.getGlobalDescribe().get('PricebookEntry').getDescribe().fieldSets.getMap().get('FieldSet Name').getFields();

Dynamic field set is a great idea, our solution is to let the user create field sets via the Metadata API on our configuration VF page.
Mitesh SuraMitesh Sura
I just started looking into this, and sounds like good approach. Can you please share the building blocks or strip down version of code? I just need to create couple of fieldsets with couple of fields in post-install (apex) script.

I got hang of this link: https://github.com/financialforcedev/apex-mdapi It has lot more info and I tried few things but did not work. Thank you very much.

1. See the link below for a portion of my codes and the screenshot of the configuration VF page I built ...


2. You can learn a lot about how the Metadata API works from the link below (full source code link included) ...


3. As for the issue of creating the remote site setting for post install, it's finally resolved, many thanks to Andrew and sfdcfox, see this link for more information ...


4. Finally, writing the test class for your apex code can be tricky, good luck and have fun with Metadata API.
Mitesh SuraMitesh Sura
Made some progress. Did you try adding fields in FieldSet as displayedFields rather than availableFields? Please see simple code below. Developer console throws an error for fieldSet.displayedFields.add(myAvailableField);

Error Message: Metadata API Exception Attempt to de-reference a null object

// FieldSet     
    MetadataService.FieldSet fieldSet = new MetadataService.FieldSet();
    fieldSet.fullName = 'PricebookEntry.MyFieldSet1';
    fieldSet.label = 'My FieldSet1';
    fieldSet.description = 'Used by my VF page';

	//FieldSet Field    
    fieldSet.availableFields = new List<MetadataService.FieldSetItem>();
    MetadataService.FieldSetItem myAvailableField = new MetadataService.FieldSetItem();
    //Field 1
    myAvailableField.field = 'UnitPrice';
    //Field 2
    myAvailableField = new MetadataService.FieldSetItem();
    myAvailableField.field = 'Product2.LastModifiedDate';

    // Create   
    List<MetadataService.UpsertResult> results = service.upsertMetadata(new MetadataService.Metadata[] { fieldSet });

Mitesh SuraMitesh Sura

Quick question, how did you automate creating fieldsets in target org.? In my dev org, I can create fieldsets using metadata API, but I had to enter the remote site manually. 

There is code out there which can create remote site using JS and then probably admin can click button to install the fieldsets. But that is not fully automated. I would like to create fieldsets in post install or something similar without end customer doing anything. 

What is best practice? 
A remote site is needed for now, please read the idea below and vote for it.

Mitesh SuraMitesh Sura
Thanks BoonPlus, already voted for that idea. Such a pain to include PBE in managed package!!