class AWSService { static sfSessionId = ''; static insertModule = 'Insert AWS '; static updateModule = 'Update AWS '; static queryModule = 'Query AWS '; static searchModule = 'Search AWS '; static confirmTrans = 'Confirm Transaction To AWS'; static successStatus = 'success'; static failStatus = 'fail'; static insertCalloutLog(module,url,request,response,status){ if(this.sfSessionId){ sforce.connection.sessionId = this.sfSessionId; let transLog = new sforce.SObject('Transaction_Log__c'); transLog.AWS_Data_Id__c = ''; transLog.Module__c = module; transLog.Interface_URL__c = url; transLog.Request__c = request; transLog.Response__c = response; transLog.Status__c = status; let insertLogResult = sforce.connection.create([transLog]); if(insertLogResult[0].getBoolean(this.successStatus)) { console.log('Insert Log Id: ' + insertLogResult[0].id); return insertLogResult[0].id; }else { console.log('Faield to insert log'); return ''; } } } //Search Contact static search(searchUrl,requestSearchPayload,searchBack,token) { fetch(searchUrl, { method: 'POST', body: requestSearchPayload, headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((data) => { return data.json(); }).then((result) => { this.insertCalloutLog(this.searchModule,searchUrl,JSON.stringify(requestSearchPayload),JSON.stringify(result),this.successStatus); searchBack(result); }).catch(error => { this.insertCalloutLog(this.searchModule,searchUrl,JSON.stringify(requestSearchPayload),JSON.stringify(error),this.failStatus); console.log(error); }); } //query static query(queryURL, awsDataId, queryback, token) { fetch(queryURL + '?dataId=' + awsDataId, { method: 'GET', headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((data) => { return data.json(); }).then((data) => { this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(data),this.successStatus); queryback(data); }).catch(error => { this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(error),this.failStatus); console.log(error); }); } //queryTSRepair static queryTSRepair(queryURL, awsDataId, queryback, token,number) { fetch(queryURL + '?dataId=' + awsDataId, { method: 'GET', headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((data) => { return data.json(); }).then((data) => { this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(data),this.successStatus); queryback(data,number); }).catch(error => { this.insertCalloutLog(this.queryModule,queryURL,queryURL + '?dataId=' + awsDataId,JSON.stringify(error),this.failStatus); console.log(error); }); } //insert static insert(newURL, payloadJson, payloadForNewPI, controllerSaveMethod, token, transactionURL,isNewMode,insertOrUpdateBack,redirectCallBack) { console.log('Process New PI Data'); console.log(JSON.stringify(payloadForNewPI)); fetch(newURL, { method: 'POST', body: payloadForNewPI, headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((data) => { console.log('data=' + JSON.stringify(data)); return data.json(); }).then((result) => { this.insertCalloutLog(this.insertModule,newURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus); AWSService.back(result, payloadJson, transactionURL, token, controllerSaveMethod,isNewMode,insertOrUpdateBack,redirectCallBack); }).catch(error => { this.insertCalloutLog(this.insertModule,newURL,JSON.stringify(payloadForNewPI),JSON.stringify(error),this.failStatus); console.log(error); }); } //update static update(updateURL, payloadJson, payloadForNewPI, controllerSaveMethod, token, transactionURL,isNewMode,insertOrUpdateBack,redirectCallBack) { console.log('Process New PI Data'); console.log(JSON.stringify(payloadForNewPI)); fetch(updateURL, { method: 'POST', body: payloadForNewPI, headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((data) => { console.log('data=' + JSON.stringify(data)); return data.json(); }).then((result) => { this.insertCalloutLog(this.updateModule,updateURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus); AWSService.back(result, payloadJson, transactionURL, token, controllerSaveMethod,isNewMode,insertOrUpdateBack,redirectCallBack); }).catch(error => { this.insertCalloutLog(this.updateModule,updateURL,JSON.stringify(payloadForNewPI),JSON.stringify(error),this.failStatus); console.log(error); }); } //update static post(postURL, payloadForNewPI, callback, token) { console.log('Process New PI Data'); console.log(JSON.stringify(payloadForNewPI)); fetch(postURL, { method: 'POST', body: payloadForNewPI, headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((data) => { console.log('data=' + JSON.stringify(data)); return data.json(); }).then((result) => { this.insertCalloutLog(this.insertModule,postURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus); callback(result); }).catch(error => { this.insertCalloutLog(this.insertModule,postURL,JSON.stringify(payloadForNewPI),JSON.stringify(error),this.failStatus); console.log(error); }); } static sfdcBack = function sfdcBack(event, result, transId, token, transactionURL,redirectCallBack) { let sfId = ''; if (event.status) { console.log('sf Id from SF Backend:' + JSON.stringify(result)); if (result.status == 'success') { sfId = result.recordId; let transParameters = { txId: transId, sfRecordId:sfId, isSuccess: 1 }; console.log(transParameters); fetch(transactionURL, { method: 'POST', body: JSON.stringify(transParameters), headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((result) => { this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(result),this.successStatus); redirectCallBack(sfId,''); }).catch(error => { this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(error),this.failStatus); console.log(error); }); } else { let errorMsg = result.message; let transParameters = { txId: transId, sfRecordId:sfId, isSuccess: 0 }; console.log(transParameters); fetch(transactionURL, { method: 'POST', body: JSON.stringify(transParameters), headers: { 'Content-Type': 'application/json', 'pi-token': token } }).then((result) => { this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(result),this.successStatus); redirectCallBack('',errorMsg); }).catch(error => { this.insertCalloutLog(this.confirmTrans,transactionURL,JSON.stringify(transParameters),JSON.stringify(error),this.failStatus); console.log(error); }); } } } // AWSService.back(result, payloadJson, transactionURL, token, controllerSaveMethod); static back = function back(result, payloadJson, transactionURL, token, controllerSaveMethod,isNewMode,insertOrUpdateBack,redirectCallBack) { let payloadJsonStr = JSON.stringify(insertOrUpdateBack(payloadJson, result,isNewMode)); let transId = result.txId + ''; Visualforce.remoting.Manager.invokeAction( controllerSaveMethod, // example '{!$RemoteAction.NewAndEditLeadController.saveLead}' payloadJsonStr, transId, isNewMode, function (result, event) { AWSService.sfdcBack(event, result, transId, token, transactionURL,redirectCallBack); }, { escape: true } ); } };