So why am I writing this blog post if there’s so much great information readily available? There are certain limitations to publisher actions that I want to address, as well as provide solutions for getting around them. And, of course, I’ll link to ideas from the Salesforce IdeaExchange so that perhaps one day this post is no longer valid.
Publisher Actions do not play nice with record types
Here’s the catch: you can’t add the record type field to the publisher layout. A publisher action must be tied to a single record type.
This means that if you wanted to offer the ability to create a record of each of those five types, you would have to create five different publisher actions. And, given that Salesforce recommends a maximum of nine actions per layout (including standard actions), you can see why this is not an optimal solution.
The Workaround
Step Five: Implement the following trigger code on the object that is being created as a result of the publisher action, replacing instances of Object__c with the API name of the object.
trigger ObjectTrigger on Object__c (before insert) {
// Grab record type information for the object
Map<String, Schema.RecordTypeInfo> objectRecordTypes
= new Map<String, Schema.RecordTypeInfo>();
Map<String, Schema.RecordTypeInfo> recordTypeInfosTemp
= Schema.SObjectType.Object__c.getRecordTypeInfosByName();
for (Schema.RecordTypeInfo recordTypeInfo : recordTypeInfosTemp.values()){
String recordTypeName = recordTypeInfo.getName();
if (recordTypeInfo.isAvailable() && recordTypeName != ‘Master’){
objectRecordTypes.put(recordTypeName, recordTypeInfo);
}
}
// Loop through all new records being created
for (Object__c obj : Trigger.new) {
// If the custom record type field is set, and does not match the current
// record type assigned to the record, change the record type to match.
if (objectRecordTypes.containsKey(obj.Object_Record_Type__c) &&
obj.Object_Record_Type__c != obj.RecordType.Name) {
obj.RecordTypeId =
requestRecordTypes.get(obj.Object_Record_Type__c).RecordTypeId;
}
}
}
Step Six: You’re all done. Test it out!
Vote for this idea on the IdeaExchange: Ability to select record type when using a Publisher Action
Global publisher actions don’t know the context in which they are being created
The Workaround
How are you using publisher actions?