1// Sample implementation to call insurance get rating input invocable action in Apex
2public class InsuranceGetRatingInputApexService implements Callable {
3
4 private static final String GET_RATING_INPUT_METHOD = 'getRatingInput';
5
6 /**
7 * @description Implementation of Callable interface for OmniStudio
8 * @param action Method name to invoke
9 * @param args Map containing input, output, and options
10 * @return Object result of the method invocation
11 */
12 public Object call(String action, Map<String, Object> args) {
13 System.debug('InsuranceGetRatingInputApexService: call ' + action);
14
15 Map<String, Object> input = (Map<String, Object>) args.get('input');
16 Map<String, Object> output = (Map<String, Object>) args.get('output');
17 Map<String, Object> options = (Map<String, Object>) args.get('options');
18
19 return invokeMethod(action, input, output, options);
20 }
21 // Invoke methods, and if the method is not supported, it will be ignored.
22 public Boolean invokeMethod(
23 String methodName,
24 Map<String, Object> inputs,
25 Map<String, Object> output,
26 Map<String, Object> options
27 ) {
28 Boolean response = false;
29
30 System.debug('Insurance Get Rating Input Apex Service - Method: ' + methodName);
31
32 try {
33 if (GET_RATING_INPUT_METHOD.equals(methodName)) {
34 response = getRatingInput(inputs, output, options);
35 } else {
36 throw new UnsupportedOperationException('this method name is incorrect or not supported');
37 }
38 } catch (Exception e) {
39 response = false;
40 output.put('success', false);
41 output.put('errorMessage', 'Exception occurred: ' + e.getMessage());
42 System.debug(LoggingLevel.ERROR, 'Exception in invokeMethod: ' + methodName + ', Error: ' + e.getMessage());
43 System.debug(LoggingLevel.ERROR, 'Stack trace: ' + e.getStackTraceString());
44 }
45 System.debug('Output from invoke:' + output);
46 return response;
47 }
48
49 private Boolean getRatingInput(Map<String, Object> inputs, Map<String, Object> output, Map<String, Object> options) {
50
51 Map<String, Object> getRatingInputPayload = inputs;
52 // Handle null input
53 if (getRatingInputPayload == null) {
54 output.put('success', false);
55 output.put('errorMessage', 'ratingInput is required in the payload for rating');
56 output.put('errorType', 'VALIDATION_ERROR');
57 return false;
58 }
59 System.debug('Create Rating Input: ' + JSON.serialize(getRatingInputPayload));
60
61 try {
62 System.debug('Calling Invocable Action: getInsuranceRatingInput');
63
64 // Create and configure the invocable action
65 Invocable.Action action = Invocable.Action.createStandardAction('getInsuranceRatingInput');
66
67 // productIds is required
68 setInvocationParameter(action, getRatingInputPayload, 'productIds', true);
69
70 // salesTransactionTypeId is optional
71 setInvocationParameter(action, getRatingInputPayload, 'salesTransactionTypeId', false);
72
73 // transactionType is optional
74 setInvocationParameter(action, getRatingInputPayload, 'transactionType', false);
75
76 // effectiveDate is optional
77 setInvocationParameter(action, getRatingInputPayload, 'effectiveDate', false);
78
79 // ratingDate is optional
80 setInvocationParameter(action, getRatingInputPayload, 'ratingDate', false);
81
82 // rootProductCodes is optional
83 setInvocationParameter(action, getRatingInputPayload, 'rootProductCodes', true);
84
85 // additionalFields is optional, but to be serialized
86 setInvocationParameter(action, getRatingInputPayload, 'additionalFields', true);
87
88 // Execute the action
89 List<Invocable.Action.Result> results = action.invoke();
90
91 if (results != null && !results.isEmpty()) {
92 Invocable.Action.Result result = results[0];
93 System.debug('IA result: ' + JSON.serialize(result));
94
95 if (result.isSuccess()) {
96 System.debug('Get Rating Input created successfully from Invocable Action');
97 output.put('output', result.getOutputParameters());
98
99 // Extract key information from the result
100 Map<String, Object> outputParams = result.getOutputParameters();
101 if (outputParams != null) {
102 if (outputParams.containsKey('getRatingInputOutputRep')) {
103 output.put('getRatingInputOutputRep', outputParams.get('getRatingInputOutputRep'));
104 }
105 System.debug('Rating Input data extracted: ' + outputParams);
106 }
107 List<Invocable.Action.Error> actionErrors = result.getErrors();
108 if (actionErrors != null && !actionErrors.isEmpty()) {
109 output.put('success', false);
110 output.put('error', result.getErrors());
111 return false;
112 }
113 output.put('success', true);
114 return true;
115
116 } else {
117 // Action failed - get error details
118 List<Invocable.Action.Error> actionErrors = result.getErrors();
119 String errorMessage = 'Get Rating Input Invocable Action failed';
120
121 if (actionErrors != null && !actionErrors.isEmpty()) {
122 List<String> errorMessages = new List<String>();
123 List<Map<String, Object>> errorDetailsList = new List<Map<String, Object>>();
124
125 for (Invocable.Action.Error actionError : actionErrors) {
126 errorMessages.add(actionError.getMessage());
127 errorDetailsList.add(new Map<String, Object>{
128 'message' => actionError.getMessage(),
129 'code' => actionError.getCode()
130 });
131 }
132 errorMessage = String.join(errorMessages, '; ');
133 output.put('actionErrors', errorDetailsList);
134 }
135 output.put('success', false);
136 output.put('errorMessage', errorMessage);
137
138 System.debug(LoggingLevel.ERROR, 'Get Rating Input Invocable Action failed: ' + errorMessage);
139 return false;
140 }
141
142 } else {
143 output.put('success', false);
144 output.put('errorMessage', 'Get Rating Input Invocable Action returned null or empty results');
145 System.debug(LoggingLevel.WARN, 'Get Rating Input Invocable Action returned null/empty results');
146 }
147 return false;
148
149 } catch (Exception e) {
150 output.put('success', false);
151 output.put('error', e.getMessage());
152 return false;
153 }
154 }
155
156 private void setInvocationParameter(Invocable.Action action, Map<String, Object> quotePayload, String inputAttribute, boolean serializeJSON) {
157 if (quotePayload.containsKey(inputAttribute) && quotePayload.get(inputAttribute) != null) {
158 System.debug('InsuranceGetRatingInputApexService::' + inputAttribute + ': ' + JSON.serialize(quotePayload.get(inputAttribute)));
159 if (serializeJSON) {
160 action.setInvocationParameter(inputAttribute, JSON.serialize(quotePayload.get(inputAttribute)));
161 } else {
162 action.setInvocationParameter(inputAttribute, quotePayload.get(inputAttribute));
163 }
164 } else {
165 System.debug('Payload does not contain attribute: ' + inputAttribute);
166 }
167 }
168}