Tag Archives: Force.com

Calculate # of Months between a Start and End Date Formula in Salesforce

IF(
AND(
YEAR(End_Date__c) = YEAR(Begin_Date__c),
MONTH(End_Date__c) = MONTH(Begin_Date__c)),
1,
IF(ISBLANK(End_Date__c),
(YEAR(TODAY())*12+MONTH(TODAY())) –
(YEAR(Begin_Date__c)*12+MONTH(Begin_Date__c)),
(YEAR(End_Date__c)*12+MONTH(End_Date__c)) –
(YEAR(Begin_Date__c)*12+MONTH(Begin_Date__c))
)
)

TestVisible annotation in Salesforce

TestVisible annotation 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 and how to access it in test class.

Sample Class:

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

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

Test Class:

@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
    }
}

Junction Object in Salesforce

A custom object with two master-detail relationships is called Junction Object. Junction objects are used to create many to many relationships between objects. A many-to-many relationship allows each record of one object to be linked to multiple records from another object and vice versa. One Object can have only two Master-Detail relationships.

For example, Let us assume that we have two custom objects – A and B. To provide the many to many relationship between A and B, we will need to create one more object let’s say it would be C, it will be called as junction object.

The first master-detail relationship you create on your junction object becomes the primary relationship. This affects the following for the junction object records:

Look and feel: The junction object’s detail and edit pages use the color and any associated icon of the primary master object.

Record ownership: The junction object records inherit the value of the Owner field from their associated primary master record. Because objects on the detail side of a relationship do not have a visible Owner field, this is only relevant if you later delete both master-detail relationships on your junction object.

Division: If your organization uses divisions to segment data, the junction object records inherit their division from their associated primary master record. Similar to the record ownership, this is only relevant if you later delete both master-detail relationships.

Note:

  • If we delete record A (First Master detail relationship is always primary) – then child record c will be deleted.
  • If we delete record B then in this case also child record C will be deleted.
  • If we delete record C then only C will be deleted , master record will not be deleted.
  • If child C has two Master record A and B, Where A is primary relation then Child record C will inherit the look and feel of Parent object A.

This organization isn’t authorized to upload change sets to other organizations

This organization isn’t authorized to upload change sets to other organizations. For authorization, contact the deployment connections administrators on the organizations where you want to upload changes.

This error is coming, just because of deployment settings. Seems like you have not created deployment connection between your source org and destination org.

To create deployment connection log into the destination org and follow below steps.

Setup -> Deploy -> Deployment Setting -> Select source sandbox from the available list of sandboxes – Edit -> Check allow inbound change sets check box and save.

What is the System.runAs() Method in Salesforce Apex Class?

Generally, all Apex code runs in system mode, where the permissions and record sharing of the current user are not taken into account. The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced. The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Note: We can use runAs only in test methods. The original system context is started again after all runAs test methods complete. The runAs method ignores user license limits. We can create new users with runAs even if your organization has no additional user licenses.

The following items use the permissions granted by the user specified with runAs running as a specific user:

  • dynamic Apex
  • methods using with sharing or without sharing
  • shared records

Example:

In the following example, a new test user is created, then code is run as that user, with that user’s record sharing access:

@isTest
private class TestRunAs {
   public static testMethod void testRunAs() {
      // Setup test data
      // This code runs as the system user
      Profile p = [SELECT Id FROM Profile WHERE Name='Standard Test User']; 
      User u = new User(Alias = 'standt', Email='standardtestuser@force.com', 
      EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='standardtestuser@force.com');

      System.runAs(u) {
         // The following code runs as user 'u' 
         System.debug('Current User: ' + UserInfo.getUserName());
         System.debug('Current Profile: ' + UserInfo.getProfileId()); 
      }
   }
}