Sample Apex Class to Create Case Records for Service Process Requests

When a service process request is submitted, an Apex class invokes the service process connect API to create a case. The apex class extracts the details provided for the service request and assigns these details to the data attributes defined for the service process in Service Process Studio. The Apex class then passes the attributes and their values to the connect API to create a case.

Apex Class to Create a Case Record for a Transaction Dispute Service Process Request 

This sample Apex class helps in processing a request related to a transaction dispute. It extracts details, such as the financial account associated with the dispute, dispute type and subtype, transaction description, status, dispute received date, disputed amount, actual amount, approved amount, supporting documents, and the list of disputed transactions. It maps these details to the data attributes that are defined for the service process in Service Process Studio. The Apex class then constructs an API payload using the retrieved data attributes and their extracted values and passes the payload to the connect API to create a case. When a case is created, service catalog request, dispute, and dispute items records are created.

The Apex class retrieves these data attributes. This set contains a mix of complex attributes, which includes attributes of type lookup, text, currency, date, and ListOfMaps. While defining the data attributes, you can choose relevant objects where to store the values for these attributes. For more information, see Considerations for Selecting Base Objects

DATA ATTRIBUTE NAMEDATA ATTRIBUTE API NAMEPARENT DATA ATTRIBUTEDATA TYPE
Account Idaccount_id–Lookup
Assessment Idassessment_id–Lookup
Financial Accountfinancial_account_id–Lookup
Dispute Typedispute_type–Text
Dispute Subtypedispute_subtype–Text
Descriptiondescription–Text
Disputed Transactions Infodisputed_transactions_info–ListOfMaps
Transaction Descriptiontransaction_descriptiondisputed_transactions_infoText
Transaction Identifiertransaction_identifierdisputed_transactions_infoText
Transaction Datetransaction_datedisputed_transactions_infoDate
Transaction Amounttransaction_amountdisputed_transactions_infoCurrency
Statusstatus–Text
Received Datereceived_date–Date
Actual Amountactual_amount–Currency
Disputed Amountdisputed_amount–Currency
Approved Amountapproved_amount–Currency

This is a sample API payload constructed by the Apex class for the Connect API. The payload is based on the values extracted for the data attributes that were defined to capture the data for the service process request.

1{
2  "attributes": {
3    "data": {
4      "disputed_transactions_info": {
5        "data": {
6          "dispute_type": {
7            "value": "Consumer Dispute"
8          },
9          "dispute_subtype": {
10            "value": "Undelivered Product or Service"
11          },
12          "description": {
13            "value": "Order was never delivered but charge still appears on credit card statement. Order was placed via their website acmemerchant.com"
14          },
15          "received_date": {
16            "value": "2023-07-14"
17          },
18          "account_id": {
19            "value": "001DI000001o10QYAQ"
20          },
21          "assessment_id": {
22            "value": "0U3DI000000018m0AA"
23          },
24          "financial_account_id": {
25            "value": "a0EDI000000Ce6W2AS"
26          },
27          "actual_amount": {
28            "value": 593.63
29          },
30          "disputed_amount": {
31            "value": 400
32          },
33          "approved_amount": {
34            "value": 400
35          },
36          "status": {
37            "value": "Submitted"
38          }
39        }
40      },
41      "disputed_transactions_info": {
42        "value": [
43          {
44            "transaction_date": "2023-01-01",
45            "transaction_identifier": "1",
46            "transaction_amount": 213.1,
47            "transaction_description": "t1"
48          },
49          {
50            "transaction_date": "2023-01-01",
51            "transaction_identifier": "2",
52            "transaction_amount": 150.94,
53            "transaction_description": "t2"
54          },
55          {
56            "transaction_date": "2023-01-01",
57            "transaction_identifier": "3",
58            "transaction_amount": 199,
59            "transaction_description": "t3"
60          },
61          {
62            "transaction_date": "2023-01-01",
63            "transaction_identifier": "4",
64            "transaction_amount": 30.59,
65            "transaction_description": "t4"
66          }
67        ]
68      }
69    }
70  },
71  "caseInfo": {
72    "data": {
73      "Subject": {
74        "value": "Julia Green Transaction Dispute Request"
75      },
76      "Origin": {
77        "value": "Phone"
78      },
79      "AccountId": {
80        "value": "001DI000001o10QYAQ"
81      }
82    }
83  },
84  "documentInfo": {
85    "data": [
86      {
87        "id": "content document id-1",
88        "name": "document name-1"
89      },
90      {
91        "id": "content document id-2",
92        "name": "document name-2"
93      }
94    ]
95  },
96  "svcCatalogItemDefApiName": "Dispute_Management"
97}

You can use this sample Apex class as a reference to create an Apex class that suits your transaction dispute service process requirements.

1global with sharing class DisputeManagementHelper implements System.Callable {
2
3    public Object call(String action, Map<String, Object> args) {
4
5        Map<String, Object> inputMap = (Map<String, Object>)args.get('input');
6        Map<String, Object> outputMap = (Map<String, Object>)args.get('output');
7        Map<String, Object> options = (Map<String, Object>)args.get('options');
8
9        if (action == 'raiseDispute') {
10
11            String accountId = (String)inputMap.get('AccountId');
12            String assessmentId = (String)inputMap.get('AssessmentId');
13            String FAId = (String)inputMap.get('FinancialAccountId');
14            String disputeType = (String)inputMap.get('Type');
15            String disputeSubType = (String)inputMap.get('SubType');
16            String description = (String)inputMap.get('Description');
17            List<Object> transactions = (List<Object>)inputMap.get('SelectedTransactions');
18
19            // Set the input rep
20            ConnectApi.ServiceProcessRequestInputRepresentation input = new ConnectApi.ServiceProcessRequestInputRepresentation();
21            input.svcCatalogItemDefApiName = (String)inputMap.get('svcCatalogItemDefApiName');
22            input.caseInfo = new Map<String, ConnectApi.GenericObject>();
23            input.attributes = new Map<String, ConnectApi.GenericObject>();
24            input.documentInfo = new List<ConnectApi.DocumentInfoAttributes>();
25
26            //set case info
27            ConnectApi.GenericObject subjectObj = new ConnectApi.GenericObject();
28            subjectObj.value = 'Transaction Dispute';
29            input.caseInfo.put('Subject', subjectObj);
30
31
32            //set attributes
33            ConnectApi.GenericObject accountIdObj = new ConnectApi.GenericObject();
34            accountIdObj.value = accountId;
35
36            ConnectApi.GenericObject assessmentIdObj = new ConnectApi.GenericObject();
37            assessmentIdObj.value = assessmentId;
38
39            ConnectApi.GenericObject financialAccountIdObj = new ConnectApi.GenericObject();
40            financialAccountIdObj.value = FAId;
41
42            ConnectApi.GenericObject disputeTypeObj = new ConnectApi.GenericObject();
43            disputeTypeObj.value = disputeType;
44
45            ConnectApi.GenericObject disputeSubTypeObj = new ConnectApi.GenericObject();
46            disputeSubTypeObj.value = disputeSubType;
47
48            ConnectApi.GenericObject descriptionObj = new ConnectApi.GenericObject();
49            descriptionObj.value = description;
50
51            ConnectApi.GenericObject createdDateObj = new ConnectApi.GenericObject();
52            createdDateObj.value = Datetime.now().format('yyyy-MM-dd');
53
54            ConnectApi.GenericObject statusObj = new ConnectApi.GenericObject();
55            statusObj.value = 'Submitted';
56
57
58            List<Map<String, Object>> transactionsList = new List<Map<String, Object>>();
59
60            for (Object t : transactions) {
61
62                Map<String, Object> transactionObj = (Map<String, Object>)t;
63                Map<String, Object> transactionObjMap = new Map<String, Object>();
64
65                String transactionDescription = (String)transactionObj.get('TransactionDescription');
66                String transactionId = (String)transactionObj.get('TransactionId');
67                String transactionDate = (String)transactionObj.get('TransactionPostedDate');
68                Double transactionAmt = (Double)transactionObj.get('TransactionAmount');
69
70                transactionObjMap.put('transaction_description', transactionDescription);
71                transactionObjMap.put('transaction_identifier', transactionId);
72                transactionObjMap.put('transaction_date', transactionDate.split('T', 2)[0]);
73                transactionObjMap.put('transaction_amount', transactionAmt);
74
75                transactionsList.add(transactionObjMap);
76            }
77
78            ConnectApi.GenericObject actualAmtObj = new ConnectApi.GenericObject();
79            actualAmtObj.value = transactionsList[0].get('transaction_amount');
80
81            ConnectApi.GenericObject disputedAmtObj = new ConnectApi.GenericObject();
82            disputedAmtObj.value = transactionsList[0].get('transaction_amount');
83
84            ConnectApi.GenericObject transactionObj = new ConnectApi.GenericObject();
85            transactionObj.value = transactionsList;
86
87            if (!String.isBlank(assessmentId)) {
88                input.attributes.put('assessment_id', assessmentIdObj);
89            }
90            input.attributes.put('account_id', accountIdObj);
91            input.attributes.put('received_date', createdDateObj);
92            input.attributes.put('financial_account_id', financialAccountIdObj);
93            input.attributes.put('description', descriptionObj);
94            input.attributes.put('status', statusObj);
95            input.attributes.put('actual_amount', actualAmtObj);
96            input.attributes.put('disputed_amount', disputedAmtObj);
97            input.attributes.put('disputed_transactions_info', transactionObj);
98
99
100            // retrieve the documentInfo by the customer
101            List<Object> uploadedDocumentInfoList = (List<Object>)inputMap.get('documentInfo');
102
103            // set docInfo
104            Set<ConnectApi.DocumentInfoAttributes> docInfoSet = new Set<ConnectApi.DocumentInfoAttributes>();
105            if (uploadedDocumentInfoList != null && !uploadedDocumentInfoList.isEmpty()) {
106                for(Object uploadedDocInfoMap : uploadedDocumentInfoList) {
107                    ConnectApi.DocumentInfoAttributes doc = new ConnectApi.DocumentInfoAttributes();
108                    Map<String, Object> uploadMap = (Map<String, Object>)uploadedDocInfoMap;
109                    doc.id = (String) uploadMap.get('id');
110                    doc.name = (String) uploadMap.get('name');
111                    docInfoSet.add(doc);
112                }
113            }
114            for (ConnectApi.DocumentInfoAttributes docInfoAttr : docInfoSet) {
115                input.documentInfo.add(docInfoAttr);
116            }
117            System.debug(input);
118
119            // Invoke API
120            Map<String, String> responseMap = new Map<String, String>();
121            ConnectApi.ServiceProcessRepresentation output = ConnectApi.IServiceProcessConnectFamily.createCaseServiceProcess(input);
122            responseMap.put('caseId', output.caseId);
123            responseMap.put('caseNumber', output.caseNumber);
124            responseMap.put('svcCatalogRequestId', output.svcCatalogRequestId);
125
126            outputMap.put('apiResponse', responseMap);
127            return outputMap;
128        }
129
130        return null;
131    }
132}