init
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
public inherited sharing class CreateOriginPicklistItem {
|
||||
public class Request {
|
||||
@InvocableVariable(required=true label='Picklist Item Name')
|
||||
public String picklistItemName;
|
||||
}
|
||||
|
||||
public class Result {
|
||||
@InvocableVariable(label='Case Origin')
|
||||
public String caseOrigin;
|
||||
|
||||
@InvocableVariable(label='Created')
|
||||
public Boolean created;
|
||||
|
||||
@InvocableVariable(label='Metadata Full Name')
|
||||
public String metadataFullName;
|
||||
}
|
||||
|
||||
@InvocableMethod(
|
||||
label='Create Origin Picklist Item'
|
||||
description='Adds a value to the Case Origin standard value set when it does not already exist.'
|
||||
category='Queue Configuration'
|
||||
callout=true
|
||||
)
|
||||
public static List<Result> createOrigin(List<Request> requests) {
|
||||
MetadataXml.require(
|
||||
requests != null && requests.size() == 1,
|
||||
'Create Origin Picklist Item accepts exactly one request per invocation.'
|
||||
);
|
||||
String label = requests[0].picklistItemName == null
|
||||
? null
|
||||
: requests[0].picklistItemName.trim();
|
||||
MetadataXml.require(
|
||||
!String.isBlank(label),
|
||||
'Picklist Item Name is required.'
|
||||
);
|
||||
|
||||
String developerName = MetadataXml.developerName(label);
|
||||
Dom.XmlNode valueSet = MetadataApiClient.readMetadata(
|
||||
'StandardValueSet',
|
||||
'CaseOrigin'
|
||||
);
|
||||
MetadataXml.require(
|
||||
valueSet != null,
|
||||
'The CaseOrigin standard value set was not found.'
|
||||
);
|
||||
|
||||
for (
|
||||
Dom.XmlNode valueNode : MetadataXml.children(valueSet, 'standardValue')
|
||||
) {
|
||||
String existingName = MetadataXml.text(valueNode, 'fullName');
|
||||
String existingLabel = MetadataXml.text(valueNode, 'label');
|
||||
if (
|
||||
label.equalsIgnoreCase(existingLabel) ||
|
||||
developerName.equalsIgnoreCase(existingName)
|
||||
) {
|
||||
if (
|
||||
developerName.equalsIgnoreCase(existingName) &&
|
||||
!label.equalsIgnoreCase(existingLabel)
|
||||
) {
|
||||
throw new IllegalArgumentException(
|
||||
'Case Origin developer name ' +
|
||||
developerName +
|
||||
' is already used by the label "' +
|
||||
existingLabel +
|
||||
'".'
|
||||
);
|
||||
}
|
||||
return new List<Result>{ result(existingName, false) };
|
||||
}
|
||||
}
|
||||
|
||||
String newValueXml =
|
||||
'<standardValue><fullName>' +
|
||||
MetadataXml.escape(developerName) +
|
||||
'</fullName><default>false</default><label>' +
|
||||
MetadataXml.escape(label) +
|
||||
'</label></standardValue>';
|
||||
MetadataApiClient.updateMetadata(
|
||||
'StandardValueSet',
|
||||
MetadataXml.serializeChildren(valueSet) + newValueXml
|
||||
);
|
||||
return new List<Result>{ result(developerName, true) };
|
||||
}
|
||||
|
||||
private static Result result(String caseOrigin, Boolean created) {
|
||||
Result value = new Result();
|
||||
value.caseOrigin = caseOrigin;
|
||||
value.created = created;
|
||||
value.metadataFullName = 'CaseOrigin';
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user