Guest User

Untitled

a guest
Jan 17th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. global class Qc_Responses {
  2.  
  3. global AccountResponse accountResponse {get;set;}
  4. global list<ContactResponse> contactResponses {get;set;}
  5. global ExceptionResponse exceptionResponse {get;set;}
  6.  
  7. public class AccountResponse
  8. {
  9. public String statusCode;
  10. public String sfAccountId;
  11. public String message;
  12.  
  13. // account Responses for Success / error
  14. public AccountResponse(String statusCode, String accId){
  15. this.statusCode = statusCode;
  16. SFAccountId = accId;
  17. this.message = (String.isBlank(accId)) ? 'Account is not Created, Duplicate Account Found' : 'Account Created Successfully' ;
  18. }
  19.  
  20. }
  21.  
  22. public class ContactResponse{
  23. public String statusCode;
  24. public String message;
  25. public String sfContactid;
  26. public String email;
  27. public String lastName;
  28. public String phone;
  29. public ContactResponse(String statusCode,String message, String sfContactid, String email, String lastName, String phone){
  30. this.statusCode = statusCode;
  31. this.message = Message;
  32. this.sfContactid = sfContactid;
  33. this.email = email;
  34. this.phone = phone;
  35. this.lastName = lastName;
  36. }
  37. }
  38.  
  39. public class ExceptionResponse {
  40. public String message;
  41. public String errorCode;
  42. public ExceptionResponse(String message, String errorcode){
  43. this.message = message;
  44. this.errorCode = errorcode;
  45. }
  46. }
  47. }
  48.  
  49. {
  50. "exceptionResponse": null,
  51. "contactResponses": [
  52. {
  53. "statusCode": "201",
  54. "sfContactid": "0030k00000RpgLdAAJ",
  55. "phone": "+610212345678",
  56. "message": "1Contact(s) Created Successfully",
  57. "lastName": "Britto",
  58. "email": "sample1@salesforce.com"
  59. },
  60. {
  61. "statusCode": "201",
  62. "sfContactid": "0030k00000RpgLeAAJ",
  63. "phone": "+61-(02)-12346678",
  64. "message": "2Contact(s) Created Successfully",
  65. "lastName": "Smith",
  66. "email": "sample2@salesforce.com"
  67. }
  68. ],
  69. "accountResponse": {
  70. "statusCode": "201",
  71. "sfAccountId": "0010k00000PKwIpAAL",
  72. "message": "Account Created Successfully"
  73. }
  74. }
  75.  
  76. @RestResource(urlMapping='/QACAcctServices/')
  77. global with sharing class QAC_AccountServicesProcess
  78. {
  79.  
  80. // Instance variables here
  81.  
  82. @HttpPost
  83. global static QAC_Responses dosomeProcess()
  84. {
  85. // All Responses from Salesforce after processing
  86. QAC_Responses qacResp = new QAC_Responses();
  87. QAC_Responses.AccountResponse myAccResp;
  88. QAC_Responses.ContactResponse myContResp;
  89. QAC_Responses.ExceptionResponse myExcepResp;
  90.  
  91. // some object responses are returned as lists after processing
  92. list<QAC_Responses.ContactResponse> myContRespList = new list<QAC_Responses.ContactResponse>();
  93.  
  94. // local variables for business logic
  95.  
  96. // Getting REST JSON from Request and setting to Response codes
  97. RestRequest req = RestContext.request;
  98. RestResponse res = RestContext.response;
  99. receivedReq = req.requestBody.toString();
  100.  
  101. try
  102. {
  103. system.debug(' @@ request 1 @@' +receivedReq);
  104. QAC_AccountServicesAPI acApi = QAC_AccountServicesAPI.parse(receivedReq);
  105.  
  106. QAC_AccountServicesAPI.AccountDetail accRecResp = acApi.agencyRegistration.accountDetail;
  107.  
  108. // insert Account records
  109. createdAcc = doAccountCreation(accRecResp);
  110. findObject = 'Account';
  111. insert createdAcc;
  112.  
  113. if(createdAcc.Id != null)
  114. {
  115. res.statusCode = 201;
  116. myAccResp = new QAC_Responses.AccountResponse('201',
  117. accRecResp.referenceId,
  118. createdAcc.Id,
  119. accRecResp.iataCode,
  120. //accRecResp.tidsCode,
  121. accRecResp.AgencyABN);
  122.  
  123. qacResp.accountResponse = myAccResp;
  124.  
  125. }
  126.  
  127. // insert Contact (or) ACR Records
  128. for(QAC_AccountServicesAPI.ContactDetails contRecResp : accRecResp.contactDetails)
  129. {
  130. // do some logics here and add ..
  131. newContactList.add(doContactCreation(contRecResp,createdAcc));
  132. }
  133. if(!newContactList.isEmpty())
  134. {
  135. findObject = 'Contact';
  136. insert newContactList;
  137. for(Contact eachContact : newContactList){
  138. ++i;
  139. if(eachContact.Id != null){
  140. res.statusCode = 201;
  141. myContResp = new QAC_Responses.contactResponse('201',i+'Contact(s) Created Successfully',
  142. eachContact.Id,
  143. eachContact.email,
  144. eachContact.LastName,
  145. eachContact.phone);
  146. myContRespList.add(myContResp);
  147. }
  148. }
  149. i=0;
  150. qacResp.contactResponses = myContRespList;
  151. }
  152. system.debug('QAC Response**'+qacResp);
  153. return qacResp;
  154. }
  155. catch(Exception ex)
  156. {
  157. isException = true;
  158.  
  159. res.statusCode = 400;
  160. String sfErrMessage = ex.getMessage();
  161.  
  162. if(ex.getMessage().contains(':')){
  163. sfErrMessage = ex.getMessage().split(':')[1];
  164. }
  165.  
  166. if(ex.getTypeName().contains('JSON')){
  167. jsonErrMessage = 'Issue with JSON Format. Please check your Payload';
  168. }
  169. else{
  170. res.statusCode = (findObject == 'Account') ? 500 : 201 ;
  171. jsonErrMessage = 'Found errors, Registration Process Failed for - '+findObject+'.Please contact Salesforce';
  172. }
  173.  
  174. myExcepResp = new QAC_Responses.ExceptionResponse('400',jsonErrMessage,
  175. 'Exception Found',sfErrMessage);
  176. qacResp.exceptionResponse = myExcepResp;
  177.  
  178. system.debug('QAC Response**'+qacResp);
  179. return qacResp;
  180. }
  181. }
  182. }
Add Comment
Please, Sign In to add comment