Play Games

Search This Blog

Thursday, July 27, 2017

How to create/write trigger on custom object in salesforce

Step 1: Go to setup --> Create --> Objects

Step 2: Select the custom object on which you want to write a trigger and click on its label.

The custom object page will be opened as shown in the image below.

scroll down the page,you will find a section with trigger as shown in the image.
Click on the "new" button to create the trigger.

How to create Custom Object in Salesforce

How to create Custom Object in Salesforce

Step 1 :Go to setup --> Create --> Objects

Step 2: Click on 'New Custom Object' button.
Enter all the mandatory fields as shown in the image below and click on "Save Button".


That's it,the custom object is created as shown in the image below.


Wednesday, July 26, 2017

How to write test classes in Salesforce

Step 1:Test class starts with @isTest and then we create test class as shown below

@isTest
public class AccountControllerTest {
}

Note: 1) While naming test class,We may append/prepend Test for the original class/controller.

2) If you perform any DML operations like insert/update/delete,those data are not saved to Original Database.You won't be able to access those test data in normal classes.

@isTest : indicates that the class is a test class.

Step 2: Create one method to validate/test data present in the original controller.

@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {

}
}

Test.StartTest() and Test.StopTest() are used to indicate exact start and stop of test class.Use them inside the method as shown below after setting up of data.

@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
Test.StopTest();
}
}

Step 3: Call the methods present in the original controller.
Example:
Assume we have the following class with 4 methods as shown below.
public class AccountController {
public void method1() {
}
public void method2() {
}
public void method3() {
}
public void method4() {
}
}
In order to call the methods of above class,We create instance of that class first and then we call those methods one by one.
like this
AccountController objAccount = new AccountController();
objAccount.method1();
objAccount.method2();
objAccount.method3();
objAccount.method4();

The test class will look like this

@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
AccountController objAccount = new AccountController();
objAccount.method1();
objAccount.method2();
objAccount.method3();
objAccount.method4();
Test.StopTest();
}
}

Step 4: If a class contains static methods,directly call those methods with class name without creating instance to that class.
Example:
Assume we have the following class with 2 methods as shown below.
public class AccountController {
public static void staticMethod1() {
}
public static void staticMethod2() {
}
}
In order to call the methods of above class,We directly call those methods by using className
like this
AccountController.staticMethod1();
AccountController.staticMethod2();

The test class will look like this
@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
AccountController.staticMethod1();
AccountController.staticMethod2();
Test.StopTest();
}
}

Step 5: If a method in a class has any arguments,then while calling that method in test class,We need to pass respective arguments.
Example:
Assume we have the following class with 2 methods as shown below.
public class AccountController {
public static void staticMethod1(Account acc) {
}
public static void staticMethod2(Account[] lstAcc) {
}
}

In order to call the methods of above class,first we create data that is required and then we call those methods by passing valid arguments.
For staticMethod1,Account acc is argument.So,first we insert account and then we can call that method by passing account instance like this
Account objAccount = new Account();
objAccount.name ='Test Account';
insert objAccount;
AccountController.staticMethod1(objAccount);

The test class will look like this
@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {
Test.StartTest();
Account objAccount = new Account();
objAccount.name ='Test Account';
insert objAccount;
AccountController.staticMethod1(objAccount);
Test.StopTest();
}
}
For staticMethod2,Account[] is the argument.It means list of Accounts are passed as argument to that method.So first we need to prepare that accountList and then call that method by passing that list variable as shown below.
List<Account> lstAccount = new List<Account>();
Account objAccount1 = new Account();
objAccount1.name ='Test Account1';
lstAccount.add(objAccount1);
Account objAccount2 = new Account();
objAccount2.name ='Test Account2';
lstAccount.add(objAccount2);
insert lstAccount;
AccountController.staticMethod2(lstAccount);
The test class will look like this
@isTest
public class AccountDisplayTest {
//Sample method to test data of original class
public static testMethod void validateData() {

List<Account> lstAccount = new List<Account>();
Account objAccount1 = new Account();
objAccount1.name ='Test Account1';
lstAccount.add(objAccount1);
Account objAccount2 = new Account();
objAccount2.name ='Test Account2';
lstAccount.add(objAccount2);
insert lstAccount;
Test.StartTest();
AccountController.staticMethod2(lstAccount);
Test.StopTest();
}
}
Note: More info will be provided soon.....

How to fetch records order by a field where empty(null) values to be displayed last using SOQL in Salesforce

Problem: I want to fetch all contacts and I need to display contacts which are associated with Account first and then remaining last.
Solution: We can use NULLS FIRST or NULLS LAST keywords along with order by clause in soql while retrieving.
Example:
When you run the below query in Developer Console
SELECT Name,Account.id FROM Contact ORDER BY Account.name ASC NULLS LAST


When you run the below query in Developer Console
SELECT Name,Account.id FROM Contact ORDER BY Account.name ASC NULLS FIRST


Tuesday, July 4, 2017

How to remove duplicates from a column in eclipse

How to remove duplicates from a column in eclipse

Problem: I have some values in a column in excel sheet.I want to remove all the duplicates from that column.

Solution : Select that column and then go to Data --> click on "Remove Duplicates" as shown in the image below.

A popup will be opened as shown in the image below.

Select necessary column and click ok.
Thats it,all duplicate values are removed.

A Java Runtime Environment (JRE) or Java Development kit (JDK) must be available in order to run Eclipse. No Java virtual machine was found after searching the following locations: C:\eclipse\jre\javaw.exe javaw.exe

Problem: When ever jdk/jre is updated,then at that time,eclipse may not work properly and throws the below error.
Solution:
Step 1 : Go to folder where eclipse is installed and open eclipse.ini as shown in the image below.
Step 2 : Check the path given below -vm in the above file and change it as per the latest java update and save it.
Thats it...Now your eclipse may work.

Salesforce Platform Developer II Certification Exam pattern going to change/Your New Path to Platform Developer II

As per TrailheaDX announcement,  PDII certification exam will contain 2 parts.
Part 1: A proctored multiple-choice exam—the same one that exists today.
Part 2: Completion of four predefined superbadges instead of Assignment exam previously.

Go through the link below for more information https://www.salesforce.com/blog/2017/06/platform-developer-ii-certification.html

Note: Please wait until salesforce provides more details on this...

Monday, July 3, 2017

How to add elements to string array in java?

How to add elements to string array in java?

Sample Code:
String[] sArray = null;
for(int j =0 ;  j <100; j++) {
sArray[j] =  "Test";

com.sforce.ws.ConnectionException: Check authEndpoint. It must contain '/services/Soap/u/' error in Java

Problem : If you are using partner wsdl and trying to access salesforce org using java code ,then at that time you may encounter this error if authEndpoint is wrong(https://login.salesforce.com/services/Soap/c/40.0).
Solution : If you are using partner wsdl,then authEndpoint url will be of this form
https://login.salesforce.com/services/Soap/u/40.0

If you use url like this "https://login.salesforce.com/services/Soap/c/40.0" for partner wsdl, then you get above error  because this url will be for Enterprise wsdl files.
Note: /services/u for Partner Wsdl.
/services/c for Enterprise wsdl.

Note:Versions may change for example https://login.salesforce.com/services/Soap/u/39.0 or https://login.salesforce.com/services/Soap/u/38.0 etc.

How to generae partner.jar file from related partner wsdl file.

Steps:
Step 1: Download partner wsdl from salesforce.com and save it as xml file.
In salesforce organisation, Go to Setup -->Develop --> API --> Click on "Generate Partner WSDL" as shown in image below and save it as "wsdl.jsp.xml".
Store this wsdl file in one folder as shown in the images below.

Step 2: Download wsc-23.jar file from below link and store it in same folder.
https://code.google.com/p/sfdc-wsc/downloads/list

Step 3: Open command prompt and move to that specified folder path and run the following command.
Assume "C:\PartnerJarFileCreation" is the path of folder
c:\PartnerJarFileCreation>java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc wsdl.jsp.xml partner.jar

It will generate one partner.jar file as shown in the image below.


javac: target release 1.6 conflicts with default source release 1.8 Error: Failed to compile Error

Problem: When generating jar file using enterprise/partner wsdl file using command prompt,sometimes you get the above error.

Solution: Check the wsc file you used.If you are using java 1.7 or above,wsc-19.jar won't support. you can use wsc-23.jar file.

Example: If you want to generate partner.jar file use command like this
java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc wsdl.jsp.xml partner.jar
where wsc-23.jar is the jar file,wsdl.jsp.xml is the partner wsdl file.
Note: All the files should be placed inside folder as shown in the image below.
tools.jar
wsc-23.jar
wsdl.jsp.xml