2

We would like to have a New Account button on the search results page for accounts: enter image description here

We currently have a custom button (as you can see above) but it is opening the incorrect record type. What we want is to have the New Account Button when clicked open a specific Account Page Layout based on Profile. For Example, If the Profile is US User it opens the Account Page Layout for "Local Agency - US". If it the Profile is UK User it opens the Account Page Layout for "Local Agency - EU".

How would this be done?

1

If you try to create a button, you'll notice a $Profile variable. This variable exposes nearly all the fields on the Profile object in Visualforce and Custom Buttons. This lets you create a Custom, On-Click Javascript button, and use a if statement or two, to redirect the page accordingly.

if ({!$Profile.Name = "Local Agency"}) {
    window.location.href = "https://c.cs71.visual.force.com/apex/AccountEdit?RecordType=RecordType"; 
} else if ({!$Profile.Name} = "Non-Local Agency"}) {
    window.location.href = "https://c.cs71.visual.force.com/apex/AccountEdit?RecordType=SomeOtherRecordType"; 
}

For the redirect, you won't be able to direct users to a certain page layout without setting up record types for those layouts. Each record type would have its own layout, and when you link to a new record, you specify not the layout, but the record type, and the system uses the appropriate layout.

The example code above uses the URLFOR function to determine the url for the $Action.Account.New

As much as I wish I could use it, URLFOR does not skip the record type selection page, (unless you disable the users ability to select record types) requiring you to hardcode the url for the new page. Then, pass this url to a javascript snippet which relocates the page.


You could however, create visualforce pages for each "layout" and link to those without needing the record types, but that adds additional development work and maintenance.

  • 1
    +1. I prefer doing comparisons inside merge fields... if({!$ProfileName='Local Agency'}) { ... } else if({!$Profile.Name='Non-Local Agency'}) { ... } This prevents the code breaking if someone names their profile "John's Profile" (and yes, I've seen this happen). – sfdcfox May 10 '17 at 19:11

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.