• Jaynandan Prasad 8
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 8
    Replies
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.
 
I need to auto-populate a lookup field(Account) on the case object with reference to the text value(having Account Name) on custom text field in same object. How to do this ? any trigger code may help..
Wrote a trigger to change the record type after a field getting populated in the same object. Its getting saved, but not working.

trigger RecordTypeMap on Case (before insert) {

Map<Id, String> typeMap = New Map<Id, String>();
for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Case']) {
     typeMap.put(rt.Id,rt.DeveloperName);
   }

 for (Case cc : trigger.new)  {

      
      if (cc.Master_Record_Type__c != null) { 
            
               id recid = typeMap.get(cc.Master_Record_Type__c);
               recordtype rectype = [select id, developername from recordtype where id=:recid];
               cc.RecordTypeid = rectype.id; 
              
            }
       }
   }