From 0953b3b5fcf19c627c6479a6b86648df653c40db Mon Sep 17 00:00:00 2001
From: buli <137736985@qq.com>
Date: 星期一, 09 五月 2022 17:50:33 +0800
Subject: [PATCH] SF PIPL Page Fix Lookup Required issue

---
 force-app/main/default/pages/NewAndEditOpportunity.page |  136 +++++++++++++++++++++++++++++++-------------
 1 files changed, 95 insertions(+), 41 deletions(-)

diff --git a/force-app/main/default/pages/NewAndEditOpportunity.page b/force-app/main/default/pages/NewAndEditOpportunity.page
index 29373ff..6fa6be2 100644
--- a/force-app/main/default/pages/NewAndEditOpportunity.page
+++ b/force-app/main/default/pages/NewAndEditOpportunity.page
@@ -1,10 +1,3 @@
-<!--
-    @description       : 
-    @author            : ChangeMeIn@UserSettingsUnder.SFDoc
-    @group             : 
-    @last modified on  : 03-08-2022
-    @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
--->
 <apex:page standardController="Opportunity" extensions="NewAndEditOpportunityController" id="page">
   <apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" />
     <script src="../../soap/ajax/53.0/connection.js" type="text/javascript"></script>
@@ -69,7 +62,7 @@
                 
                 //payloadJson.AWS_Data_Id__c = r.object[0].dataId;
                 payloadJson.AWS_Data_Id__c = '{!AWSDataId}';
-                if (isNewMode) {
+                if (isNewMode || {!isCloneMode}) {
                     payloadJson.AWS_Data_Id__c = r.object[0].dataId;
                 } else {
                     payloadJson.AWS_Data_Id__c = '{!AWSDataId}';
@@ -137,10 +130,23 @@
         var VLookUpFields = {! VLookUpFieldsJson};
         function ProcessPI(sobjJson, payloadForNewPI) {
             blockme();
-            if ({!isNewMode}) {
+            if ({!isNewMode} || {!isCloneMode}) {
             	NewPIToAWS(sobjJson, payloadForNewPI)
             }else {
                 UpdatePIToAWS(sobjJson, payloadForNewPI)
+            }
+        }
+        
+        function EditButton(isDisabled){
+            var topele = document.getElementById('topButtonRow');
+            var bottomele = document.getElementById('bottomButtonRow');
+
+            if (isDisabled) {
+                topele.classList.add("disabledbutton");
+                bottomele.classList.add("disabledbutton");
+            }else {
+                topele.classList.remove("disabledbutton");
+                bottomele.classList.remove("disabledbutton");
             }
         }
 
@@ -173,18 +179,18 @@
 
         function validateFieldValueFormate() {
             let error_msg = '';
-            let textEmail = "[data-id='Email']";
-            let textPhone = "[data-id='MobilePhone']";
-            //Email
-            let email = document.querySelector(textEmail);
-            if(email && !/^[\w-]{3,12}@[\da-zA-Z]{2,16}\.[a-zA-Z]+$/.test(email.value)){
-                error_msg += ';閭欢鏍煎紡閿欒';
-            }
-            let phone = document.querySelector(textPhone);
+            // let textEmail = "[data-id='Email']";
+            // let textPhone = "[data-id='MobilePhone']";
+            // //Email
+            // let email = document.querySelector(textEmail);
+            // if(email && !/^[\w-]{3,12}@[\da-zA-Z]{2,16}\.[a-zA-Z]+$/.test(email.value)){
+            //     error_msg += ';閭欢鏍煎紡閿欒';
+            // }
+            // let phone = document.querySelector(textPhone);
 
-            if(phone && !/^1[3|5|8|7][0-9]\d{4,8}$/.test(phone.value)){
-                error_msg += ';鐢佃瘽鍙风爜閿欒';
-            }
+            // if(phone && !/^1[3|5|8|7][0-9]\d{4,8}$/.test(phone.value)){
+            //     error_msg += ';鐢佃瘽鍙风爜閿欒';
+            // }
 
             for(let e of document.getElementsByTagName('select')){
                 for(let op of e.options){
@@ -226,9 +232,9 @@
                             v = ele.value;
                         }
 
-                        if(v && v != "000000000000000"){
+                        //if(v && v != "000000000000000"){
                             result[field_api_name] = v;
-                        }
+                        //}
                         
                     }
                 } else if (ele.type == 'checkbox') {
@@ -243,6 +249,12 @@
                 } else {
                     result[field_api_name] = ele.value;
                 }
+            }
+            // if(document.querySelector("[data-id='HeadOfCooperationArea__c']")){
+            //     result['HeadOfCooperationArea__c'] = document.getElementById(document.querySelector("[data-id='HeadOfCooperationArea__c']").children[7].children[0].id + '_lkid').value;
+            // }
+            if('{!primaryContactId}'){
+                result['ContactId'] = '{!primaryContactId}';
             }
             return result;
         }
@@ -298,7 +310,9 @@
         function checkRequiredFieldMsg(formData) {
             let blankRequiredFields = '';
             for(i = 0;i<requiredFieldAPIList.length;i++){
-                if(formData[requiredFieldAPIList[i]]){
+                if(formData[requiredFieldAPIList[i]] && (VLookUpFields.indexOf(requiredFieldAPIList[i]) == -1)){
+                    continue;
+                }else if((VLookUpFields.indexOf(requiredFieldAPIList[i]) != -1) && formData[requiredFieldAPIList[i]] != '000000000000000'){
                     continue;
                 }else{
                     if(blankRequiredFields == ''){
@@ -313,6 +327,7 @@
         }
         
         function saveSobjectProcess(save_and_new) {
+            EditButton(true);
             if(save_and_new){
                 config.SaveAndNew = true;
             }
@@ -351,6 +366,7 @@
             let errorMsgNode = document.getElementById("page:form:block:msgContent");
             errorMsgNode.innerText = errorMsg;
             errorMsgNode.className = 'pbError';
+            EditButton(false);
         }
         function hiddenErrorMsgNode() {
             let errorMsgNode = document.getElementById("page:form:block:msgContent");
@@ -393,16 +409,20 @@
         }
 
         function queryContactName() {
-            let sfId = document.getElementById(document.querySelector("[data-id='EndUser__c']").id + '_lkid').value;
-            let contactsInfo = JSON.parse('{!LookUpOverrideFieldsMapJson}');
-            let dataId = contactsInfo[sfId];
-            let queryContactBack = function(data){
-                //To Do later
-                console.log('ContactData = ' + data.object);
-                
-                document.querySelector("[data-id='EndUser__c']").value = data.object.lastName;
+            if(document.querySelector("[data-id='EndUser__c']")){
+                let sfId = document.getElementById(document.querySelector("[data-id='EndUser__c']").id + '_lkid').value;
+                let contactsInfo = JSON.parse('{!LookUpOverrideFieldsMapJson}');
+                let dataId = contactsInfo[sfId];
+                let queryContactBack = function(data){
+                    //To Do later
+                    console.log('ContactData = ' + data.object);
+                    if(!data.object){
+                        return;
+                    }
+                    document.querySelector("[data-id='EndUser__c']").value = data.object.lastName;
+                }
+                AWSService.query(staticResourcesContact.queryUrl, dataId, queryContactBack, staticResources.token);
             }
-            AWSService.query(staticResourcesContact.queryUrl, dataId, queryContactBack, staticResources.token);
             // queryContactNameFetch(dataId, 'EndUser__c');
         }
         
@@ -428,7 +448,8 @@
             console.log(accountValue);
             if(accountValue !='000000000000000'){
                 let baseUrl = "/apex/SearchContactPage";
-                let suffixUrl = "?contactId="+contactNodeId+"&accountId="+accountValue;
+                let searchContactKeyWord = document.querySelector("[data-id='EndUser__c']").value;
+                let suffixUrl = "?contactId="+contactNodeId+"&accountId="+accountValue+"&searchContactKeyWord=" + searchContactKeyWord;
                 let newSearchContactParam = 'height=600,width=800,left=100,top=100,dialogHide=true,resizable=no,scrollbars=yes,toolbar=no,status=no';
                 newSearchContactWindow = window.open(baseUrl+suffixUrl, 'Popup', newSearchContactParam);
                 if (window.focus) {
@@ -456,10 +477,10 @@
         <div class="ptBody">
             <div class="content">
                 <img src="/img/s.gif" alt="{! SobjectLabel}" class="pageTitleIcon" title="{! SobjectLabel}" />
-                <h1 class="pageType">{! SobjectLabel}<apex:outputText rendered="{!isNewMode}">{!$Label.New}</apex:outputText><apex:outputText rendered="{!not(isNewMode)}">{!$Label.Edit}</apex:outputText>
+                <h1 class="pageType">{! SobjectLabel}<apex:outputText rendered="{!isNewMode || !isCloneMode}">{!$Label.New}</apex:outputText><apex:outputText rendered="{!not(isNewMode || isCloneMode)}">{!$Label.Edit}</apex:outputText>
                     <span class="titleSeparatingColon">:</span>
                 </h1>
-                <h2 class="pageDescription"> <apex:outputText rendered="{!isNewMode}">{!$Label.New}</apex:outputText><apex:outputText rendered="{!not(isNewMode)}">{!$Label.Edit}</apex:outputText>{! SobjectLabel}</h2>
+                <h2 class="pageDescription"> <apex:outputText rendered="{!isNewMode || isCloneMode}">{!$Label.New}</apex:outputText><apex:outputText rendered="{!not(isNewMode || isCloneMode)}">{!$Label.Edit}</apex:outputText>{! SobjectLabel}</h2>
                 <div class="blank">&nbsp;</div>
             </div>
             <div class="links">
@@ -483,7 +504,7 @@
                                 <img src="/img/s.gif" alt="" class="minWidth" title="" width="1" height="1" />
                                 <h2 class="mainTitle">{! SobjectLabel}<apex:outputText rendered="{!isNewMode}">{!$Label.New}</apex:outputText><apex:outputText rendered="{!not(isNewMode)}">{!$Label.Edit}</apex:outputText></h2>
                             </td>
-                            <td class="pbButton" id="topButtonRow">
+                            <td class="pbButton" id="topButtonRow"  style="pointer-events: none; opacity: 0.4;">
                                 <input class="btn" type="Button" value="{!$Label.Save}" onclick="saveSobjectProcess()" />
                                 <input class="btn" type="Button" value="{!$Label.SaveAndNew}" onclick="saveSobjectProcess(1)" />
                                 <apex:commandButton action="{!cancel}" value="{!$Label.Cancel}" />
@@ -505,8 +526,14 @@
                     
                     <!--Each section has layoutFields, let's iterate them as well-->
                     <apex:repeat value="{!layoutSection.layoutFields}" var="layoutField">
-                        <apex:inputField styleClass="{!ApiPrefix} {!ApiPrefix}_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Opportunity[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)}"
-                                         required="{!layoutField.isRequired}" />
+                        <!-- <apex:inputField styleClass="{!ApiPrefix} {!ApiPrefix}_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Opportunity[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)}"
+                                         required="{!layoutField.isRequired}" /> -->
+                        <apex:inputField styleClass="PIBackApi PIBackApi_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Opportunity[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)&&isNewMode}"
+                            required="{!layoutField.isRequired}" />
+                        <apex:inputField styleClass="PIBackApi PIBackApi_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Opportunity[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)&&not(isNewMode)&&layoutField.fieldAPI!='RecordTypeId'}"
+                            required="{!layoutField.isRequired}" />
+                        <apex:outputField styleClass="PIBackApi PIBackApi_{!layoutField.fieldAPI}" html-data-id="{!layoutField.fieldAPI}" value="{!Opportunity[layoutField.fieldAPI]}" rendered="{!not(layoutField.isPlaceHOlder)&&not(isNewMode)&&layoutField.fieldAPI=='RecordTypeId'}"
+                        />
                         <apex:pageblocksectionitem rendered="{!layoutField.isPlaceHolder}">
                         </apex:pageblocksectionitem>
                     </apex:repeat>
@@ -523,6 +550,30 @@
             }
             console.log(api_id_map);
             sfdcPage.appendToOnloadQueue(function () {
+                jQuery('option').each(function(i,e){
+                	if(e.value == '_\x01_'){
+                		e.value = '';
+                	}
+                })
+                var layoutSections = JSON.parse('{!layoutSectionsStr}');
+                for (let m = 0; m < layoutSections.length; m++) {
+                    let layoutSection = layoutSections[m].layoutFields;
+                    for (let n = 0; n < layoutSection.length; n++) {
+                        let layoutField = layoutSection[n];
+                        if (layoutField.fieldAPI != '' && document.querySelector("[data-id='"+layoutField.fieldAPI+"']") != null) {
+                            let e = document.querySelector("[data-id='"+layoutField.fieldAPI+"']");
+                            e.disabled = !(layoutField.editableField);
+                            if (!(layoutField.editableField)) {
+                                if (e.getAttribute("multiple") && e.getAttribute("multiple") == 'multiple') {
+                                    e.parentNode.classList.add("disabledbutton");
+                                }
+                                if (e.tagName == 'DIV') {
+                                    e.classList.add("disabledbutton");
+                                }
+                            }
+                        }
+                    }
+                }
                 //1. Set Last Name label
                 //document.querySelector("[data-id='LastName']").parentNode.parentNode.parentNode.children[2].children[0].innerText = '濮撳悕';
                 //2. Query AWS Data by dataId 
@@ -534,8 +585,11 @@
                 //Replace Vlookup Field
                 replaceSearchContactLookup();
                 //3. Set Readonly Attribute
-                document.querySelectorAll("[data-id='OwnerId']")[0].classList.add("disabledbutton");
-                document.querySelectorAll("[data-id='OwnerId']")[1].classList.add("disabledbutton");
+                // document.querySelectorAll("[data-id='OwnerId']")[0].classList.add("disabledbutton");
+                // document.querySelectorAll("[data-id='OwnerId']")[1].classList.add("disabledbutton");
+
+                document.getElementById('topButtonRow').style = '';
+                document.getElementById('bottomButtonRow').style = '';
             });
             </script>
             <div class="pbBottomButtons">
@@ -544,7 +598,7 @@
                         <tr>
                             <td class="pbTitle">
                                 <img src="/img/s.gif" alt="" class="minWidth" title="" width="1" height="1" />&nbsp;</td>
-                            <td class="pbButtonb" id="bottomButtonRow">
+                            <td class="pbButtonb" id="bottomButtonRow"  style="pointer-events: none; opacity: 0.4;">
                                 <input class="btn" type="Button" value="{!$Label.Save}" onclick="saveSobjectProcess()" />
                                 <input class="btn" type="Button" value="{!$Label.SaveAndNew}" onclick="saveSobjectProcess()" />
                                 <apex:commandButton action="{!cancel}" value="{!$Label.Cancel}" />

--
Gitblit v1.9.1