function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SaltinSaltin 

Solution Custom Fields referenced in Case-centric Email template

Hi,

 

Looking through the support data model, you can see that cases and solutions are linked via a junction object, CaseSolution.

 

I've got a custom field on the solution object (lets call it solution.custom__c).

 

For Cases, we often want to send an email template which includes information about any solution attached to the case. I've noticed that when the email template is related to object 'Case' only Solution Description and Solution Details are available to me through the email template field drop downs and none of our custom objects are available. I assume this is due to the junction object.

 

So, my question, is there any reliable way for me to delve a little deeper into the fields of any given solution? Basically I want to build an email template related to cases which can reference custom fields of any solution which is attached to the case.

 

Any help would be greatly appreciated!

 

Message Edited by Saltin on 02-23-2010 09:03 AM
werewolfwerewolf
Not with merge fields, because you can link many solutions to a case, so how would a given merge field know which solution you want?  You could do it with a Visualforce email template though.
SaltinSaltin
Thanks werewolf. I had considered a VF template, but I've only ever seen examples that iterate through related list items. I need to go from Case, through CaseSolution Junction object, and down to Solution, and then pull the contents of a field. There are no related lists in this case. Do you have an example of how something like that might be achieved? Ideally it would do this for any Solution attached to the Case.
Message Edited by Saltin on 02-23-2010 02:02 PM
werewolfwerewolf
No, I do not have an example of that.  There is a related list here, it's CaseSolution, and you can query both Case and Solution fields from that.
DevNVDevNV

I ran into the same kind of requirement - here is my code that you can modify to suit for custom fields.

 

Controller code to search for Solution details based on a CaseId passed in (with test method at the end):

 

 

// Controller used by CaseSolutions component to find all related Solutions for a single Case
public class findCaseSolutions {
    // resulting list of Case Solutions
    public List<CaseSolution> solutions {get; set;}
    // Id value of case sent into controller
    public ID CaseId {get; 
        set{
            CaseId = value;
            // if there is a value passed in, query the CaseSolution table for solution details linked to the Case
            if( CaseId != null ) {
                solutions = [select solution.SolutionNumber, solution.SolutionNote, solution.SolutionName from CaseSolution where caseId = :caseId];
            }
            // if there is nothing set in, return an empty list
            else {
                solutions = new List<CaseSolution>();
            }    
        }
    }
    
    // constructor for controller
    public findCaseSolutions() {
    }

    // test method
    static testmethod void testController(){
        // create a Case and 2 Solutions
        Case c = new Case(subject='testing');
        insert c;
        Solution s1 = new Solution(solutionname = 'solution 1', solutionnote = 'solution text');
        insert s1;
        Solution s2 = new Solution(solutionname = 'solution 2', solutionnote = 'solution2 text');
        insert s2;
        // first call controller without linking case and solution
        findCaseSolutions find = new findCaseSolutions();
        find.caseId = c.id;
        system.assertequals(find.solutions.size(), 0);
        
        // now link solutions to case
        CaseSolution cs1 = new CaseSolution(caseId = c.id, solutionid = s1.id);
        insert cs1;
        CaseSolution cs2 = new CaseSolution(caseId = c.id, solutionid = s2.id);
        insert cs2; 
        find = new findCaseSolutions();
        find.caseId = c.id;
        system.assertequals(find.solutions.size(), 2); 
        
        // call it without sending a Case id in
        find = new findCaseSolutions();
        find.caseid = null;
        system.assertequals(find.solutions.size(), 0);     
    }
}

 

Then I build this component to display the Solution details returned.  My template is in  plain text so I didn't use a dataTable component or anything fancy that doesn't work in plain text.

 

 

<apex:component controller="findCaseSolutions" access="global">

    <apex:attribute name="CaseId" description="Case Id for which we are finding Case Solution records" type="String" required="required" assignTo="{!CaseId}"/>
    <!-- using a simple repeat component so this can be inserted into a plain text email template -->       
    <apex:repeat value="{!solutions}" var="s">
        {!s.solution.SolutionNote}
    </apex:repeat>

</apex:component>

 And finally I included this component into my visualforce Email Template:

 

 

<messaging:plainTextEmailBody >
Dear {!recipient.Name},

Here is your solution detail: <c:CaseSolutions CaseId="{!relatedTo.Id}"/>
</messaging:plainTextEmailBody>

 

 

The only downside of this is you can't include the Solution attachments.  We can query for them in the controller but I don't see a way to attach them into the email template.  I added an Idea for that - vote if you think it would be useful.

 

You can add any custom fields you need into the controller query.

 

Hope that helps.

 

Niki

www.vankerksolutions.com