'Field Not Updated in Apex Class - Trigger
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 ');
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
