Skip to main content
Brig Larimer が「#Apex」で質問
I have a trigger that operates on solutions that are inserted via solution creation specifically from the case close layout. This trigger operates perfectly in practice:

trigger tUpdateSolutionProduct on Solution (after insert)

{

    Set<Id> caseIds = new Set<Id>();

    Set<Id> solutionIds = new Set<Id>();

    List<Solution> solutionList = new List<Solution>();

    List<Case> caseList = new List<Case>();

    Map<Id,Solution> solutionMap = new Map<Id,Solution>([SELECT Id, (SELECT Id, CaseId FROM CaseSolutions) FROM Solution WHERE Id IN: trigger.newMap.keySet()]);

    

    for (solution s : trigger.new)

    {

        if (solutionMap.get(s.Id).CaseSolutions.size() > 0) //verifies that this solution is being created as a solution to a particular case via close case layout

        {

            solutionIds.add(s.Id);

            caseIds.add(solutionMap.get(s.Id).CaseSolutions[0].CaseId);

        }

    }

    

    Map<Id,Case> caseMap = new Map<Id,Case>([SELECT Id, Area__c FROM Case WHERE Id IN: caseIds]);

    solutionList = [SELECT Id, Product__c FROM Solution WHERE Id IN: solutionIds];

    

    for (solution s : solutionList)

        s.Product__c = caseMap.get(solutionMap.get(s.Id).CaseSolutions[0].CaseId).Area__c;

    

    update solutionList;

}

My issue is trying to create a test class. Namely I do not know how to simulate this use case in code. Here's what I have so far:

@isTest

public class testgp_tUpdateSolutionProduct

{

    public static testmethod void testAll()

    {

        account a = new account();

        a.Name = 'Vandelay Industries';

        insert a;

        

        case c = new case();

        c.AccountId = a.Id;

        c.Area__c = 'Machine';

        insert c;

        

        solution s = new solution();

        s.SolutionName = 'Test Solution';

        insert s; //trigger does not fire here because casesolution does not yet exist

        

        casesolution cs = new casesolution();

        cs.CaseId = c.Id;

        cs.SolutionId = s.Id;

        insert cs; //this is being inserted too late, but can't be inserted earlier because SolutionId is a required field

    }

}

Note my two comments above explaining my dilemma: in order for the trigger to operate on the solution, a casesolution must already exist on insert. However, I cannot insert the casesolution first because SolutionId is a required field, which isn't populated until after the solution is inserted.

I am racking my brain trying to think of a way to do this. Essentially what needs to happen is similar to what must actually happen in the real use case: i.e. both distinct objects are being inserted roughly at the same time, at least to the point where by the time the after insert event occurs on the solution, the casesolution has already been saved to the database, which could only occur after the solution is initially saved to the database in order for the casesolution to have a SolutionId. As far as I know, I can only get as granular as using a DML insert and can't dig down any more finely than this to simulate this chain of events in code.

Any suggestions here??

Thanks!
2 件の回答
  1. 2016年2月9日 14:26
    What I do is start at a case. I click the close case button to be taken to the close case layout. Then in the solution information section of the close case layout I enter in some information to create a solution upon save. This is the targeted use case that the trigger handles well.

    By the way, if I need to re-write the trigger I can accept that.

    Thanks for your feedback.
0/9000