Showing posts with label Visualforce Example. Show all posts
Showing posts with label Visualforce Example. Show all posts

Monday, June 2, 2014

How to know Recently Viewed items using Soql Apex Class?

How to  know Recently Viewed items using Soql Apex Class?


In Summer ’13 Release salesforce introduced the new Standard Object called  RecentlyViewed, so using that we can easily get the all the recently viewed items using apex class.
Sample SOQL:
SELECT Id, Name, LastViewedDate FROM RecentlyViewed  WHERE Type IN ('Account', 'Contact', 'Case') ORDER BY LastViewedDate DESC
Note:
The RecentlyViewed object does not support the Report, KnowledgeArticle, and Article objects.





What is the maximum trigger depth exceeded exception in salesforce?

What is the  maximum trigger depth exceeded exception in salesforce?



When you are creating an Apex code that recursively fires triggers due to insert/update/delete statement for more than 16 times. You will get the Maximum Trigger Depth Exceeded error.

The following example will demonstrate this issue:

trigger cloneAnotherAcc on Account (before insert) {
Account acc = new Account(name=’Clone me’);
insert acc;
}

This trigger will end up in an infinite loop.

In order for you to solve this issue, you can set a condition on insert so it will not be called recursively. Set a flag to manage the insert trigger will be the ideal. To do this, you need to create a new class to keep track the number of times of insert or stop the insert process on second time.

global class triggerCount {
static integer runTimes = 0;

public static integer getRunTimes(){
return runTimes;
}

public static void setRunTimes(){
runTimes++;
}
}

Once you successfully create this class, you can implement this triggerCount class on your trigger to limit the number of times for insert.

trigger createAcc on Account (before insert) {
if(triggerCount.getRunTimes < 2){
Account acc= new Account(name=’Clone me’);
triggerCount.setRunTimes();
insert acc;
}
}



Friday, May 30, 2014

What is TestVisible annotation in Salesforce?

What is TestVisible annotation in Salesforce?


Hi,
This is the documentation notes about @TestVisible annotation in salesforce. 

Use the TestVisible annotation to allow test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes. This annotation enables a more permissive access level for running tests only. This annotation doesn’t change the visibility of members if accessed by non-test classes.

With this annotation, you don’t have to change the access modifiers of your methods and member variables to public if you want to access them in a test method. For example, if a private member variable isn’t supposed to be exposed to external classes but it should be accessible by a test method, you can add the TestVisible annotation to the variable definition.

This example shows how to annotate a private class member variable and private method with TestVisible.


public class TestVisibleExample {
    // Private member variable
    @TestVisible private static Integer recordNumber = 1;

    // Private method
    @TestVisible private static void updateRecord(String name) {
        // Do something
    }
}    

This is the test class that uses the previous class. It contains the test method that accesses the annotated member variable and method.

@isTest
private class TestVisibleExampleTest {
    @isTest static void test1() {
        // Access private variable annotated with TestVisible
        Integer i = TestVisibleExample.recordNumber;
        System.assertEquals(1, i);

        // Access private method annotated with TestVisible
        TestVisibleExample.updateRecord('RecordName');
        // Perform some verification
    }





That's It!

Enjoy the Coding !!!!!!   

 
| ,