condtionally render the content in vf pag email template in salesforce - salesforce

Depending upon account type( personal, commercial) need to render welcome message in VF page with if condition.
relatedToType="Account"
for commercial account
{!relatedTo.Name},
for personal account
Dear {!relatedTo.FirstName},
how to render the given requirement with if condition in vf page.

Try
Dear {!IF(relatedTo.isPersonAccount, relatedTo.FirstName, relatedTo.Name)} for simple things. If you have bigger blocks of text - most VF tags have rendered attribute to turn them on and off.
That's a weird requirement though. "Dear Four Seasons Total Landscaping..." ;)
Who do you send it to when it's a company? Maybe use recipientType="Contact" and then recipient.FirstName?

Related

SalesForce Visualforce email template Apex Controller

I have an assignment.
I need to send to different contacts the participants associated with it using visualforce template in pdf format.
I know how send email to different recipients, but i dont know how to send with different text. When I do SOQL in my controller I have List of List (Contacts and Participants) but how send to different contacts with different text body i dont know
Visualforce email template can't have an apex controller like say a vf page. BUT it can include a vf component - and that component can have controller and accept parameters!
It's bit trickier than usual because you'd need to move some logic to getter but it's doable. Make a component, pass to it "related to.Id" or something and it'll work
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm
https://salesforce.stackexchange.com/q/245930/799
There's my ancient answer https://salesforce.stackexchange.com/q/4303/799 if you're still stuck, overcomplicated for what you need but hey, gives some ideas.
Remember that each such email + component + controller will count to limit of soqls. If it uses 1 query (gimme participants for this contact id) - that's max 100 emails in single transaction.

How do i add a related record to a lightning page layout

I'm trying to add a related record to a lightning page layout for BAN but don't have an update/new action to associate with it
My goal is based on the BAN entered into the Case to show the BAN page on the Case lightning page for a custom object
Carolyn
We don't know what BAN is (some custom object specific only to your Salesforce org?) and how they're related to each other ;)
You have a lookup field on Case that points to BAN and you want to show more details than just the name/link? Check if what I did in https://stackoverflow.com/a/61280895/313628 is any help. You edit the Lightning Page, find "record detail" component, drop it somewhere, configure a bit, job done?

How to show recently record list on my custom visualforce page?

The sObject's record list
Hey, I have a question, when I click a sObject's tab it will present a standard page. The picture shows the recently record list, is it a standard function like "apex:listViews" mark-up tag? I want to achieve this function in my own custom page. How do I do this?
I don't think there's a dedicated VF tag for it (aside from some very specialised cases like Knowledge and Ideas-related tags). But you can manually query it from database and display in <apex:pageBlockTable>.
There's a table with "recently viewed everything, across all objects" (think of it like "Recent Items" sidebar in clasic SF UI): https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_recentlyviewed.htm
And also most objects will have a LastViewedDate field so you should be able to do
SELECT Name
FROM Restaurant__c
WHERE LastViewedDate != null
ORDER BY LastViewedDate DESC

How can I set the approvers for all steps in an approval process?

I have an approval process with three steps, all of which are set to Assigned Approver = Manually Chosen. When the user submits the record for approval, I'd like to have Apex code determine who the three approvers are. However, I don't see a way to hook into the approve request submission.
If I submit the approval with Apex Using Approval.process(), I can set the initial (and only the initial) approver with ProcessSubmitRequest.setNextApproverIds(). This call leads you to believe you can specify multiple approvers since it takes an array of Ids, but the array can only have 1 element, or else runtime a error occurs.
Once I know what the first approver's response is, I can use Apex to submit her response and, again, set the immediately next approver by passing a ProcessWorkitemRequest instance to Approval.process(). An important note here is that the approver must not approve via the standard UI. Instead, they must do something that invokes the Apex code so that we can set who the next approve should be. A trigger on the object under review, or a custom button + VF page could be used to invoke the Apex.
My main question is, how can I make sure that the user does not use the standard approval buttons? They appear in the Approvals related list and on the salesforce home screen. It may be in other places as well. Again, if they use the standard submit and approve buttons, I don't have any way to hook in to set the next approver.
We ran into a similar issue a while back and solved it by creating custom lookup fields to certain users. For example, if we wanted to route an approval request up to a Director and then a VP, we added Director__c and MarketVP__c fields to the object. These fields were populated in code by climbing the role hierarchy whenever a request was submitted. Our approval process's steps then chose who the assignee would be based on the values in these fields (first step would be assigned to Related User: Director and the second step would be assigned to Related User: Market VP, etc.).
To get around the standard approval button issue (we had other reasons for hiding it), we just hid that from their homepage layouts and built our own VF page and included it in a custom homepage component. This component functioned as an inbox with links to any records that were pending the user's approval. All user interaction with the approval objects was handled through other VF pages with their own Approve and Reject buttons. I don't know if the objects you're submitting to the approval process even use VF pages, so this may not be feasible for your situation.
A lot of customization for something that shouldn't need it, I know. Might not be the answer you're looking for, but hopefully it's some food for thought.

Using apex:relatedList in a Visualforce page when the user profile denies access to child objects

I have two custom objects, Appointment and Session Report. Session Report is a child of Appointment in a standard Master-Detail relationship. Additionally, I have a profile, Student, which has Read-Create-Edit-Delete for Appointments and no rights for Session Reports. The use case is a Student can create an Appointment but cannot view the Session Reports created for this Appointment by a Tutor.
Using a standard layout for the Appointment object works as expected when viewing Appointments. Namely, the Student can see the Appointment fields and the related list of Session Reports is not displayed. All other user profiles observe can see the related list of Session Reports.
However, I have encountered a problem when replacing the standard layout with a Visualforce page as such:
<apex:page standardController="Appointment__c">
<apex:sectionHeader title="{!$ObjectType.Appointment__c.label}" subtitle="{!Appointment__c.Name}"/>
<apex:pageBlock title="{!$ObjectType.Appointment__c.label} Detail">
<apex:pageBlockSection showHeader="false" columns="1">
<apex:outputField value="{!Appointment__c.Tutor_Name__c}"/>
<apex:outputField value="{!Appointment__c.Student_Name__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:relatedList list="Session_Reports__r"/>
This new page works as expected for all users with at least Read rights for the Session Report object. The Student user has no rights to this object and receives this error message
'Session_Reports__r' is not a valid child relationship name for entity Appointment
Clearly this relationship does exist as the page can be displayed properly for users with different profiles. I have been unable to resolve the difference between the standard layout and the VF page that would result in this failure. It has been suggested to me that I could identify the user profile in the VF page and use that information to toggle rendering. However, this type of approach defeats the purpose of the Salesforce security model and I won't be adopting such a technique.
Should I be able to use apex:relatedList in this fashion? Or have I wrongly assumed that the VF rendering engine could figure out when it can and cannot display related lists?
Salesforce security model is only going to ensure you don't show data that is not accessible to a particular user. How it does this is by throwing that exception you see. If you're building a custom vf page your responsible for making sure you don't display something the user isn't allowed to see. Note this is different than for fields which just don't show up if the user doesn't have the proper field level security.
You'll need to add a check to verify a user can view that object. Fortunately they have a lot of "is" methods (isAccessible, isCreatable, isDeletable, etc.) on the Describe Object Result for you to determine what the current user's permissions are for that object without having to hard code profiles into your code. For your specific case you don't what to display that related list if it's object not accessible.
Visualforce Page:
<apex:relatedList list="Session_Reports__r"
rendered="{!$ObjectType.Session_Report__c.accessible}"/>

Resources