Tuesday, 8 December 2015

Automate the Approval Process

We can use process builder  and trigger to automate the Approval Process in salesforce.

Now we are going to see, how process builder is used for automation.

First we need to create the Approval Process in Salesforce.

Approval Process:


Choose the Object, you want to create the Approval Process.

For creating the Process Builder, I have used "Jump Start Wizard"  in Approval Process.

 

Then the below page will get displayed.

Here I have given the

Name for the Approval Process.
Given the criteria as Type = "Customer-Direct , Customer-Channel"
Approver as Manager.

 
Click Save. The below page will appear.



Click on "View Approval Process Detail Page". The below page will get displayed.



I have a custom field called "Approval Status field". It contains the "Submitted,Approved,Rejected,Recalled" values.

In the Initial Submission Actions Section,  click on Add New -> Field Update. The below page will get displayed.

I have given the

1. Name
2. choose the Approval Status Field.
3. Choose the "Submitted" value in the "A specific Value" Field.

Click on save.

Do the similar steps for "Final Approval Action" and "Final Rejection Action".

For Approval Action give "Approved".
For Final Rejection Action give "Rejected".

Then Activate the Approval Process. It looks like below.


Process Builder


From setup Click on "Process Builder" under "Create". click New Button. below screen will appear. Give the Name and Description. Click Save.

 
The below screen will appear.

Click on "Add Object". Choose the "Account" object and you can choose either "only when record is created" or "when a record is created or edited". Here I have choose "Only when record is created".
 Now we to give criteria, Inorder to execute the Process Builder. Here I have given "No Criteria - Just execute".


Now we have to define the Action. Click on "Add Action" link in the Process builder and choose action type "Submit for Approval". Give the Action Name, Choose the Approval Process that you have created. choose the submitter as "Current User" and click Save.

If you want to schedule, you can schedule the Actions. this is similar to Time trigger workflow.

Now Activate the Process Builder.

Note: Once process Builder is activated. we cant edit the Process Builder. In order to edit, we have to clone the Process builder.


So when ever Account record is created, it will automatically submit the record for Approval. No need of custom code to automate this.






Wednesday, 27 May 2015

Using the isTest(seeAllData = true) Annotation



If we set the annotation @isTest(seeAllData = true) in the test class then that class will have access to all data in the organization.

Example Class:

  1. @isTest(seeAllData = true)
  2. Public Class Test_AccountClass
  3. {
  4.                 Static testmethod void testAccountMethod1()
  5.                 {
  6.                                 Account ac = [select Name from account Limit 1];
  7.                                 System.assert(ac != null);
  8.                 }
  9.                 Static testmethod void testAccountMethod2()
  10.                 {
  11.                                 // This method will also have access to all org data.
  12.                 }
  13. }
If we are using only @isTest, then it will not have access to all org data, but it will have access to the object that are used to manage our organization.

Object used for manage Organization:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CornTrigger
  • RecordType
  • Apex Class
  • Apex trigger
  • Apex Component
  • Apex Page

Example Class:

  1. @isTest
  2. Public Class Test_AccountClass
  3. {
  4.                 @isTest(seeAlldata = true)
  5. Static testmethod void testAccountMethod1()
  6.                 {
  7.                                 // this method will have access all the org data.
  8.                                 Account ac = [select Name from account Limit 1];
  9.                                 System.assert(ac != null);
  10.                 }
  11.                 @isTest Static testmethod void testAccountMethod2()
  12.                 {
  13.                                 // This method will have access only to the above mentioned object.eg:User
  14.                                 User u =[select Id,Name from User Limit 1];
  15.                                 System.assert(u != null);
  16.                 }
  17. }
This annotation will work for the test class saved using the salesforce API version 24.0 or latter calls. For earlier version i.e. 23.0 or earlier continues to have access to all data in the organization and its data access is unchanged.

Email Link in Email Template

Sometimes we may come across the requirement for email Templates like User has to click the email link and the link should redirect to the compose page of the email.

Here is the solution for that,

1. While creating the email Template, choose the HTML email template.


In this Template, I have added my signature. when the user click on the email it should go to compose page. for that I need to select the email and click on link in the tool bar.



Once the link is clicked, the dialog box will appear. There we have to give the values like below.


Once we give the mail link like this, it will redirect to the email compose page.

Wednesday, 20 May 2015

With Sharing and Without Sharing


The Use of “With Sharing” and “Without Sharing” is to enforce the sharing rules.

With sharing keyword:

 

It allows you to specify the sharing rules for the current user be taken into the account for Apex class.  Because apex classes will always runs in system context. 

In System context Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user. 

This is to ensure that code won’t fail to run because of hidden fields or objects for a user. If a class is defined as with sharing, the method within that class will act as with sharing.

Public Class with sharing apexsharingclass
{
// sample code
}

Without Sharing:

 

Without sharing will ensure that the class will not enforce the sharing rules for the current user. 

Public class without sharing apexclass
{
// your code.
}

Note: 

If the method within “with sharing” class is called by a “without sharing” class, then with sharing method will execute with sharing rule enforced.

Inner classes do not inherit the sharing setting from their container class.

Classes inherit the sharing setting from a parent class when one class extends or implements another.

Tuesday, 19 May 2015

Salesforce Object Search Language


When you want to find records for more than one Object based on search string that you specify is called SOSL.

We are passing the entire SOSL expression in “StringsearchExpr” parameter of the search() call.

SOSL does not search the following object and fields

  1. Any elements such as picklist that are defined as not searchable()
  2. Number,date or checkbox fields. (Use SOQL to search this fields)
  3. Textarea fields, unless you use the ALL FIELDS in search group.
  4. Attachment records associated with Account, contact or opportunity.

Example Query 

 

In Apex:

FIND ‘keyword*’ IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead

In Force.com API

FIND {keyword*} IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead
 
Find Keyword
  • You can search a single word or a phrase.
  • You can use “ ”,AND,OR,AND NOT,().
  • Reserved Character:
? & | ! { } [ ] ( ) ^ ~ * : \ " ' + -  
            If we are using the above characters, then we should use backslash \ character (escape sequence).

IN Search group: 

 

This is optional clause, which allows you to define the type of fields to search.
ALL FIELDS
EMAIL FIELDS
NAME FIELDS
PHONE FIELDS
SIDEBAR FIELDS


Returning:


Syntax:
Returning ObjectTypeName [(Fieldlist [where ConditionExpression] [order BY Clause] [LIMIT n])][ ,ObjectTypeName[(FieldList) [where ConditionExpression] [Order BY clause] [Limit n])] ]

Object Type Name
Object to return.
Field List
Optional One or more field from the object
Where Condition
Optional, Specify the filter for the particular object
Order By Clause
Optional, Arrange the given values in asc and desc for the specified object.
Limit n
Optional, Number of rows the query should return for the specified object.

With Division :


The division should be enabled for the organization, in order to use this syntax. It will act similar to where condition. But if we use division keyword, below are the advantages.

  • It pre-filters all records based on division before applying other filters.
  • You can specify the division’s name in the filter, rather than its ID( as is required if you filter on division in the WHERE clause)

To Label:


Syntax:

toLabel(Object.field)

FIND ‘string*’ RETURNING Account(Name, toLabel(Recordtype.Name))

This query returns Account records with the record type name translated into the language for the user who issued the query.

We cannot filter on the translated name value of the record type. Always filter on the master values or the ID of the object for record types.