+ Start a Discussion
BoonPlusBoonPlus 

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

Mitesh
BoonPlusBoonPlus
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.

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

https://github.com/financialforcedev/apex-mdapi/issues/45

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

http://andyinthecloud.com/2014/02/09/new-release-spring14-declarative-rollup-summary-tool/

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

http://andyinthecloud.com/2014/07/29/post-install-apex-metadata-api-configuration-solved/

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';
    fieldSet.availableFields.add(myAvailableField);
    //Field 2
    myAvailableField = new MetadataService.FieldSetItem();
    myAvailableField.field = 'Product2.LastModifiedDate';
    fieldSet.displayedFields.add(myAvailableField);

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



Mitesh SuraMitesh Sura
BoonPlus, 

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? 
BoonPlusBoonPlus
A remote site is needed for now, please read the idea below and vote for it.

https://success.salesforce.com/ideaView?id=08730000000l4TkAAI
Mitesh SuraMitesh Sura
Thanks BoonPlus, already voted for that idea. Such a pain to include PBE in managed package!!