批量更新ID,没有重复的电话和帐户
我正在根据要求创建一个批次,其中如果联系人根据帐户和手机有重复项,则名为 Eksternal ID 的字段不应设置该值。 谁能指导我满足这个要求? 预先感谢
这是我现在的代码,但它仍然不正确,导致具有重复联系人的 eksternal ID 仍然可以设置该值。
public class UT_MDM_batchEksternalIDContacts implements Database.Batchable <sObject>,Database.Stateful {
final Integer MAX_TEXT_LENGTH = 255;
public UT_MDM_batchEksternalIDContacts(){
}
public Database.QueryLocator start(Database.BatchableContext info){
String SOQL = '';
if(Test.isRunningTest()){
SOQL = 'SELECT Id,Eksternal_ID__c,AccountId, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE MobilePhone != null AND LastName != null AND Account.SAP_Account_Id__c != null AND Title != null AND Is_Drop__c = FALSE';
}else{
SOQL = 'SELECT Id,Eksternal_ID__c,AccountId, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE Eksternal_ID__c = null AND MobilePhone != null AND LastName != null AND Account.SAP_Account_Id__c != null AND Title != null AND Is_Drop__c = FALSE LIMIT 100';
}
return Database.getQueryLocator(SOQL);
}
public void execute(Database.BatchableContext info, List<Contact> listContact){
Set<Id> S_ContactIds = new Set<Id>();
List <String> S_Lastname = new List<String>();
List <String> S_Phone = new List<String>();
List <String> S_Account = new List<String>();
List <String> S_Title = new List<String>();
Integer tempUpdate;
for(Contact cont : listContact){
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);
}
}
List<Contact> contractorList = [SELECT Id, MobilePhone,AccountId FROM Contact WHERE MobilePhone =:S_Phone AND AccountId =:S_Account AND ID NOT IN :S_ContactIds]; //duplicate
List<Id> contIds = new List<Id>();
if(S_ContactIds.size() > 0){
if(contractorList.size() > 0 ){
for(Contact contactVal : listContact){
for(Contact contList: contractorList){
if(contactVal.MobilePhone == contList.MobilePhone && contactVal.AccountId == contList.AccountId){//error
}
else{
contIds.add(contList.Id);
}
}
}
}
List<Contact> finalList = new List<Contact> ([SELECT Id, Eksternal_ID__c, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE Id IN :contIds]); //
if (contIds.size() > 0){
updateContact(finalList);
}
else if (contractorList.size() == 0 ){
updateContact(listContact);
}
}
}
public void updateContact(List<Contact> listContact) {
List<Contact> lstContact = new List<Contact>();
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);
lstContact.add(cont);
}
}
}
update lstContact;
}
public void finish(Database.BatchableContext info){
}
}
I am creating a batch for the requirement in which if contact has duplicates based on account and mobile phone than field named Eksternal ID should not set the value.
Can anyone guide me with this requirement?
Thanks in advance
This is my code now, but it still not right, cause eksternal ID with duplicate contact still can set the value.
public class UT_MDM_batchEksternalIDContacts implements Database.Batchable <sObject>,Database.Stateful {
final Integer MAX_TEXT_LENGTH = 255;
public UT_MDM_batchEksternalIDContacts(){
}
public Database.QueryLocator start(Database.BatchableContext info){
String SOQL = '';
if(Test.isRunningTest()){
SOQL = 'SELECT Id,Eksternal_ID__c,AccountId, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE MobilePhone != null AND LastName != null AND Account.SAP_Account_Id__c != null AND Title != null AND Is_Drop__c = FALSE';
}else{
SOQL = 'SELECT Id,Eksternal_ID__c,AccountId, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE Eksternal_ID__c = null AND MobilePhone != null AND LastName != null AND Account.SAP_Account_Id__c != null AND Title != null AND Is_Drop__c = FALSE LIMIT 100';
}
return Database.getQueryLocator(SOQL);
}
public void execute(Database.BatchableContext info, List<Contact> listContact){
Set<Id> S_ContactIds = new Set<Id>();
List <String> S_Lastname = new List<String>();
List <String> S_Phone = new List<String>();
List <String> S_Account = new List<String>();
List <String> S_Title = new List<String>();
Integer tempUpdate;
for(Contact cont : listContact){
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);
}
}
List<Contact> contractorList = [SELECT Id, MobilePhone,AccountId FROM Contact WHERE MobilePhone =:S_Phone AND AccountId =:S_Account AND ID NOT IN :S_ContactIds]; //duplicate
List<Id> contIds = new List<Id>();
if(S_ContactIds.size() > 0){
if(contractorList.size() > 0 ){
for(Contact contactVal : listContact){
for(Contact contList: contractorList){
if(contactVal.MobilePhone == contList.MobilePhone && contactVal.AccountId == contList.AccountId){//error
}
else{
contIds.add(contList.Id);
}
}
}
}
List<Contact> finalList = new List<Contact> ([SELECT Id, Eksternal_ID__c, Account.SAP_Account_Id__c, LastName, Title, MobilePhone FROM Contact WHERE Id IN :contIds]); //
if (contIds.size() > 0){
updateContact(finalList);
}
else if (contractorList.size() == 0 ){
updateContact(listContact);
}
}
}
public void updateContact(List<Contact> listContact) {
List<Contact> lstContact = new List<Contact>();
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);
lstContact.add(cont);
}
}
}
update lstContact;
}
public void finish(Database.BatchableContext info){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论