2

Design problem - Implement an approval process on case, which sends email to approver showing some additional info, which should not be visible to case owner/agent (who clicked on "Submit for approval" button).

Possible solution - All that additional info will not be stored on Case (otherwise agent will also be able to see it). I am creating a child object to case, where all that additional info be stored. We can make sure agent does not have access to that child object (lets call it ChildObj).

In the email template used, we can include info about that child object. From what i understand, standard email templates dont allow cross object reference. We probably need to use visualforce email templates.

Questions -

1) Can anyone see/point out any issues with that design? 2) I am very new to salesforce. Can anyone give me any example of how i will access child record's field? I mean there won't be any lookup or anything to child object, right? How do i access a field on the child object? Any clue would be helpful

2

I think you need to use visualforce email template with controller.

Controller "

    ppublic class GetChild {
    private final List<Account> accounts;


    public GetChild() {
        accounts = [SELECT Name,(SELECT CreatedBy.Name FROM Notes) FROM Account];
    }

    public List<Account> getchildNotes() {

       Return accounts;
    }
}

Create a component and name it SmithNotes and access the child values something like this

<apex:component controller="GetChild" access="global">
    <apex:variable var="count" value="{!0}"/> //declare
        <apex:dataTable value="{!childNotes}" var="s_account">
            <apex:column >
                <apex:facet name="header">Notes Title</apex:facet>
                {!s_account.Notes[count].title}
            </apex:column>
        </apex:dataTable>
        <apex:variable var="count" value="{!count + 1}"/> //increment
   </apex:component>

In your visualforce email template include the component.

<messaging:emailTemplate subject="Embedding Apex Code" recipientType="Contact" relatedToType="Opportunity">
    <messaging:htmlEmailBody>
        <p>As you requested here are the notes</p>
        <c:SmithNotes/>
        <p>Hope this helps with the {!relatedToType}.</p>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
  • 1
    Prabhat, i am not sure i follow your example. In my scenario, email goes when an approval process is initiated on a case. my first question - will "relatedToType" be "Case"? Now i need to retrieve the custom child object specific to that case. So there must be somewhere where we pass the current case as a parameter. I can't seem to find how to do that in any example (how to pass the current case as parameter to the controller .... controller can then query and retrieve any ChildObj that has parent case equal to the parameter passed) – user21352 Oct 12 '15 at 5:35
  • 1
    In your example, as well as one given in VF developer guide, seems like the component is an independent entity. It doesn't depend (or need) "relatedTo". Thats the problem i am facing. "relatedTo" holds the current case number, and i need to retrieve the child object of that specific case – user21352 Oct 12 '15 at 5:43
  • 1
    Just like we use ApexPages.currentPage().getParameters().get('id') with VF pages, can we use something similar (emailMessage.relatedTo) in the controller? – user21352 Oct 12 '15 at 5:54
  • 1
    You need to pass id of case from email template to controller. related post is here - salesforce.stackexchange.com/questions/35268/…. and query based on id to get child records. – MIX DML Oct 12 '15 at 6:58
  • 1
    Use Apex:param or AssignTo to pass the id. – MIX DML Oct 12 '15 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.