Sunday 5 July 2015

How to deal with: Apex CPU time limit exceeded


In my recent project on SFDC-SAP integration, there is one interface called "Customer Master Data and Hierarchy". SFDC has exposed this service thru SOAP APIs. The web service class is so huge and complex (more than 2000 LoC) that I have to keep the code optimization and performance in mind. During bulk data load testing in UAT, SFDC thows "Apex CPU time limit exceeded" limit exception during run time.

Wayout: Optimize your code by using maps and sets. Use less or avoid using nested For loops as the more is the iterations, the more time it takes for the logic when there are bulk load (thousands and thousands of records).

In my case,

1. there were two nested For loops which was causing the error: Replaced with only one For loop and used maps and sets for compensating this change.
2. the packet size from SAP:  When it was set to 999, SFDC was skipping some intermidiate batch of data. When it is set to 200. everything is perfect.

How to deal with: System.ListException: Duplicate id in List [duplicate]


Sometimes you may have received this error:
System.ListException: Duplicate id in List [duplicate]..

The caveat is: List should not hold multiple instances of the same object. Otherwise you will get this run time exception.

It's better to user set or map as they remove the duplicates.

Account oAccount = new Account(Id = sSfdcAccountId) ;
list<Account> listAccountWithDuplicateIds = new list<Account>() ;
listAccountWithDuplicateIds.add(oAccount) ;
Database.insert(listAccountWithDuplicateIds, false) ;

will result in System.ListException: Duplicate id in List [duplicate]..

As per your needs, use either:

1. map<String, Account> mapAccountId2Account = new map<String, Account>() ;
mapAccountId2Account.add(oAccount .Id, oAccount ) ;
Database.insert(mapAccountId2Account.values(), false) ;

2. setAccount.addAll(listAccountWithDeuplicate) ;

#MySfdcTweets #SalesforceDotCom #SFDC

Saturday 4 July 2015

HTTPS Security Certificate Change from SHA-1 to SHA-256 hash algorithms


Recently Salesforce upgraded security for HTTPS certificate by replacing SHA-1 signature hash algorithm with SHA-256 signed hash algorithm, and CA signed certificates to be only Symantec-issued certificates.

We were in middle of UAT, and Go-Live planned in couple of weeks ahead. Initially the self signed certificate SAP used was SHA-1 signed. There was one scenario where suddenly the connection between SAP and SFDC in all the sandboxes seemed broken resulting in communication error and failure. The most likely reason, which I later figured out in my opinion, was this HTTPS certificate security upgradation. SAP PI, the middleware, had to re-install this new SHA-256 certificate again and restart its XIAdapter to get the connection working.

The only potential change in our case was the middleware SAP PI to update the cached certificate with this new upgrade.

For more details, refer this SFDC knowledge article