23

For those of us who are regular contributors (especially on the Apex side), we've all benefited from the contributions of others to improve our own game.

As such, from time-to-time, I've grabbed posted code snippets and added (or adapted) into my own Util class to maximize my productivity.

The question at hand is -- where can we post into a common Community-wide place our own 'personal favorite' short-concise Util methods (or links to said same) so we can all benefit? Even a running list here in Meta would be more than adequate.

For example, this is one I borrowed and adapted from @sfdcfox that is incredibly useful to me.

Without the method (and so often seen in OPs) -- tedious bulkification code

set<ID> aIdSet = new set<ID>();
for (Contact c : Trigger.new)
    aIdSet.add(c.AccountId);
map<ID,Account> aIdToAccountMap = new map<ID,Account> (
              [select id, name from Account where id IN :aIdSet]);

With the method - one line of code!

map<ID,Account> aIdToAccountMap = new map<ID,Account> (
              [select id, name from Account 
                  where id IN :Util.getIdSetFromField(Trigger.new,Contact.AccountId)]);

The Util method(s)

// ---------------------------------
// getIdSetFromField : returns set of IDs from a field of type ID in a list of sobjects
// ---------------------------------
public static Set<ID> getIdSetFromField(SObject[] records, SObjectField fieldname) {
    return new Set<ID> ((List<ID>) getValListFromField(List<ID>.class, records, fieldname));
}

//  --------------------------------------------------------
//  getValListFromField : Returns a list of field values from a list of sobjects; 
//                        ref: http://salesforce.stackexchange.com/a/64949/2602
//   Usage Ex: Decimal[] amountList = (Decimal[])Util.getValListFromField(Decimal[].class,
//                                                                        Trigger.new,
//                                                                        Opportunity.Amount);
//  --------------------------------------------------------
public static Object[] getValListFromField(Type resListType, SObject[] records, SObjectField fieldname) {
    Object[] res    = (Object[])resListType.newInstance();
    for (SObject sObj: records)
        res.add(sobj.get(fieldname));
    return res; 
}
  • Oof, that getIdSetFromField method has got to be way slower than Map<Id, SObject>(...).keySet(). – Adrian Larson Aug 9 '16 at 22:30
  • @AdrianLarson - I'm not following you with the contents of (...) please elaborate – cropredy Aug 9 '16 at 22:45
  • It would just be new Map<Id, SObject>(records).keySet(). I guess it was a little silly to shorten. – Adrian Larson Aug 9 '16 at 23:01
  • 2
    @AdrianLarson - I must be missing something - but how does that extract a set of AccountIds from a list of Contacts – cropredy Aug 9 '16 at 23:08
  • Well, not for lookup fields I suppose. The comparison doesn't work there. – Adrian Larson Aug 10 '16 at 0:05
  • 1
    Also, I could use some more ideas for the open-source/managed library I am working on :p – sfdcfox Aug 12 '16 at 21:23
  • 1
    @sfdcfox -- well, one could argue that if the Open Source library was called sfdcfox, it would show up a lot in Google and hence might address my desire to have a one stop shop :-) – cropredy Aug 12 '16 at 21:31
  • 2
    Always had this question.. we should have wiki for all such amazing API or tips that will help improve the programming standard of newbies like us and make us reach prod faster. Googling for such leads to blogs with MCQ . This will help. – Pranay Jaiswal Aug 13 '16 at 0:31
  • @AdrianLarson here's an example of another discussion/opinionated post that lives and thrives in meta – Ralph Callaway Nov 7 '18 at 7:13
14

Open source it. If you're willing to share it with the world, GitHub is a great option.

I've shared a couple there I'm fairly proud of (though they're not necessarily a finished product):

Also, some of my favorite third-party tools are on there and actually have install links:

I have a couple where I didn't come up with the idea and don't want to share someone else's work, but the names and a skeleton can tell you a fair amount. That's all I needed to build it from scratch: Pluck and GroupBy (gists are great, too).

  • 2
    yes - I get that -- but how would the community know where to look? It took me by chance to discover yours - and DLRS gets mentioned a ton because it solves a large problem – cropredy Aug 9 '16 at 22:46
  • 3
    I think that for new comers to Salesforce knowing where resources are on GitHub would be an issue as they wouldn't know users names to be searching for. – Dave Humm Aug 10 '16 at 9:37
6

Since you seem to have a passion for this @cropredy, I would suggest that either you or "a trusted volunteer" (a mod perhaps?) open a new Github repository under the name of "SalesforceStackExchangeForum" (or similar) to which users can either contribute their code, or provide links to their own repositories. A Gist of the same name would seem appropriate to create as well.

I'd hope we'd have no issues providing a prominent link to it from our site, but would expect one of the mods would need to speak with management to make that happen. I'd also hope we'd have no issues with using the the forum's name on Github but recommend we ask permission from StackExchange to use the forum's name first.

It's possible they may want to open it and/or control it (doubtful, but you never know). If that should happen and we decide that's not how we want to do it, then obviously we can use some acronym that's similar enough to convey this Forum's association with the repository without violating anyone's copyrights.

  • Seems a good idea. – Pranay Jaiswal Aug 14 '16 at 15:06
  • It would fit within documentation on Apex, perhaps, if that feature is ever rolled out here. – Adrian Larson Aug 16 '16 at 3:09
  • would love to contribute to an SFSE helper index – Ralph Callaway Nov 7 '18 at 7:15
5

Well I will list down some of API's that I have used.

  1. ESAPI - For Handling FLS in Apex.
  2. Apex MDAPI - Metadata features in Apex.
  3. Salesforce Test Data Generator - Quickly Generate Lookups, Master-Detail Relationships, and Required Fields
  4. FinancialForce Apex Common- Application Enterprise Patterns on Force.com
  5. Apex Tooling API.
0

I'm throwing in my library to the pool :) https://github.com/amorek/sfdc-toolkit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .