Skip to content

Instantly share code, notes, and snippets.

@lkatney
lkatney / Randomize a list.cls
Last active October 22, 2020 17:20
Randomize a list through apex
public static list<wrapper> randomize(list<wrapper> lst){
integer currentIndex = lst.size();
wrapper temporaryValue;
integer randomIndex;
// While there remain elements to shuffle...
while (0 != currentIndex) {
// Pick a remaining element...
randomIndex = integer.valueOf(Math.floor(Math.random() * currentIndex));
currentIndex -= 1;
// And swap it with the current element.
@lkatney
lkatney / Formula for n months
Created November 17, 2014 07:35
Formula to calculate n months from date and give actual date - Salesforce
DATE(YEAR(DATEVALUE(Custom_date_field__c)),MONTH(DATEVALUE(Custom_date_field__c)),DAY(DATEVALUE(Custom_date_field__c))) - VALUE(TEXT(DATE(YEAR(DATEVALUE(Custom_date_field__c)),MONTH(DATEVALUE(Custom_date_field__c)),1) - DATE(IF(MONTH(DATEVALUE(Custom_date_field__c))+ 3 &lt;= 12 ,YEAR(DATEVALUE(Custom_date_field__c)) , YEAR(DATEVALUE(Custom_date_field__c))+ 1), IF(MONTH(DATEVALUE(Custom_date_field__c))+ 3 &lt;= 12 , MONTH(DATEVALUE(Custom_date_field__c))+ 3,MONTH(DATEVALUE(Custom_date_field__c))+ 3 - 12),1)))
@lkatney
lkatney / statement for conversion.cls
Last active August 29, 2015 14:09
Convert Unix timestamp to Apex datetime
string unixDatetime = '1387033750000';
system.debug(datetime.newinstance(long.valueOf(unixDateTime)));
@lkatney
lkatney / Apex rest with JSONP.cls
Last active August 29, 2015 14:09
Apex rest service to send JSONP response
@RestResource(urlMapping='/Accounts/*')
global class AccountsService {
@HttpGet
global static void getAccounts() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String callback = req.params.get('callback');
String result;
@lkatney
lkatney / Javascript with apexrest - JSONP.js
Last active August 29, 2015 14:09
JavaScript to hit salesforce apex rest that will return JSONP response
<Script>
$j(document).ready(function() {
//call out to salesforce
$j.ajax({
type: 'GET',
url: 'https://XXXX.secure.force.com/services/apexrest/accounts?callback=processData',
async: false,
jsonpCallback: 'jsonCallback',
@lkatney
lkatney / Email Address Check - Regex.cls
Created November 18, 2014 08:58
Method with regex to check email format - Apex
public Boolean checkValidUsername(String emailAddress){
String emailRegex = '([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';
Pattern MyPattern = Pattern.compile(emailRegex);
Matcher MyMatcher = MyPattern.matcher(emailAddress);
if(MyMatcher.matches()){
return true;
}else{
return false;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Prior 32.0v
String comment = 'Click to vote/demote this idea';
// Create feed item input
ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
feedItemInput.body = new ConnectApi.MessageBodyInput();
// Create text segment to hold the message body
List<ConnectApi.MessageSegmentInput> segments = new List<ConnectApi.MessageSegmentInput>();
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
@lkatney
lkatney / callout method.cls
Last active August 29, 2015 14:15
Method to make callouts handling PATCH request
/*********************************************************************************
@description : method to make https callouts
@param : url, method, body , headers
@return : HttpResponse
**********************************************************************************/
public static HttpResponse makeCallout(String url, String method, String body, Map<String,String> headers){
HttpRequest req = new HttpRequest();
HTTPResponse res = new HTTPResponse();
Http http = new Http();
<!--twitter -->
<a class="icon icon-social-twitter" href="https://twitter.com/intent/tweet?url={{url absolute="true"}};text={{title}} » " alt="Tweet '{{title}}'" target="_blank">
</a>
<!-- Facebook -->
<a class="icon icon-social-facebook" href="https://www.facebook.com/sharer/sharer.php?u={{url absolute="true"}}" alt="Share '{{title}}'" target="_blank">
</a>
<!-- Linkedin -->
<a class="icon icon-social-linkedin" href="https://www.linkedin.com/shareArticle?mini=true&url={{url absolute="true"}}&title={{title}}&summary={{meta_description}}" alt="Share '{{title}}'" target="_blank">
</a>
<!-- Google+ -->
<apex:page >
<!-- USE YOUR OWN BUTTON & DEPLOYMENT CODE HERE -->
<!-- button code -->
<img id="liveagent_button_online_573280000008OUC" style="display: none; border: 0px none; cursor: pointer" onclick="liveagent.startChatWithWindow('573280000008OUC', 'chat-window')" src="https://lakshay37demo-developer-edition.ap2.force.com/resource/1430373426000/Online_Chat_Button" /><img id="liveagent_button_offline_573280000008OUC" style="display: none; border: 0px none; " src="https://lakshay37demo-developer-edition.ap2.force.com/resource/1430373447000/Offline_Chat_Button" />
<script type="text/javascript">
if (!window._laq) { window._laq = []; }
window._laq.push(function(){liveagent.showWhenOnline('573280000008OUC', document.getElementById('liveagent_button_online_573280000008OUC'));
liveagent.showWhenOffline('573280000008OUC', document.getElementById('liveagent_button_offline_573280000008OUC'));
});