RenderAs – Rendering visualforce page as a pdf

Rendering visualforce page as a pdf




How can you rendering vf page as a pdf? By using renderAs attribute you can render visualforce page as a pdf.

Where we can use renderAs attribute? We can use renderAs attribute in apexPage tag like below.

<apex:page  StandardController=”Account ” renderAs=”pdf”>

Below is simple code to rendering a visualforce page as pdf.

<apex:page renderAs=”pdf”>
<center>
<h1>Welocome to Salesforce Tutorial</h1>
</center>
</apex:page>

Below is the output for this.

RenderingAs








ViewState in salesforce

What is viewState?




ViewState holds state of the visualforce page that holds state that includes the fields, components and controller state. Viewstate data in encrypted and cannot be viewed tools like firebug.

To maintain state in a Visualforce page, the Force.com platform includes the state of components, field values, and controller state in a hidden form element. This encrypted string is referred to as the view state and has a limit of 135KB.

The data in the viewstate should be sufficient to recreate the state of the visualforce page when the postback is received. To do this, viewstate stores:

– Stores Non transient data members in the associated controller (standard\custom) and controller extensions.

– Stores Objects that are reachable from a non-transient data member in a controller or extension.

–  Stores the component tree for that page, that tree represents the visualforce page’s component structure and the associated state, which are the values applied to those components.

– view state stores small amount of data.

How to see view state:

You can see view state when developer mode is enabled. See below screen.

ViewState





Field Level Security in salesforce

Field Level Security (FLS):




What is Field level security (FLS)? At object level we can give permission on what user can with salesforce record like user can read, create, edit, delete, view all & modify all. Object permissions we can give at object level. Now i want to restrict few sensitive fields form my users. I don’t want display these sensitive fields for all users. We can restrict fields by using field level security.

We can give read & edit permissions to the field. We can handle these filed level permissions in three ways.

1. By using “set field level security” button at field level.

2. By using “view field accessibility” button at field level

3. In profiles/permission sets we can handle field level permissions.

How can we provide filed level security?

Now in Account object object i don’t want to display revenue filed for all profiles in salesforce organization. For few profiles i want give visible permission , For few profiles i don’t want display this filed and for few profiles i want to give both visible and edit permissions. So how many ways we can achieve this? We can achieve this  two ways first is from filed and second one is from profile.

1. Field level security by using set field level security button:

Go to the filed you want to enable field level permissions and click  on Set field level security button. See the below screen for reference.

Field level security

After clicking field level security button, there you can enable that field for profiles you want give access and save settings. See the below screen for reference.

FLS2

If you enable only visible check box, then user can read & edit that field. If you enable both visible & read only, then user can only see that field, he can edit.

2. By using view field accessibility button.

Go to the filed you want to enable field level permissions and click  on view field accessibility button. then you will navigate to below screen.

Field level Security

Here you can field accessibility options. Here we can manage field viability for profiles and page layouts. You can select fields in field access for field list view. If you want enable/disable permission click hyper link editable/hidden, you will get below screen.

Filed level securityhere you can select filed level security at profile level and you can select page layout options also.

3. Managing Field level security at profile level

To handle Field permissions go to profile you want choose and go to Field level security section and search for object you want enable field permissions, click on view and edit and provide permissions for required fields and save.

Field level security

Note: In profiles you can find Field level security section on profile detail page. You cannot find  Field permission on profile edit page.





How to provide Object Permissions?

Object Permissions




What is object permissions? In salesforce we can provide permissions to object at profile/permissions sets level. Object level we can provide read, create, edit, delete, view all & Modify all permissions in profiles/permission sets.

Read: Users can only read/view records.

Create: Users can read & create records.

Edit: Users can read & Edit.

Delete: Users can read, Edit & delete records.

View All: Users can view all records in the object irrespective of sharing settings.

Modify All: Users can read, edit, delete, view all & modify all (can transfer, approve) irrespective of sharing settings.



Above 6 permissions we can set at object level in profile/permission set. Read, Create, Edit, Delete respects sharing settings. But View all & Modify all overrides sharing settings.

Navigation to provide object permissions

Setup -> Administer -> Manage Users -> profiles -> select the profile you want give object permissions and go to object permissions and provide required object permissions to profile. See the below screen for reference.

Object Permissions





Visualforce page to add multiple records

Adding multiple records:




Generally by using standard salesforce tab you can enter only one record at a time form UI. I don’t want to enter single record at a time. I want to enter multiple records at time. How can we achieve this? We can achieve this by using simple visual force page.

Example: I want to enter multiple accounts in single  save. See the below visualforce page.

Visualforce page:

<apex:page Controller=”AddmultipleAccountsController”>
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value=”{!listAccount}” var=”acc”>
<apex:column headerValue=”Account Name”>
<apex:inputField value=”{!acc.Name}”/>
</apex:column>
<apex:column headerValue=”Account Number”>
<apex:inputField value=”{!acc.AccountNumber}”/>
</apex:column>
<apex:column headerValue=”Account Type”>
<apex:inputField value=”{!acc.Type}”/>
</apex:column>
<apex:column headerValue=”Industry”>
<apex:inputField value=”{!acc.Industry}”/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value=”Add Accounts Row” action=”{!addAccount}”/>
<apex:commandButton value=”Save Accounts” action=”{!saveAccount}”/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>



Controller related to above page:

public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }

public AddmultipleAccountsController()
{
listAccount=new list<Account>();
listAccount.add(account);
}

Public void addAccount()
{
Account acc = new Account();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.Allaccountssaved;
}
}

Visualforce page Allaccountssaved( using this page above controller):

<apex:page sidebar=”false” showHeader=”true”>
<center><h3>Congrtas your accounts are successfully saved!!</h3></center>
</apex:page>

Output of above example:

Multiple records

Here Add Account row is used to enter one more account and Save Account is used to save all account records you entered. See below screen, you can understand functionality of Add Account Row button.

Multiple

After saving you will get below screen.

Multiple 3




This is simple example to explain how to enter multiple records at time form UI.




Working with Chatter Group

Chatter Groups




What is a chatter groups? What type of chatter groups we can create? Groups allow collaborate with specific set of people. Many of you know about groups in Facebook. In Facebook you can create groups and you can invite your friends as a members. The same type of groups you can create in salesforce chatter groups and you can invite members to that group. We can create public and private chatter groups.

Public Group: In public groups every one can join and every one can see, add post, add files & add comments.

What public group members can do in public groups: Public chatter group Members can change email settings, they can invite people and they can leave the group.

If you are the public group the owner, if you are a public chatter group owner, you can add/remove group members, you can change the group photo, you can edit group settings like information section and description section and you can change member roles.



Private Group: In private groups only members can see add posts, comments and files. And also Group admin/Owner approval must to join in private groups. People who is having modify all permission at profile/permission set level  can see post, updates, and files, and they can join private groups directly.

What private group members can do in private groups: Private chatter group Members can change email settings, they can invite people and they can leave the group.

If you are the private group the owner, If you are a owner of private chatter group you can add/remove group members, you can change the group photo, you can edit group settings like information section and description section, you can change member roles and  you can accept/decline group member request.

Creating Chatter Group: 

To create chatter group user should have “Create and Own new chatter groups” user permission required at profile/permission set level.

To create chatter group login to salesforce -> click on chatter tab -> click on Groups and click on New Group. See the below screen for reference.

Chatter Group

After clicks on new group and enter required details and save it. See the below screen for reference.




Chatter Group

While creating groups you have to enter

1. Basic information like Group name, description and Archive settings.

2. Group access settings like Public and Private and save. After creating group, members/owners can see/edit group settings like email settings, information, description they can invite members to the group. And also publisher actions available those are marked in red color in below image and also you can share your posts. See the below screen for reference.

chatter group





Salesforce chatter

Working with Salesforce Chatter:




What is Salesforce chatter? Chatter in salesforce allows you to collaborate the people with each other in your organization. You can connect with your coworkers and you can share information securely in real time. You can create groups like Facebook groups such public groups and private groups and you can invite your coworkers to join those groups by using salesforce chatter. And also you share your comments, add images and you can like others comments/posts.

By default Salesforce chatter is enabled now in Salesforce (Organizations created after 22nd June 2010). If this is not enabled to your organization and if you want to enable this setup follow below steps.

Make sure that you have “Customize application” user permission to do this setup.

1. Login to Salesforce.

2. Setup -> Customize -> chatter -> settings and ‘enable’ chatter under chatter settings and save.

See the below screen for reference.

Salesforce Chatter

Once you enabled chatter, by default Global header will be enabled to your organization. And also by default you may fallow few users and records.



By enable chatter few additional profile permissions will come at profile/permission set level. Those are

App Permissions:

Salesforce Chatter: When you enable this functionality automatically this application permission will be enabled for all your profiles in slaesforce. If you don’t want to give this application permission to some profiles, you can disable this permission.

Tab Permission:

Chatter : When you enable chat functionality, this tab will automatically enable (default on) to all profiles in your organization. If you don’t want display this to some profiles you can hide this tab.

Administrative permissions: Chatter Internal User, Create and Own New Chatter Groups, Invite Customers to Chatter, Moderate Chatter, and Manage Chatter Messages.

General User permissions: Create and Share Links to Chatter Files, Insert System Field Values for Chatter Feeds.

Important notes when you enable chatter in salesforce:

– After enabling this you will see new look and feel. Global header will enable there you can search your sobject records, chatter feeds, Groups and users.

– All users will automatically fallow when you enable this for organization with 15 or fewer users.

– This functionality is automatically available for salesforce1 apps when you enable in your org.

– Few standard publisher action available when  enable this. And also you can create your own publisher actions.




There are many more interesting things about salesforce chatter. See reference to more about salesforce chatter.





Using VLOOKUP to enforces data consistency

Using VLOOKUP function in validation rules




What is the purpose of VLOOKUP function? It searches an object for a record where specified field matches the specified lookup value. If a match is found, returns another specified value.

Syntax of vlookup:

VLOOKUP(field_to_return, field_on_lookup_object, lookup_value)

Here I will give a simple example, how to use VLOOKUP function in validation rule.




Example: Create validation rule that checks the “Billing ZIP/postal code” entered against a table to validate that the zip code and state match.

To track ZIP Codes I have created custom object called “Zip Code” in this object I am storing zip code, states & countries. And created below records (sample records) in this zip codes object.

ZIP Code State City
35004 Alabama Moody
35005 Alabama Adamsville
35010 Alabama Alexander City
90001 California Los Angeles

Below validation rule validate Billing zip/postal code in account object with the codes in zip code object. If you enter wrong zip code this validation rule throw an error.

Creating validation rule:

To create validation rule go to setup -> Build -> Customize -> Account -> Validation rule and enter required information.

Error condition formula:

VLOOKUP($ObjectType.Zip_code__c.Fields.State__c, $ObjectType.Zip_code__c.Fields.Name , LEFT( BillingPostalCode ,5))<> BillingState

See the below image for reference.

VLOOKUP

How to test this validation rule?

To test this validation rule go to Account tab and create new account record with enter billing city = Los Angeles, Billing State/Province = Califirnia and postal code = 3099 and save this record, you will get associated error message. If you enter correct data which is mentioned in above table, record will save. If you enter wrong data validation rule will fire an error.

See the below image for reference.

VLOOKUP





Using REGEX function in validation rule

REGEX function




What is the purpose of REGEX function? By using this function we can build validation rule to enforces proper data format.

Example : Validates Account Billing Zip/Postal Code is in 5 digit format or 9 digit format if billing country is USA.

Steps to create validation rule:

Go to steps -> Build -> Customize -> Account -> Validation Rule and enter required fields and save validation rule.




Error condition formula for this example:

(BillingCountry=="USA")&&NOT(REGEX(BillingPostalCode,"\\d{5}(-\\d{4})?"))

See the below screen for reference.

REGEX

Testing above validation Rule:

To test this validation rule go to account tab and give Billing country = USA and Billig ZIP/Postal code = any number which is not equal to 5 or 9 digit and try to save account record, you will get error message. See below screen for reference.

REGEX





Validation Rules in Salesforce

Validation Rules




What is a validation Rule? Validation rules helps you to improve data quality by preventing users form entering incorrect data. We can write one or more validation rules that consists of  an error and corresponding error message.

1. Validationrules are executed will executed, when you are saving the record.

2. A validation rule that contains a formula or an expression that evaluates the data in one or more fields and returns a value, true or false. We can display error message at the top of the page or below the field when rule returns true.

3. After writing validationrules for a field or for set of fields, following actions will fired when user creates a new record or edits an existing records and then click on save button.

  • Salesforce executes validationrules you defined and if data is valid then record will save.
  • If entered invalid data, it will display the associated error message without saving the record.
  • Even if the fields referenced in the validation rules are not visible on the page layouts, the validation rule still apply and will result in an error message if the rule fails.

Creating validation rules:

For standard objects Go to setup -> Build – > Customize -> select standard object you want to create validationrule (For Ex: Account) -> and click on validation rules and then define your validationrule.

For custom objects Go to setup -> Build -> create -> object -> select object you want to create validationrules -> go to validation rules section and then create your validationrule.




Examples of validationrules:

Example 1: In this example i will make field required depends on the other field value.

Here i am writing validationrule on account object. “If Industry equal to Banking then annual revenue should not be null”.

Steps to write this validation rule.

Go to setup -> Build -> customize -> Account -> Validationrule -> click on NEW and enter required fields and click on save.

See below information and image for reference.

Rule Name: Validation_on_Account_revenue

Active : True

Error condition Formula: AND( ISPICKVAL(  Industry  , ‘Banking’),ISBLANK(AnnualRevenue) )

Error Message: Annual Revenue should not null when Industry is Banking.

Error Location: at field Annual Revenue.

Validation Rule 1

 How to test this validation rule?

Go to Account Tab and create one new account record and give Industry = Banking and Annual Revenue = Null and click on save button, you will get error message. See the below screen for reference.

Validation Rule 2