binxie
2023-07-11 67765d99c19ad354e66f8646d8597622b5d81ef7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { LightningElement,wire,track,api} from 'lwc';
import { CurrentPageReference } from "lightning/navigation";
import { CloseActionScreenEvent } from 'lightning/actions';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
 
import init from '@salesforce/apex/lexNewRepairAuraController.init';
 
export default class lexNewRepairLWC extends NavigationMixin(LightningElement) {
 
    // Flexipage provides recordId 
    @api recordId;
    // @api recordId;
    @api urlStr;
    @api objectApiName;
 
    /* @wire(CurrentPageReference)
     getStateParameters(currentPageReference) {
             console.log(111);
             console.log(currentPageReference);
 
         if (currentPageReference) {
           const urlValue = currentPageReference.state.recordId;
           if (urlValue) {
             let str = `${urlValue}`;
             console.log("str");
             console.log(str);
             this.recordId = str;
           }
         }
     }*/
 
    objectName = 'Repair__c';
    uiPageLayoutView;
    isNewMode;
    //属性默认值
    defaultValue = {};
    isLoading = true;
    activeSections;
    isSaveAndNew = false;
    //更新操作时 不显示保存新建
    hasRecordId = false;
    recordIdStr;
    recordTypeId = '';
 
    connectedCallback(){ 
        console.log(this.recordId); 
        console.log(JSON.parse(this.urlStr)); 
        if (!this.recordId) {
          this.hasRecordId = true;
          // urlStr 传参处理
          let urlObj = JSON.parse(this.urlStr);
          if (urlObj.hasOwnProperty("CF00N10000009H1rR_lkid") || urlObj.hasOwnProperty("retURL") ) {
              console.log('urlObj.hasOwnProperty:',urlObj);
              //  init  向Controller发送请求获取数据
              init({
                recordId : this.recordId,urlStr : this.urlStr
              }).then(result => {
                  console.log("result");
                  console.log(result);
                  this.defaultValue = result;
                  //返回结果赋值
                  if (result.status) {
                    this.recordTypeId = result.recordTypeId;
                  }
              }).catch(error => {
                  console.log("error");
                  console.log(error);
              }).finally(() => {
              });  
          }
        }
    }
    //获取Layout字段
    handleRecordEditFormLoad(event) {
        if (event.detail.layout == undefined) {
          return;
        }
        console.log('Layout => ', JSON.stringify(event.detail.layout));
        this.uiPageLayoutView = event.detail.layout;
        console.log('Layout1 => ',this.uiPageLayoutView);
    
       //页面内容初始化 del
       //to del !  err: Delivered_Product   02i10000004tNXIAA2  Hospital  0011000001g11rDAAQ
        // this.defaultValue['Delivered_Product'] = '02i10000004tMKgAAM';
        // // this.defaultValue['Delivered_Product'] = '02i10000004tNXIAA2';
        // this.defaultValue['Account'] = '0011000001g11rDAAQ';
        // this.defaultValue['SalesOfficeCode_selection'] = '北京';
        // this.defaultValue['On_site_repair'] = 'RC修理';
        // this.defaultValue['Repair_Detail'] = 'testLwcSaveAndNew';
        // this.defaultValue['Hospital'] = '0011000001g0R8vAAE';
        // // this.defaultValue['Hospital'] = '0011000001g11rDAAQ';
        // //  0011000000V9SG5AAN
        // this.defaultValue['work_location_select'] = '北京办事处';
 
        const inputFields = this.template.querySelectorAll('lightning-input-field');
        if (inputFields) {
           inputFields.forEach(field => {
            let fieldName = field.name.replace("__c","");
            if (this.defaultValue.hasOwnProperty(fieldName)) {
                field.value= this.defaultValue[fieldName];
            }
           });
       }
 
       let checkedSections = new Set();
        if (this.uiPageLayoutView.sections.length > 0) {
          for(const tabs of this.uiPageLayoutView.sections){
            checkedSections.add(tabs.id);
          }
          this.activeSections =  Array.from(checkedSections);
        }
        this.isLoading = false;   
        this.recordIdStr = this.recordId;
              
    }
    //保存  保存新建提交事件
    handleSubmit(event){
       this.isLoading = true;
       console.log("handleSubmit");
       event.preventDefault();       // stop the form from submitting
       const fields = event.detail.fields;
       /*console.log("fields");
       console.log(fields);*/
       this.template.querySelector('lightning-record-edit-form').submit(fields);
       // console.log("success");
    }
    handleSubmitAndNew(event){
      this.isSaveAndNew = true;
    }
    //成功后跳转
    handleSucess(event){
      // console.log("handleSucess");
      const updatedRecord = event.detail.id;
      console.log('onsuccess: ', updatedRecord);
      if (this.isSaveAndNew) {
          // console.log("isSaveAndNew");
         window.location.reload();
      }else if (event.detail.id) {
        // console.log("NavigationMixin");
         this[NavigationMixin.Navigate]({
              type:'standard__recordPage',
              attributes:{
                  recordId:updatedRecord,
                  objectApiName:this.objectName,
                  actionName:'view'
              }
          });
      }
      this.isLoading = false;
    }
    //对象验证,属性验证
    handleError(event) {
        console.log("handleError event");
        console.log(JSON.stringify(event.detail));
    }
    handleCancel(event) {
      window.location.href = "https://ocsm--partial.sandbox.lightning.force.com/lightning/o/Repair__c/list?filterName=Recent&0.source=alohaHeader";
    }
 
 
    showToast(theTitle, theMessage, theVariant) {
          const event = new ShowToastEvent({
              title: theTitle,
              message: theMessage,
              variant: theVariant
          });
          this.dispatchEvent(event);
    }
 
}