You need to sign in to do that
Don't have an account?
Jim 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 );
}
}
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 );
}
}
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.
Hope this helps!
AM
All Answers
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.
Hope this helps!
AM
Hi Jim,
Below is the code, which will definately help you :)
Please Mark as best answer, If it works :)
Thanks
Rajat Maheshwari