import { LightningElement, api, track, wire } from 'lwc'; import { CurrentPageReference } from 'lightning/navigation'; import initData from '@salesforce/apex/LexNewAndEditAgencyContactPIPLController.initData'; import { NavigationMixin } from 'lightning/navigation'; import { AWSService } from 'c/piUtils'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import { loadStyle } from 'lightning/platformResourceLoader'; import lexPIPLCustomStyle from '@salesforce/resourceUrl/lexPIPLCustomStyle'; import { decodeDefaultFieldValues } from "lightning/pageReferenceUtils"; import lexAgencyContactRecordTypeId from '@salesforce/label/c.lexAgencyContactRecordTypeId'; import { conmentCancel } from "c/lexPIPLConmentMethods"; import AgencyContact from '@salesforce/schema/Agency_Contact__c'; import { getObjectInfo } from 'lightning/uiObjectInfoApi'; // Add By Li Jun 20231025 Start import LexRecordNotCreateable from '@salesforce/label/c.LexRecordNotCreateable'; import LexNotCreateNote from '@salesforce/label/c.LexNotCreateNote'; const customLabel = { LexRecordNotCreateable, LexNotCreateNote } // Add By Li Jun 20231025 End export default class LexNewAndEditAgencyContactPIPL extends NavigationMixin(LightningElement) { sobjectType = 'Agency_Contact__c'; @track customLabel = customLabel; lexAgencyContactRecordTypeId = lexAgencyContactRecordTypeId; @api recordId; @track recordCloneId; @track recordTypeId = null; @wire(CurrentPageReference) pageRef; @track recordData = {}; @track title; @track isLoading = false; @track layout = []; @track staticResource; @track staticResourceContact; @track piFieldsMap; @track abstractData = ''; @track data = {}; @track piplData = {}; @track isNewMode = false; @track isCloneMode = false; @track isEditMode = false; @track sectionName = []; @track AWSToSobjectMap = {}; @track AWSDataId = ''; @track txId = ''; @track isShowSearchAccount = false; @track hospitalId = ''; @track contactId = ''; @track saveAndNew = false; AWSService; @track modifyObj = {}; modifyArray = ['']; @track isInitSuccess = false; defaultValues = {}; fieldLabel = {}; @wire(getObjectInfo, { objectApiName: AgencyContact }) getReportInfo({data,error}){ if(data){ this.fieldLabel = data.fields; } } connectedCallback() { this.isLoading = true; console.log('enter connectedCallback'); loadStyle(this, lexPIPLCustomStyle); this.AWSService = new AWSService(); if (!this.recordId) { this.title = '新建.客户人员'; this.isNewMode = true; } if (this.recordId) { this.title = '编辑.客户人员'; this.isEditMode = true; } if (this.pageRef && this.pageRef.state) { this.recordTypeId = this.pageRef.state.recordTypeId; console.log('this.recordTypeId = ' + this.recordTypeId); this.isCloneMode = Boolean(this.pageRef.state.c__isClone); console.log('this.isCloneMode = ' + this.isCloneMode) //克隆 if (this.isCloneMode) { this.isNewMode = false; this.isEditMode = false; this.recordCloneId = this.pageRef.state.c__recordId; console.log('this.recordCloneId = ' + this.recordCloneId); this.title = '克隆.客户人员'; } if (this.pageRef.state.defaultFieldValues) { this.defaultValues = decodeDefaultFieldValues( this.pageRef.state.defaultFieldValues ); console.log('this.contactAWSDataId = ' + this.contactAWSDataId); } console.log("defaultValues:", JSON.stringify(this.defaultValues)); } console.log('recordId = ' + this.recordId + ' recordCloneId = ' + this.recordCloneId + ' recordTypeId = ' + this.recordTypeId + ' sobjectType = ' + this.sobjectType); let id = this.isCloneMode == true ? this.recordCloneId : this.recordId; debugger initData({ rid: id, recordTypeId: this.recordTypeId, sobjectType: this.sobjectType }).then((r) => { r = JSON.parse(JSON.stringify(r)); console.log('r = ' + JSON.stringify(r)) if (r.status == 'Success') { if (this.recordTypeId == null && r.entity.recordTypeId != this.lexAgencyContactRecordTypeId) { this.recordTypeId = r.entity.recordTypeId; } this.AWSDataId = r.entity.AWSDataId; console.log('this.recordTypeId = ' + this.recordTypeId); let layout = JSON.parse(r.entity.layout); this.recordData = r.entity.data; console.log('this.recordData = ' + JSON.stringify(this.recordData)); let index = 1; this.sectionName = layout.map(s => s.label); layout = this.sortMetaLayouts(layout); for (let s of layout) { s['showSection'] = false; if(s['editHeading'] || s['detailHeading']){ s['showSection'] = true; } s['showLabel'] = false; for (let c of s.layoutColumns) { c['index'] = index++; if(c.layoutItems.length != 0 && s['showLabel'] == false){ s['showLabel'] = true; } if (c.layoutItems) { for (let item of c.layoutItems) { item['isDisable'] = item.behavior == 'Readonly' ? true : false; item['isRequired'] = item.behavior == 'Required' ? true : false; item['isModify'] = false; let fieldName = item['field']; if (this.isCloneMode && fieldName != 'UnifiedI_Contact_ID__c' && fieldName != 'ServicePlatformCode__c') { item['value'] = this.recordData[fieldName]; } if (this.isNewMode && this.defaultValues && Object.keys(this.defaultValues).length != 0) { if(this.defaultValues[item["field"]]){ item["value"] = this.defaultValues[item["field"]]; } } } } } } this.layout = layout; console.log('this.layout = ' + JSON.stringify(this.layout)) this.AWSToSobjectMap = JSON.parse(JSON.stringify(r.entity.AWSToSobjectNonEncryptedMap)); this.staticResource = JSON.parse(r.entity.staticResource); //编辑 if (this.isEditMode || this.isCloneMode) { //解密.客户人员的加密字段 this.querySobjectFromAWS(); } this.isInitSuccess = true; } else { //this.showToast('Error', r.msg); this.showMyToast(r.msg , '' , 'error') } }) } handleLoad() { console.log('handleLoad') console.log('this.isInitSuccess = ' + this.isInitSuccess) if (this.isInitSuccess) { let time = 1000; if (!this.isNewMode) { time = 2000; } let that = this; setTimeout(() => { that.isLoading = false; }, time); } } sortMetaLayouts(layout){ layout.forEach(s => { let c1 = (s.layoutColumns[0].layoutItems != null) ? s.layoutColumns[0].layoutItems : []; let c2 = []; if(s.layoutColumns.length > 1){ c2 = (s.layoutColumns[1].layoutItems != null) ? s.layoutColumns[1].layoutItems : []; } else{ s.layoutColumns.push({ "reserved": null, "layoutItems": [] }); } let emptySpaceItem = { "width": null, "showScrollbars": null, "showLabel": null, "scontrol": null, "reportChartComponent": null, "page_x": null, "height": null, "field": null, "emptySpace": true, "customLink": null, "component": null, "canvas": null, "behavior": null, "analyticsCloudComponent": null } let maxSize = 0 if(c1.length > c2.length){ maxSize = c1.length; for(let i = 0; i < maxSize; i++){ if(c2.length == maxSize){ break; } c2.push(emptySpaceItem); } }else{ maxSize = c2.length; for(let i = 0; i < maxSize; i++){ if(c1.length == maxSize){ break; } c1.push(emptySpaceItem); } } let tempList = []; let index = 0; for(let i = 0; i < maxSize; i++){ tempList.push(c1[i]); if(tempList.length == maxSize){ s.layoutColumns[index].layoutItems = tempList; tempList = []; tempList.push(c2[i]); index++; }else{ tempList.push(c2[i]); if(tempList.length == maxSize){ s.layoutColumns[index].layoutItems = tempList; tempList = []; index++; } } } }) layout = [...layout]; return layout; } querySobjectFromAWS() { let that = this; this.AWSService.query(this.staticResource.queryUrl, this.AWSDataId, function (data) { console.log('queryLeadFromAWSIFS data = ' + JSON.stringify(data)); if (data.object) { for (let s of that.layout) { for (let lc of s.layoutColumns) { if (lc.layoutItems) { for (let c of lc.layoutItems) { for (let f in that.AWSToSobjectMap) { if (data.object.hasOwnProperty(f) && c['field'] == that.AWSToSobjectMap[f]) { c['value'] = data.object[f] == null ? '' : data.object[f]; } } } } } } } that.isLoading = false; }, this.staticResource.token) } dataChange(event) { let fieldName = event.target.getAttribute("data-field"); let value = event.detail.value; console.log("fieldName = " + fieldName + " value = " + value); } clickSubmit(){ console.log('clickSubmit'); const btn = this.template.querySelector( ".hidden" ); if( btn ){ btn.click(); } } clickSubmitAndNew(){ console.log('clickSubmitAndNew'); this.saveAndNew = true; const btn = this.template.querySelector(".hidden"); if (btn) { btn.click(); } } handleSubmit(event) { this.isLoading = true; //1. Get Sobject Information from Form console.log('handleSubmit'); event.preventDefault(); const fields = event.detail.fields; console.log('this.modifyObj = ' + JSON.stringify(this.modifyObj)); Object.assign(fields, this.modifyObj); console.log('fields = ' + JSON.stringify(fields)); //2. select cannot actively select redaction option let validationResultMessage = this.validateFieldValueFormate(fields); console.log(validationResultMessage); if (validationResultMessage) { //this.showToast('Error', validationResultMessage); this.showMyToast(validationResultMessage , '' , 'error') return } //3. Check Required Field let checkRequiredFieldMsgResult = this.checkRequiredFieldMsg(fields); console.log('checkRequiredFieldMsgResult = ' + checkRequiredFieldMsgResult); if (checkRequiredFieldMsgResult != '') { //this.showToast('Error', checkRequiredFieldMsgResult + '需要进行填写'); this.showMyToast(checkRequiredFieldMsgResult + '需要进行填写' , '' , 'error') return } //4. Prepare the payload for New PI API To AWS - To Do let payloadForNewPI = this.getPIPayload(fields); console.log('payloadForNewPI = ' + payloadForNewPI); debugger //5. PI To AWS //新建/克隆 if (this.isNewMode || this.isCloneMode || this.AWSDataId == '') { this.NewPIToAWS(payloadForNewPI, fields); return } //编辑 if (this.isEditMode) { this.UpdatePIToAWS(payloadForNewPI, fields); } } //验证字段 validateFieldValueFormate(fields) { let error_msg = ''; let b = false; for (let key in fields) { if (fields[key] == "*****") b = true; } if (b) error_msg = '下拉框不能主动选择密文选项'; return error_msg; } //验证required字段需要进行填写 checkRequiredFieldMsg(fields) { let msg = ''; try { for (let s of this.layout) { for (let lc of s.layoutColumns) { if (lc.layoutItems) { for (let c of lc.layoutItems) { if (!c.isDisable && c.isRequired && (fields[c.field] == null || fields[c.field] === '')) { msg += ";" + this.fieldLabel[c.field].label ; } } } } } } catch (err) { //this.showToast('Error', err.message); this.showMyToast(err.message , '' , 'error') } msg = msg.substring(1); return msg; } //获取PI字段 getPIPayload(sobjJsonLwc) { console.log() let leadPayloadList = []; let leadPIData = {}; for (let f in this.AWSToSobjectMap) { if (sobjJsonLwc.hasOwnProperty(this.AWSToSobjectMap[f])) { leadPIData[f] = sobjJsonLwc[this.AWSToSobjectMap[f]] } else { console.log(this.AWSToSobjectMap[f] + 'is not in sobjJsonLwc'); } } leadPIData.medicalStaffFullName = leadPIData.lastName; leadPIData.sfRecordId = ''; console.log('Sobject PI Data x :' + leadPIData); leadPayloadList.push(leadPIData); console.log('leadPayloadList = ' + JSON.stringify(leadPayloadList)); return JSON.stringify(leadPayloadList); } //新建保存ToAWS NewPIToAWS(payloadForNewPI, fields) { let that = this; debugger this.AWSService.post(this.staticResource.newUrl, payloadForNewPI, (result) => { if (result && result.object) { console.log('result = ' + JSON.stringify(result)); for (let f in that.AWSToSobjectMap) { if (result.object[0].hasOwnProperty(f)) { fields[that.AWSToSobjectMap[f]] = result.object[0][f]; console.log('that.AWSToSobjectMap[f] = ' + that.AWSToSobjectMap[f]); console.log('fields[that.AWSToSobjectMap[f]] = ' + fields[that.AWSToSobjectMap[f]]); } else { console.log(f + 'is not in result.object[0]'); } } fields['AWS_Data_Id__c'] = result.object[0].dataId; that.txId = result.txId; console.log('fields = ' + JSON.stringify(fields)) that.template.querySelector('lightning-record-edit-form').submit(fields) } else { console.log('result = ' + JSON.stringify(result)); } }, this.staticResource.token); } //编辑保存ToAWS UpdatePIToAWS(payloadForNewPI, fields) { debugger let obj = JSON.parse(payloadForNewPI); if(this.AWSDataId){ obj[0].dataId = this.AWSDataId; }else{ this.showMyToast("Error", '未找到该记录的AWS Id,请确认记录完整性', 'error'); return; } obj[0].dataId = this.AWSDataId; let payloadForNewPIJson = JSON.stringify(obj); let that = this; this.AWSService.post(this.staticResource.updateUrl, payloadForNewPIJson, (result) => { if (result && result.object) { console.log('result = ' + JSON.stringify(result)); for (let f in that.AWSToSobjectMap) { if (result.object[0].hasOwnProperty(f)) { fields[that.AWSToSobjectMap[f]] = result.object[0][f]; console.log('that.AWSToSobjectMap[f] = ' + that.AWSToSobjectMap[f]); console.log('fields[that.AWSToSobjectMap[f]] = ' + fields[that.AWSToSobjectMap[f]]); } else { console.log(f + 'is not in result.object[0]'); } } fields['AWS_Data_Id__c'] = that.AWSDataId; that.txId = result.txId; console.log('that.txId = ' + that.txId); console.log('PI After fields = ' + JSON.stringify(fields)); if (fields.RecordTypeId) { fields['RecordTypeId'] = fields.RecordTypeId.substring(1, fields.RecordTypeId.length - 1); } //保存到后端 console.log('update submit = ' + JSON.stringify(fields)); debugger that.template.querySelector('lightning-record-edit-form').submit(fields); } else { console.log('result = ' + JSON.stringify(result)); } }, this.staticResource.token); } //提交保存成功 handleSuccess(event) { debugger let updatedRecord = event.detail.id; console.log('onsuccess: ', updatedRecord); console.log('confirmTrans'); let that = this; this.AWSService.confirm(true, updatedRecord, this.txId, this.staticResource.token, this.staticResource.transactionUrl, function (result) { console.log('result = ' + JSON.stringify(result)) that.showToast('Success', '保存成功'); console.log('updatedRecord = ' + updatedRecord) if(that.saveAndNew){ that[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: that.sobjectType, actionName: 'new' }, state: { recordTypeId: that.recordTypeId } }); }else{ that[NavigationMixin.Navigate]({ type: 'standard__recordPage', attributes: { actionName: "view", recordId: updatedRecord, objectApiName: that.sobjectType } }); } }); } //提交保存失败 handleError(event) { debugger event.preventDefault(); event.stopImmediatePropagation(); console.log('event.detail = ' + JSON.stringify(event.detail)) //字段级别的报错 if (event.detail.output && event.detail.output.fieldErrors && Object.keys(event.detail.output.fieldErrors).length != 0) { let msg = ''; for (let key in event.detail.output.fieldErrors) { msg += ',' + event.detail.output.fieldErrors[key][0].message; } msg = msg.substring(1); this.showMyToast(event.detail.message + ' : ' + msg, '', 'error'); } else { //add by Li Jun 20231025 Start if(event.detail.message != '' && this.customLabel.LexRecordNotCreateable.indexOf(event.detail.message) != -1){ this.showMyToast("Error", this.customLabel.LexNotCreateNote, 'error'); }else{ this.showMyToast("Error", event.detail.message + ' : ' + event.detail.detail, 'error'); } //add by Li Jun 20231025 End } //add by Li Jun 20231025 Start if(this.txId && this.staticResource){ this.AWSService.confirm(false,'',this.txId,this.staticResource.token,this.staticResource.transactionUrl,function(result){ console.log('result = ' + JSON.stringify(result)) }); } //add by Li Jun 20231025 End } //change事件 dataChange(event) { let fieldName = event.target.getAttribute("data-field"); let value = event.detail.value; console.log("fieldName = " + fieldName + " value = " + event.detail.value); if (this.modifyArray.indexOf(fieldName) != -1) { switch (fieldName) { case "LastName": this.modifyObj[fieldName] = value; break; } } } //取消 cancel() { conmentCancel(this.defaultValues); } //标准新建页面跳转 RedirectStandardNewPage() { this[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: 'Contact', actionName: 'new' }, state: { nooverride: '1', recordTypeId: this.recordTypeId } }); } //标准编辑页面跳转 RedirectStandardEditPage() { this[NavigationMixin.Navigate]({ type: 'standard__recordPage', attributes: { objectApiName: 'Contact', recordId: this.recordId, actionName: 'edit', }, state: { nooverride: '1', } }); } //显示信息 showToast(type, msg) { this.isLoading = false; const event = new ShowToastEvent({ title: type, variant: type, message: msg }); this.dispatchEvent(event); } //显示信息 showMyToast(title, message, variant) { console.log('show custom message'); let iconName = ''; let content = ''; if (variant == 'success') { iconName = 'utility:check'; } else { iconName = 'utility:error'; } if (message != '') { content = '

' + title + '

' + message + '
'; } else { content = '

' + title + '

'; } this.template .querySelector('c-common-toast') .showToast(variant, content, iconName, 10000); this.isLoading = false; } }