Created
August 24, 2016 21:22
-
-
Save abeyer/cf9ddfec8f9d3bf5f3284f29d5de81fe to your computer and use it in GitHub Desktop.
Set or clear multiselect picklist values from process builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PBClearMultiPicklist { | |
public class Params { | |
@InvocableVariable(label='Object ID' required=true) | |
public String sobjectId; | |
@InvocableVariable(label='Picklist Field API Name' required=true) | |
public String fieldName; | |
@InvocableVariable(label='Value' required=true) | |
public String value; | |
} | |
@InvocableMethod( | |
label='Clear a multiselect picklist value' | |
description='Removes a selected value from a multiselect picklist without changing other selected values.' | |
) | |
public static void clearPicklistValue(List<Params> ps){ | |
Params p = ps[0]; | |
Id sobjectId = (Id)(p.sobjectId); | |
String fieldName = p.fieldName; | |
String value = p.value; | |
// Fetch object by ID | |
Schema.SObjectType sobjectType = sobjectId.getSObjectType(); | |
String sobjectName = sobjectType.getDescribe().getName(); | |
SObject record = Database.query('Select Id,' + fieldName + ' From ' + sobjectName + ' Where Id = :sobjectId'); | |
// Get existing field values into set | |
String prevStr = (String)record.get(fieldName); | |
Set<String> values = new Set<String>(prevStr.split(';')); | |
// Add new value | |
values.remove(value); | |
// Rebuild value string from set | |
List<String> sorted = new List<String>(values); | |
sorted.sort(); | |
String newStr = String.join(sorted, ';'); | |
record.put(fieldName, newStr); | |
update record; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PBSetMultiPicklist { | |
public class Params { | |
@InvocableVariable(label='Object ID' required=true) | |
public String sobjectId; | |
@InvocableVariable(label='Picklist Field API Name' required=true) | |
public String fieldName; | |
@InvocableVariable(label='Value' required=true) | |
public String value; | |
} | |
@InvocableMethod( | |
label='Set a multiselect picklist value' | |
description='Adds a selected value to a multiselect picklist without changing other selected values.' | |
) | |
public static void setPicklistValue(List<Params> ps){ | |
Params p = ps[0]; | |
Id sobjectId = (Id)(p.sobjectId); | |
String fieldName = p.fieldName; | |
String value = p.value; | |
// Fetch object by ID | |
Schema.SObjectType sobjectType = sobjectId.getSObjectType(); | |
String sobjectName = sobjectType.getDescribe().getName(); | |
SObject record = Database.query('Select Id,' + fieldName + ' From ' + sobjectName + ' Where Id = :sobjectId'); | |
// Get existing field values into set | |
String prevStr = (String)record.get(fieldName); | |
Set<String> values = new Set<String>(prevStr.split(';')); | |
// Add new value | |
values.add(value); | |
// Rebuild value string from set | |
List<String> sorted = new List<String>(values); | |
sorted.sort(); | |
String newStr = String.join(sorted, ';'); | |
record.put(fieldName, newStr); | |
update record; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an example test class for this apex class.
http://salesforce.stackexchange.com/questions/138902/method-does-not-exist-or-incorrect-signature-calling-public-class-from-test-cl