Author: Prasanna

  • Salesforce: Adding specific business days to a date using BusinessHours class

    Businesshours is a system class provided by Salesforce for performing various operations on the DateTime values. It provides methods for checking if a date falls during business hours, whats the diff between two dates in business.

    The in-built method does allow adding the specific days to the DateTime value but there is a catch, it needs to be in the Long format. More information could be found here.

    add(businessHoursId, startDate, intervalMilliseconds)

    What exactly this method does?

    The method has been specifically designed to add the number of hours in the millisecond format. And addition is done with respect to business hours. Let’s consider you want to add 1 day to the Date December 18th, 10 AM. And business hours are set for 8 AM to 8 PM for all days.

    Long interval = 24 * 60 * 60 * 1000;
                     h   m    s    ms
    Datetime dt = DateTime.newInstance(2017, 12, 18, 10,0,0);
    System.debug('Date :' + dt.format());
    Long interval = (24 * 60 * 60 * 1000);
    Datetime newdt = BusinessHours.add(bh.Id, dt, interval);
    System.debug('After adding 1 day :' + newdt.format());
    
    //OUTPUT
    22:12:52:003 USER_DEBUG [3]|DEBUG|Date :12/18/2017 10:00 AM
    22:12:52:004 USER_DEBUG [6]|DEBUG|After adding 1 day :12/20/2017 10:00 AM
    

    This calculation added complete 2 days as business hours are set 12 hours a day.

    Alternative for avoiding such hour based calculation could be going in an iterative way.

    public static Datetime addBusinessDays(Datetime startDate, Integer numberOfDays, id busniesshourId)
    {
    	Integer count = 0;
    	while (count < days) {
    		startDate = startDate.addDays(1);
    		if (BusinessHours.isWithin(businesshourId, startDate))
    			count++;
    	}
    	return startDate;
    }

    This will also ensure that the initial time is preserved.

  • Salesforce: Trigger on Attachment – Restricting user from attaching files with specific extensions

    By default, Salesforce doesn’t allow admin to configure or restrict a user from adding a specific type of files. Admin may need that user should not be able to attach files of type exe, dll which can have the virus in them. Also, there is no virus check is done when a file is getting uploaded to the Salesforce.

    But Salesforce does allow writing trigger on the Attachment Object by which you can implement such restrictions. Following is the snippet of the code which will restrict the user from adding the files with extension mentioned in the set list. You can also edit the code and do the reverse by checking if extension exists in the set then only allow attaching the file.

    https://gist.github.com/prasannadeshpande/7ad6f5e49c83ab5a84e628e1096c24f8

    Once trigger code is up and running, whenever Salesforce tries to attach any file with extension exe or dll he will come across following error message.

    AttachmentException

  • Changing default RDP port of Amazon EC2 server

    First thing you should do if you are hosting a server in AWS cloud is to change its default ports.

    Why one should do that?

    1. Does changing default port number actually increase security?
    2. The 5 biggest security mistakes users make in Amazon’s cloud
    3. AWS Security Alert: Insecure RDP Server Configuration

    Steps to change the RDP port for EC2 server. YOU NEED TO PERFORM ALL BELOW STEPS IN THE SAME SEQUENCE ELSE YOU CAN LOSE SERVER/ RDP ACCESS.

    1. Configure your Security Group and allow inbound access to the custom port you want to use for RDP (Say 7777). You can get more information about the Security Groups here.

    Once done, next step is to open up the port 7777 from the server firewall so that external system can connect on this port.

    You can manually open up the port from the Firewall settings or you can run following command if you have admin access.

    netsh advfirewall firewall add rule name=Custom RDP Port dir=in action=allow protocol=TCP localport=7777

    Last step is to change the RDP listening Port:

    To change the port that Remote Desktop listens on, follow these steps.

    Important This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base:

    322756 How to back up and restore the registry in Windows
    1. Start Registry Editor.
    2. Locate and then click the following registry subkey:
      HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp\PortNumber
    3. On the Edit menu, click Modify, and then click Decimal.
    4. Type the new port number(7777 in our case), and then click OK.
    5. Quit Registry Editor.
    6. Restart the computer.