• 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 am using salesforce to Salesforce to sync the case obj. for two diff. orgs. As we cannot map the Record Type while mapping the fields from one org to another.

Here's the workaround I used- 1.Have the sending organization create text field that is automatically updated with the Record Type name via a Work Flow Rule 2.Have the receiving organization create a text field to receive the incoming value and then setup a Work Flow Rule to update the Record Type field based off of this value. 3.The final step would be mapping the two newly created Text Fields together.

Issue is- Record type is not updating directly to required record type in the target org. Its mapping to some other RT(rec. type) and then changing it to the req. one in workflow rule.

Hence its sending email notification attached to that "other" record type it mapping initially.

I have checked the default & assigned RT for the profile, its seems fine.

Why record type is not mapping directly to the one I am telling via WFR or Trigger?
I have the following trigger that automate the case obj.  sync from one Org to another via salesforce to salesforce connection.
trigger OrgSync on Case (after insert, after update) {

PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted'];
                   
        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();
               
        for (Case cc : Trigger.new){
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

           newrecord.ConnectionId = conn.Id;
           newrecord.LocalRecordId = cc.id;  
            newrecord.SendClosedTasks = true;
            newrecord.SendOpenTasks = true;
            newrecord.SendEmails = true;
            recordConnectionToInsert.add(newrecord);
        }
        if (recordConnectionToInsert.size() > 0){
            System.debug('>>> Sharing ' + recordConnectionToInsert.size() + ' records');
            insert recordConnectionToInsert;
        }


}

I am getting the following error when try to save a case record in the sorce org.
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger trCase caused an unexpected exception, contact your administrator: trCase: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Class.trCaseHandler.AddEmailSenderToCaseTeam: line 203, column 1

Upon inspection I see that its conflicting with handler class of  another trigger. Here is the code snippet of the methoid where the conflict is happening in the handler class. Am I missing something ?
public void AddEmailSenderToCaseTeam( case[] newCases )
    {
        CaseTeamMember[] team = new CaseTeamMember[]{};
        
        CaseTeamRole createRole = [SELECT Id FROM CaseTeamRole WHERE Name = 'Created By' LIMIT 1];
        
        MAP <String, Id> emailParentMap = new MAP <String, Id>{};
        for( case c: newCases )
        {
            emailParentMap.put( c.SuppliedEmail, null );
        }
        
        //check if case supplied email is in users
        User[] emailUsers = [select Id, Email from User where Email =: emailParentMap.keySet() order by Email, LastLoginDate desc];
        for( User u: emailUsers )
        {
            Id x = emailParentMap.get( u.Email );
            if( x == null )
                emailParentMap.put( u.Email, u.Id );
        }
        
        for( case c: newCases )
        {
            Id x = emailParentMap.get( c.SuppliedEmail );
            if( x != null )
            {
                team.add(new CaseTeamMember(ParentId = c.Id, MemberId = x,TeamRoleId = createRole.Id) );
            }
            else
            {
                String msg = 'Unable to add email-to-case sender ' + c.SuppliedEmail + ' to case team. No matching user found.';
                if(m_isExecuting)
                    c.addError( msg );
                ErrorMsgs.add(msg + ' [Case ID ' + c.ID + ']');
            }
        }
        insert team;
	}





 
I tried using process builder to update the account lookup field (case obj.)to null when some criteria is met, however it failed to do so. I think ID type field are not properly handled by process builder.
 
Then I wrote a trigger to do the same thing,  but its failing too. Can you guys tell me if I am missing something ?
Here’s the trigger-
trigger CopyAccountTrigger on Case (before  insert, before update) {
   Map mapAcc = new Map();
   Set acNameSet = new Set();
  
   for(Case cc: Trigger.new){
      acNameSet.add(cc.copAccount__c);
     
   }
   For(Account acc :[Select Id,Name from Account where Name  in:acNameSet]){
        mapAcc.put(acc.Name,acc.Id);
    }
   
    for(Case cc:Trigger.new){
        if(!mapAcc.isempty()){
         cc.AccountId = mapAcc.get(cc.copAccount__c);
       }
      
       else if (cc.account.name != cc.copAccount__c){
         
          cc.accountid = null;
           
        }
   }
  }
Here, copAccount__c is custom text fields in case object  which holds the account name coming from S2S connection..after field mapping
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 write a before insert/update trigger to copy the opportunity name(opportunity__c), a lookup field in the case object to another custom text field(CopyOppty__c) in the same object(case).  i wrote the below trigger, but not working.
 
trigger mapOppty on Case (before insert, before update) {
 
    Set<ID> oppIDs = Trigger.newMap.keySet();
    
    Case [] cc = [SELECT Id, Opportunity__c, Opportunity__r.Name FROM Case WHERE Opportunity__c IN :oppIDs ] ;           

    for(case ca : cc)
    {
        ca.copyOppty__c = ca.Opportunity__r.Name ;
    }

    update cc ;
}

 
How to write the test class for the trigger to automate salesforce to salesforce connection via apex-

here's the trigger
 
trigger OrgCaseSync on Case (after insert, after update) {

PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted'];
        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();
        for (Case cc : Trigger.new){
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

            newrecord.ConnectionId = conn.Id;
            newrecord.LocalRecordId = cc.id;  
            newrecord.SendClosedTasks = false;
            newrecord.SendOpenTasks = false;
            newrecord.SendEmails = false;
            recordConnectionToInsert.add(newrecord);
        }
        if (recordConnectionToInsert.size() > 0){
            System.debug('>>> Sharing ' + recordConnectionToInsert.size() + ' records');
            insert recordConnectionToInsert;
        }

 
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; 
              
            }
       }
   }
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; 
              
            }
       }
   }