function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jaynandan Prasad 8Jaynandan Prasad 8 

Trigger on case obj. auto updating Account lookup while synching Contact in S2S connection

I have S2S connection to sync the Case object
1. Trigger-'CopyContactTrigger' helps in synching the contact lookup field in the case object
2.Trigger - 'CopyAccountTrigger' helps in synching the Account lookup filed in the case object.

Here are the two triggers-
trigger CopyContactTrigger on Case (after insert) {
   Set<id> caseIdSet=new Set<Id>();
   Map<String, Id> mapCon = new Map<String, Id>();
   List<Case> caseListToupdate=new List<Case>();
   Set<String> conNameSet = new Set<String>();
   for(Case cc: Trigger.new){
      conNameSet.add(cc.copContact__c);
       caseIdSet.add(cc.id);
   }
   For(Contact con :[Select Id, Name from Contact where Name  in:conNameSet]){
        mapCon.put(con.Name,con.Id);
    }
    for(Case cc:[SELECT id,ContactId,copContact__c FROM Case WHERE id IN : caseIdSet]){
       // if(!mapCon.Isempty()){
         cc.ContactId = mapCon.get(cc.copContact__c); 
         caseListToupdate.add(cc);
       // }
   }
  try{
    update caseListToupdate;
  }catch(DMLException de ){
 }
}
 
trigger CopyAccountTrigger on Case (before insert, before update) {
   Set<id> caseIdSet=new Set<Id>();
   Map<String, Id> mapAcc = new Map<String, Id>();
   List<Case> caseListToupdate=new List<Case>();
   Set<String> acNameSet = new Set<String>();
   
   if(Trigger.isInsert){
   for(Case cc: Trigger.new){
      acNameSet .add(cc.copAccount__c);
       caseIdSet.add(cc.id);
   }
   For(Account acc :[Select Id,Name from Account where Name  in:acNameSet]){
        mapAcc.put(acc.Name,acc.Id);
    }
    for(Case cc:[SELECT id,AccountId,copAccount__c FROM Case WHERE id IN : caseIdSet]){
        //if(!mapAcc.Isempty())
        //{
         cc.AccountId = mapAcc.get(cc.copAccount__c); 
         caseListToupdate.add(cc);
        //}
    }
    
   if(trigger.isUpdate){
    for(Case cc:[SELECT id,AccountId,copAccount__c FROM Case WHERE id IN : caseIdSet]){
         if(cc.copAccount__c !=null){
         cc.AccountId = mapAcc.get(cc.copAccount__c); 
         caseListToupdate.add(cc);
        }
   }
   
   } 
   }
  try{
    update caseListToupdate;
  }catch(DMLException de ){
 }
}

Issue: While synching the contact lookup field in the case obj. to the Traget Org, it is auto-populating the Account lookup field also. And hence doing wrong mapping of the account lookup. The issue is that when a contact from Source Org exists in Target but the Account from Source org. does not exist, the  Account lookupfield(in Target org) gets populated with the  Account(in Target) that the Contact is associated with. The rule should be that if there is no exact match on Account  on Target then the fields should be blank.
 
Jaynandan Prasad 8Jaynandan Prasad 8
copContact__c , copAccount__c are custom text fields in target org.  which holds the Contact name and the account name coming from the Source Org respectively..after field mapping