Thursday, 24 April 2014

Encryption in salesforce

Hi

The methods in the Crypto class provide standard algorithms for creating digests, message authentication codes, and signatures, as well as encrypting and decrypting information.
These can be used for securing content in Force.com, or for integrating with external services such as Google or Amazon WebServices (AWS).

Example code for Crypto class

public class HMacAuthCallout1 {

   public void testAlexaWSForAmazon() {
      system.debug('----Checking time-----------'+timestamp);
      String action = 'salesforce Key';
      String inputStr = action;
      String algorithmName = 'SHA1';
      Blob mac = Crypto.generateDigest(algorithmName,  Blob.valueOf(inputStr));
      system.debug('-------------'+mac);
      String md5Coded = EncodingUtil.convertToHex(mac);
      system.debug('------SHA1 Code------------'+md5Coded);     
  }
}

For more information about crypto class

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_classes_restful_crypto.htm

you can check the encrypted code in

http://sha.shadecrypt.fr/SHA1-SHA256-SHA384-SHA512/encrypt.html

Thursday, 17 April 2014

System.LimitException: Apex CPU time limit exceeded

Hi

The time taken by the apex transaction in a context including validations,workflows apex code and  excluding time taken in DML,queries,callouts is called CPU time .

below is the list that will be taken into account for calculating CPU time

- All Apex code
- Library function exposed in Apex
- workflow Execution

below is the list that will not be taken into Account 

- DatabaseOperation eg: soql,DML
-  SOSL
- HTTP callouts.

If you get this error, it is because your apex code is not efficient, so follow the best practice to write the code.

like 

you can avoid so many conditions inside the for loop.
reduce the for loop.

CPU time Limits

Synchronous - 10,000 ms
Asynchronous - 60,000 ms

below are the methods where we can get the CPU time and CPU limit.

Limits.getCPUTime() - Returns the CPU time(in milliseconds) accumulated on the salesforce.com server in the current transaction.

Limits.getLimitCPUTime() - Returns the total CPU time (in milliseconds) accumulated on the salesforce.com servers in the current transaction.

Tuesday, 15 April 2014

Future Methods Limitations

Hi

Below are the list of the limitations for future annotation.

  • Methods with the future annotation must be static methods, and can only return a void type.

  • The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. 

  • Methods with the future annotation cannot take sObjects or objects as arguments.

  • Remember that any method using the future annotation requires special consideration because the method does not necessarily execute in the same order it is called.

  • Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
 
  • You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.

  • The getContent and getContentAsPDFPageReference methods cannot be used in methods with the future annotation. 

For more information please refer the below link.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

Monday, 14 April 2014

MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object

Hi

 We cannot perform DML operation on setup and nonsetup Objects in salesforce.

Example for Non-setupObject -  standard object like Account,contact etc and any custom object.

Example for setObject -

Group
Group Member
Field Permission
Object Permissions
Permissionset
Permissionset Assignment
Queue sObject
SetupEntityAccess
User
UserRole
UserTerritory
Territory


If you are using a Visualforce page with a custom controller, you can only perform DML operations on a single type of sObject within a single request or action. However, you can perform DML operations on different types of sObjects in subsequent requests, for example, you can create an account with a save button, then create a user with a submit button.
You can perform DML operations on more than one type of sObject in a single class using the following process:
  1. Create a method that performs a DML operation on one type of sObject.
  2. Create a second method that uses the future annotation to manipulate a second sObject type.

For More information please refer the below link.

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_mix_sobjects.htm?SearchType=Stem

Friday, 11 April 2014

Salesforce to Salesforce Connection Using Trigger

Hi

When we publish connection between one org to another org, it will be very difficult for the end user to forward the data manually to the another org.

Salesforce provide some standard object, to get the connection and do some automation for connections.



PartnerNetworkConnection - It is used to get the connection Id from the salesforce.


PartnerNetworkRecordConnection - It is used to push the record into another organization.


Apex Trigger Code.

trigger ConnectionOnLead_Trg on Lead (after insert,after update) {
List<Lead> Leadlist = new List<Lead>();
for(Lead l: trigger.New)
{
    if(l.Status == 'Closed Xfer' && l.Lead_Source_Original__c == 'Prod1')
    {
        Leadlist.add(l);
    }
}
if(Leadlist.size() > 0)
{
    List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>();
    List<PartnerNetworkRecordConnection> connMaplist = new List<PartnerNetworkRecordConnection>();
    connMap = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted'];
    system.debug('---------Connection Trg----------------------'+connMap+'*****SIZE*******'+connMap.size());
    for(PartnerNetworkConnection Network : connMap)
    {
        for(Lead le : Leadlist)
        {
             PartnerNetworkRecordConnection conn= new PartnerNetworkRecordConnection();
             conn.ConnectionId = network.Id;
             conn.LocalRecordId = le.Id;
             conn.SendClosedTasks = false;
             conn.SendOpenTasks = false;
             conn.SendEmails = false;
           //  conn.ParentRecordId = newContact.AccountId);
           connMapList.add(conn);
        }
    }
    if(connMapList.size() > 0)
    {
        insert connMapList;
    }
}
}

If it met the criteria, it will automatically publish the record to another org.