Apex 类中的字段未更新 - 触发器

发布于 2025-01-11 19:11:45 字数 5685 浏览 2 评论 0原文

我有 Apex 类,它将填充字段 Eksternal ID 的值,我没有收到错误,当我尝试更新具有不同帐户的相同手机的数据时,Eksternal ID 不会更改,即使在 System.debug( 'FinalList2 :' +finalList) 值是正确的(已更改)。但是,当我单击“保存”按钮时,Eksternal ID 没有任何变化。

如果我更新具有不同手机和帐户的数据,那就可以了,eksternal ID将完美改变。

我应该怎么办?或者我的错在哪里?谢谢大家,

这是 APex Manager

public class UT_MDM_EksternalKey_Manager {    

final Integer MAX_TEXT_LENGTH = 255;
Set<Id> S_ContactIds = new Set<Id>();
public List <String> S_Lastname = new List<String>(); 
public List <String> S_Phone = new List<String>();
public List <String> S_Account = new List<String>();
public List <String> S_Title = new List<String>();
public List <String> S_Email = new List<String>();

public void preProcessingForInsert(List<Contact> newContact){      
    for(Contact cont : newContact){
        if(cont.MobilePhone != null && cont.LastName != null && cont.AccountId != null && cont.Title != null ){
            S_ContactIds.add(cont.Id);
            S_Lastname.add(cont.LastName);
            S_Phone.add(cont.MobilePhone);
            S_Account.add(cont.AccountId);
            S_Title.add(cont.Title);
            S_Email.add(cont.Email);
        }else{
            cont.adderror('Please Check Your MobilePhone, LastName, Account and Title Data First!! ');
        }
    }
    
   if(S_ContactIds.size() > 0){
        UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
        ck.validateDuplicate(S_ContactIds,S_Phone,newContact);
    }

}


public void preProcessingForUpdate(List<Contact> newContact){
    for(Contact cont : newContact){
        if(cont.MobilePhone != null && cont.LastName != null && cont.AccountId != null && cont.Title != null ){
            S_ContactIds.add(cont.Id);
            S_Phone.add(cont.MobilePhone);
            S_Account.add(cont.AccountId);
        }else{
            cont.adderror('Please Check Your MobilePhone, LastName, Account and Title Data First!! ');
        }
    }
    if(S_ContactIds.size() > 0){
        UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
        ck.validateDuplicate(S_ContactIds,S_Phone,newContact);
    }
}


public void postProcessingForUpdate(List<Contact> newContact){
}

}

这是 UT_MDM_CreateEksternalkey 类

public class UT_MDM_CreateEksternalKey {
final Integer MAX_TEXT_LENGTH = 255;

public void validateDuplicate(Set<Id> S_ContactIds,List <String> S_Phone,List<Contact> newContact){
    List<Contact> contractorList = [SELECT Id,AccountId, MobilePhone FROM Contact WHERE MobilePhone =:S_Phone AND ID NOT IN :S_ContactIds]; //duplicate
    List<Id> contIds = new List<Id>();
    List<Contact> finalList = new List<Contact>();
    
    if(S_ContactIds.size() > 0){
       if(contractorList.size() > 0 ){
            for(Contact contactVal : newContact){ 
                for(Contact contList: contractorList){ 
                    if (contactVal.MobilePhone == contList.MobilePhone && contactVal.AccountId == contList.AccountId){
                        contactVal.adderror('Duplicate Contact: Already Exist in '+contactVal.AccountId );
                    }
                    else{
                        contIds.add(contactVal.Id);
                        Contact FinalContact = new Contact();
                        FinalContact.Id= contactVal.Id;
                        FinalContact.AccountId= contactVal.AccountId;
                        FinalContact.MobilePhone = contactVal.MobilePhone;
                        FinalContact.LastName = contactVal.LastName;
                        FinalContact.Title = contactVal.Title;
                        finalList.add(FinalContact);
                    }                        
                }            
            }     
        }
        //List<Contact> finalList = new List<Contact> ([SELECT Id, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE Id IN :contIds]); //
        if (contIds.size() > 0){
            System.debug('FinalList2 :' +finalList);
            UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
            ck.CreateContactEksternalID(finalList);
        }
        else if (contractorList.size() == 0 ){
            UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
            ck.CreateContactEksternalID(newContact);
        }
    }
}


public void CreateContactEksternalID(List<Contact> listContact){
    List<Id> accountIds = new List<ID>();
    for(Contact cont : listContact){
        accountIds.add(cont.AccountId);
    }
    Map<Id,Account> accountMap = new Map<Id,Account>([SELECT ID,SAP_Account_Id__c FROM ACCOUNT
                                                      WHERE ID IN:accountIds]);
    Account tempAccount = null;
    for(Contact cont : listContact){
        if(accountMap.containsKey(cont.AccountId)){
            String mobile = cont.MobilePhone.replaceAll('\\D','');
            tempAccount = accountMap.get(cont.AccountId);
            if(tempAccount.SAP_Account_Id__c != null){
               cont.Eksternal_ID__c =  cont.LastName + '_' + tempAccount.SAP_Account_Id__c + '_'+ cont.Title + '_'   + mobile  ;
                // Ensure that the search text does not exceed max length:
               cont.Eksternal_ID__c = cont.Eksternal_ID__c.left(MAX_TEXT_LENGTH);                 
            }
            else{
                cont.adderror('There is no SAP ID in Account selected ');
            }
        }
    }
}

}

I Have Apex class that will fill value of field Eksternal ID, there is no error i got, when i tried to update data that have same mobilephone with different account, the Eksternal ID will not be changed, even though,in System.debug('FinalList2 :' +finalList) the value is correct (Changed). But, when i klik button save there is no changes in Eksternal ID.

If i update data that have different mobile phone and account, it will be okay, the eksternal ID will changed perfectly.

what should i do? or where is my fault? thank you guys

Here is APex Manager

public class UT_MDM_EksternalKey_Manager {    

final Integer MAX_TEXT_LENGTH = 255;
Set<Id> S_ContactIds = new Set<Id>();
public List <String> S_Lastname = new List<String>(); 
public List <String> S_Phone = new List<String>();
public List <String> S_Account = new List<String>();
public List <String> S_Title = new List<String>();
public List <String> S_Email = new List<String>();

public void preProcessingForInsert(List<Contact> newContact){      
    for(Contact cont : newContact){
        if(cont.MobilePhone != null && cont.LastName != null && cont.AccountId != null && cont.Title != null ){
            S_ContactIds.add(cont.Id);
            S_Lastname.add(cont.LastName);
            S_Phone.add(cont.MobilePhone);
            S_Account.add(cont.AccountId);
            S_Title.add(cont.Title);
            S_Email.add(cont.Email);
        }else{
            cont.adderror('Please Check Your MobilePhone, LastName, Account and Title Data First!! ');
        }
    }
    
   if(S_ContactIds.size() > 0){
        UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
        ck.validateDuplicate(S_ContactIds,S_Phone,newContact);
    }

}


public void preProcessingForUpdate(List<Contact> newContact){
    for(Contact cont : newContact){
        if(cont.MobilePhone != null && cont.LastName != null && cont.AccountId != null && cont.Title != null ){
            S_ContactIds.add(cont.Id);
            S_Phone.add(cont.MobilePhone);
            S_Account.add(cont.AccountId);
        }else{
            cont.adderror('Please Check Your MobilePhone, LastName, Account and Title Data First!! ');
        }
    }
    if(S_ContactIds.size() > 0){
        UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
        ck.validateDuplicate(S_ContactIds,S_Phone,newContact);
    }
}


public void postProcessingForUpdate(List<Contact> newContact){
}

}

And Here is UT_MDM_CreateEksternalkey Class

public class UT_MDM_CreateEksternalKey {
final Integer MAX_TEXT_LENGTH = 255;

public void validateDuplicate(Set<Id> S_ContactIds,List <String> S_Phone,List<Contact> newContact){
    List<Contact> contractorList = [SELECT Id,AccountId, MobilePhone FROM Contact WHERE MobilePhone =:S_Phone AND ID NOT IN :S_ContactIds]; //duplicate
    List<Id> contIds = new List<Id>();
    List<Contact> finalList = new List<Contact>();
    
    if(S_ContactIds.size() > 0){
       if(contractorList.size() > 0 ){
            for(Contact contactVal : newContact){ 
                for(Contact contList: contractorList){ 
                    if (contactVal.MobilePhone == contList.MobilePhone && contactVal.AccountId == contList.AccountId){
                        contactVal.adderror('Duplicate Contact: Already Exist in '+contactVal.AccountId );
                    }
                    else{
                        contIds.add(contactVal.Id);
                        Contact FinalContact = new Contact();
                        FinalContact.Id= contactVal.Id;
                        FinalContact.AccountId= contactVal.AccountId;
                        FinalContact.MobilePhone = contactVal.MobilePhone;
                        FinalContact.LastName = contactVal.LastName;
                        FinalContact.Title = contactVal.Title;
                        finalList.add(FinalContact);
                    }                        
                }            
            }     
        }
        //List<Contact> finalList = new List<Contact> ([SELECT Id, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE Id IN :contIds]); //
        if (contIds.size() > 0){
            System.debug('FinalList2 :' +finalList);
            UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
            ck.CreateContactEksternalID(finalList);
        }
        else if (contractorList.size() == 0 ){
            UT_MDM_CreateEksternalKey ck = new UT_MDM_CreateEksternalKey();
            ck.CreateContactEksternalID(newContact);
        }
    }
}


public void CreateContactEksternalID(List<Contact> listContact){
    List<Id> accountIds = new List<ID>();
    for(Contact cont : listContact){
        accountIds.add(cont.AccountId);
    }
    Map<Id,Account> accountMap = new Map<Id,Account>([SELECT ID,SAP_Account_Id__c FROM ACCOUNT
                                                      WHERE ID IN:accountIds]);
    Account tempAccount = null;
    for(Contact cont : listContact){
        if(accountMap.containsKey(cont.AccountId)){
            String mobile = cont.MobilePhone.replaceAll('\\D','');
            tempAccount = accountMap.get(cont.AccountId);
            if(tempAccount.SAP_Account_Id__c != null){
               cont.Eksternal_ID__c =  cont.LastName + '_' + tempAccount.SAP_Account_Id__c + '_'+ cont.Title + '_'   + mobile  ;
                // Ensure that the search text does not exceed max length:
               cont.Eksternal_ID__c = cont.Eksternal_ID__c.left(MAX_TEXT_LENGTH);                 
            }
            else{
                cont.adderror('There is no SAP ID in Account selected ');
            }
        }
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文