+ Start a Discussion
Jim Parker 7Jim Parker 7 

Sobject Coding Issues

Having a coding issue that I can't figure out.... New to this and feel like I am missing something.

Creating a trigger on a joiner object (Horizon_Account_Cases__c) between cases and a custom object (Horizon_Account__c) which has only two fields (Horizon_Acct__c and Case__c) which are unique keys for the ID's on the two related object cases.  Upon triggering on Horizon_Account_Cases__c, the related case (eg Case__C) should have its field Related_Horizon_Account__c updated with the value in the joiner object that triggered (Horizon_Acct__c).  Can't get it to compile and need help!  I am sure I am missing something very obvious...

public class horizonCaseLinkUtil {
    
    public static void addHorizonToCase (List<sObject> linkIds) {
        try {
        
        List<Case> casesToUpdate = [select Id 
                                            from Case caseUsed
                                            where Id in :linkIds.Case__c 
                                                         limit 1];                                                         

        List<Horizon_Account__c> horizonAccounts = [select     Id
                                            from Horizon_Account__c hzActCase
                                            where Id in :linkIds.Horizon_Acct__c
                                                         limit 1];                                                         
                                                         
        for (Case cases : casesToUpdate) 
        {
            for (Horizon_Account__c horizon : horizonAccounts)
            {
                cases.Related_Horizon_Account__c = horizon.Id;
            }
            
        }
        update casesToUpdate;
        }
        catch (Exception e) {
            System.debut('Issue with the Horizon Case Update: ' +e.getMessage() );
        }
    }
    
}


trigger HorizonAcctCaseLinked on Horizon_Account_Cases__c (after insert, after update) {
            List<sObject> linkedHorizonCase = new List<sObject>();
            for (Horizon_Account_Cases__c hzActCase : Trigger.new)
                linkedHorizonCase.add(hzActCase);
            if (linkedHorizonCase.isEmpty() == false)
            {
                horizonCaseLinkUtil.addHorizonToCase(linkedHorizonCase);
            }   
}


@isTest(SeeAllData=true)
public class HorizonCaseLinkTest {
        static testMethod void caseLinkTestMethod() {
        List<sObject> sourceList = [SELECT Id 
        FROM Horizon_Account_Cases__c LIMIT 1];
        Case c = new Case(Subject = 'Test Case');
        Horizon_Account__c horizonAcct = new Horizon_Account__c (Name='Test Horizon Acct');
        if(sourceList.size() == 0) {
            sourceList.add(
                    new Horizon_Account_Cases__c(Case__c =c.Id, Horizon_Acct__c=horizonAcct.Name)
            );
        }
        horizonCaseLinkUtil.addHorizonToCase( sourceList );
        }
}

 
Best Answer chosen by Jim Parker 7
UC InnovationUC Innovation
Hi Jim, 

I was a bit confused by your explaination of the schema but let me know if I have the right idea here.

The trigger is on the object (Horizon_Account_Cases__c) after update and insert. You want to update the field (Related_Horizon_Account__c) on the related case to be the Id of the horizon account related to the triggered horizon account case record.

If this is the case then I went ahead and rewrote the code you provided. Adjusting a bit to include best practices and whatnot. Let me know if this works for you.
 
public class horizonCaseLinkUtil {
    public static void addHorizonToCase (List<Horizon_Account_Cases__c> linkedHorizonCases) {
        try {
			List<Case> casesToUpdateList = new List<Case>();
			Map<Id, Case> relatedCasesMap = new Map<Id, Case>([SELECT Related_Horizon_Account__c 
															   FROM Case
															   WHERE Id IN :linkedHorizonCases.Case__c]);                                                                                                              
															 
			for (Horizon_Account_Cases__c linkedHorizonCase : linkedHorizonCases) {
				Case caseToUpdate = relatedCasesMap.get(linkedHorizonCase.Case__c);
				caseToUpdate.Related_Horizon_Account__c = linkedHorizonCase.Horizon_Acct__c;
				casesToUpdateList.add(caseToUpdate);
			}
			
			update casesToUpdateList;
        } catch (Exception e) {
            System.debug('Issue with the Horizon Case Update: ' + e.getMessage());
        }
    }
    
}

trigger HorizonAcctCaseLinked on Horizon_Account_Cases__c (after insert, after update) {
	List<Horizon_Account_Cases__c> linkedHorizonCases = new List<Horizon_Account_Cases__c>();
	
	for (Horizon_Account_Cases__c hzActCase : Trigger.new) {
		linkedHorizonCases.add(hzActCase);  
	}

	horizonCaseLinkUtil.addHorizonToCase(linkedHorizonCases);
}

Hope this helps!

AM

All Answers

UC InnovationUC Innovation
Hi Jim, 

I was a bit confused by your explaination of the schema but let me know if I have the right idea here.

The trigger is on the object (Horizon_Account_Cases__c) after update and insert. You want to update the field (Related_Horizon_Account__c) on the related case to be the Id of the horizon account related to the triggered horizon account case record.

If this is the case then I went ahead and rewrote the code you provided. Adjusting a bit to include best practices and whatnot. Let me know if this works for you.
 
public class horizonCaseLinkUtil {
    public static void addHorizonToCase (List<Horizon_Account_Cases__c> linkedHorizonCases) {
        try {
			List<Case> casesToUpdateList = new List<Case>();
			Map<Id, Case> relatedCasesMap = new Map<Id, Case>([SELECT Related_Horizon_Account__c 
															   FROM Case
															   WHERE Id IN :linkedHorizonCases.Case__c]);                                                                                                              
															 
			for (Horizon_Account_Cases__c linkedHorizonCase : linkedHorizonCases) {
				Case caseToUpdate = relatedCasesMap.get(linkedHorizonCase.Case__c);
				caseToUpdate.Related_Horizon_Account__c = linkedHorizonCase.Horizon_Acct__c;
				casesToUpdateList.add(caseToUpdate);
			}
			
			update casesToUpdateList;
        } catch (Exception e) {
            System.debug('Issue with the Horizon Case Update: ' + e.getMessage());
        }
    }
    
}

trigger HorizonAcctCaseLinked on Horizon_Account_Cases__c (after insert, after update) {
	List<Horizon_Account_Cases__c> linkedHorizonCases = new List<Horizon_Account_Cases__c>();
	
	for (Horizon_Account_Cases__c hzActCase : Trigger.new) {
		linkedHorizonCases.add(hzActCase);  
	}

	horizonCaseLinkUtil.addHorizonToCase(linkedHorizonCases);
}

Hope this helps!

AM
This was selected as the best answer
rajat Maheshwari 6rajat Maheshwari 6

Hi Jim,

Below is the code, which will definately help you :)

trigger HorizonAcctCaseLinked on Horizon_Account_Cases__c (after insert, after update,before insert, before update) {
           
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
  {
      
     
       horizonCaseLinkUtil.addHorizonToCase(Trigger.new);
          
  } 

 }



public class horizonCaseLinkUtil {

    Set<Id> st_ParentId = new Set<Id>();
    public void addHorizonToCase (List<Horizon_Account_Cases__c> linkedHorizonCases) {
        try {
                 for(Horizon_Account_Cases__c Horizon_Account_Recd : linkedHorizonCases)
                     {
                       st_ParentId.add(Horizon_Account_Recd.Case__c);
                     }

               
			// List<Case> casesToUpdateList = new List<Case>();

            if(st_ParentId!=null && !st_ParentId.isEmpty())
               {
                	Map<Id, Case> relatedCasesMap = new Map<Id, Case>([SELECT Id, Related_Horizon_Account__c 
															   FROM Case
															   WHERE Id IN :st_ParentId]);                                                                                                              
															 
			for (Horizon_Account_Cases__c Horizon_Account_Recd : linkedHorizonCases) 
            {
               if(relatedCasesMap!=null && relatedCasesMap.containsKey(Horizon_Account_Recd.Case__c))
                  {
                     
				relatedCasesMap.get(Horizon_Account_Recd.Case__c).Related_Horizon_Account__c = Horizon_Account_Recd.Horizon_Acct__c;
				
		        	}
             }
           }

			if(relatedCasesMap!=null && relatedCasesMap.values()!=null)
			   update relatedCasesMap.values();
        } catch (Exception e) {
            System.debug('Issue with the Horizon Case Update: ' + e.getMessage());
        }
    }
    

 

Please Mark as best answer, If it works :)

Thanks
Rajat Maheshwari