Friday, 23 October 2015

Use string.contains in visulaforce page


rendered="{!IF(contains(field.FieldPath, '.'), false, true)}"

Thursday, 17 September 2015

Cron expression for scheduling batch in Salesforce

Cron expression

Every 30 minutes - '0 30 * * * ?'

Every hourly - '0 0 * * * ?'

Every 5 min - '0 5 * * * ?'

Every 10 min - '0 10 * * * ?'

Every 15 min - '0 15 * * * ?'

Every 20 min - '0 20 * * * ?'

Every 25 min - '0 25 * * * ?'

Every 30 min - '0 30 * * * ?'

like upto 55 or 59 your wish

Note: If you schedule your batch for every 5 min '0 5 * * * ?'. It means it will run every hours 5th minute.

It will not run every 5 minutes

If you want to run for every 5 min then you need to schedule 12 times with different different cron expression like for 5 min, 10 min, 15min like upto 60 min..


System.schedule('Job name', '0 30 * * * ?', new controller());

Thursday, 10 September 2015

Beautiful HTML buttons with simple css

<html>
<head>
<style>
 input:hover, button:hover {
    background:white;color:black;
}
.btn{
box-shadow: 10px 10px 5px#888888; width:200px; height:50px; background:black;     color:white; border-radius:5px;
}
</style>
</head>
<body>
<input type="button" value="button" class="btn"></input>
<button class="btn">Home</button>
<button class="btn">Contact</button>
<button class="btn">Help</button>
</body>
</html>

Put cursor at end of text input's value



<input type="text"  style="text-align:right;" onfocus="this.value = this.value;" value="120" autofocus="autofocus">

Example: 


Source: http://stackoverflow.com/questions/17780756/put-cursor-at-end-of-text-inputs-value

Sunday, 6 September 2015

Working with fielsets in salesforce

Working with Field Sets Using Visualforce

<apex:page standardController="Contact">
    <apex:repeat value="{!$ObjectType.Contact.FieldSets.properNames}" var="f"> 
        <apex:outputText value="{!Contact[f]}" /><br/>
    </apex:repeat>
</apex:page>

------------------------------------------------------------------------------------

Working with Field Sets Using Apex


public class MerchandiseDetails {

    public Merchandise__c merch { get; set; }
    
    public MerchandiseDetails() {
        this.merch = getMerchandise();
    }

    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.Merchandise__c.FieldSets.Dimensions.getFields();
    }

    private Merchandise__c getMerchandise() {
        String query = 'SELECT ';
        for(Schema.FieldSetMember f : this.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id, Name FROM Merchandise__c LIMIT 1';
        return Database.query(query);
    }
}

Global Describe in salesforce



Map<String, Schema.SObjectType> mapSchemaSobjectType = Schema.getGlobalDescribe();

Set<String> setSobjets = new Set<String>();

for(Schema.SObjectType d :mapSchemaSobjectType.values())
{
       Schema.DescribeSObjectResult dSobject = d.getDescribe();
       setSobjets.add(dSobject.getName());
}

setSobjets This set will contain all the SObject name.

-----------------------------------------------------------------------------------------------------

Map<String, Schema.SObjectType> mapSchemaSObjectType = Schema.getGlobalDescribe();
Set<String> standardObjects = new Set<String>();
Set<String> customObjects = new Set<String>();
for(Schema.SObjectType objSchemaSobjectType : mapSchemaSObjectType.values())
{
    Schema.DescribeSObjectResult objResult = objSchemaSobjectType.getDescribe();
    if(!objResult.isCreateable())
      continue;
    if(objResult.isCustom() == false && objResult.getRecordTypeInfos().size() > 0)
        standardObjects.add(objResult.getName());
    else if(objResult.isCustom())
        customObjects.add(objResult.getName());
}

standardObjects - Contains set of standard objects.
customObjects - Contains set of Custom objects

Override SObject list view with Visualforce page


Create a visualforce page with standard controller and recordSetVar attribute of <apex:page>

Based on your Sobject change the StandardCopntroller and recordsetvar value.


<apex:page recordsetvar="Accounts" standardcontroller="account" tabstyle="account">
  <apex:enhancedlist customizable="True" height="600" id="YourListViewId" rowsperpage="10" type="Account">
  </apex:enhancedlist>
</apex:page>


Once visualforce page is created.
Goto Sobject detail page and goto Buttons, Links, and Actions section.
Now click edit next to list now you can see like below image


Now check radio button next to Visualforce page and select your visualforce page.
final save the changes.

Now your Sobject list view is overridden.





Add a list view button for deleting records in Salesforce



Create a javascript list view button











{!requirescript("/soap/ajax/28.0/connection.js")}
{!requirescript("/soap/ajax/28.0/apex.js")}

sforce.connection.deleteIds({!GETRECORDIDS($ObjectType.Account)});
location.reload();

 
 Instead of Account use your object name and add this button in object's search layout --> list view.

Sunday, 21 June 2015

Execute Territory Assignment Rules when update or insert Accounts.


Account Assignment Rules in Salesforce.com via webservice

To run the Territory rule assignment
We need to use webservice call out

To do this, we must import the partner wsdl and generate helper apex classes.

http://developer.force.com/cookbook/recipe/calling-salesforce-web-services-using-apex


And then after insert or update of Account, pass the all accounts in fireTerritoryManagementViaSoap method.

global class TerritoryMangRule
{    
    webService static void fireTerritoryManagementViaSoap(List<Account> lstAccountToUpdate)
    {
        partnerSoapSforceCom.Soap obj = new partnerSoapSforceCom.Soap();
        partnerSoapSforceCom.LoginResult loginResult = obj.login('org@username.com', 'Password');  
        obj.SessionHeader = new partnerSoapSforceCom.SessionHeader_element();
        obj.endpoint_x =loginResult.ServerUrl;
        obj.Sessionheader.sessionid = loginResult.sessionid;
        List<sobjectPartnerSoapSforceCom.sObject_x> lst = new List<sobjectPartnerSoapSforceCom.sObject_x>();
        for(Account ac:lstAccountToUpdate)
        {
            sobjectPartnerSoapSforceCom.sObject_x tmpObj = new sobjectPartnerSoapSforceCom.sObject_x();
            tmpObj.type_x = 'Account';
            tmpObj.Id = ac.Id;
            lst.add(tmpObj);
        }
        partnerSoapSforceCom.AssignmentRuleHeader_element  obj1 = new partnerSoapSforceCom.AssignmentRuleHeader_element();
        obj1.useDefaultRule = true;
        obj.AssignmentRuleHeader = obj1;
        partnerSoapSforceCom.SaveResult[] lst1 =obj.update_x(lst);
    }
}

Google Static map


Simple map
Documentation: https://developers.google.com/maps/documentation/staticmaps/

<img src="https://maps.googleapis.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400">
</img>


-------------------------------------------

Map with

<img src="https://maps.googleapis.com/maps/api/staticmap?size=512x512&maptype=roadmap\&markers=size:mid%7Ccolor:red%7CMumbai,Maharastra%7CPune,Maharastra%7CNavi+mumbai,Maharastra">


------------------------------------------------------------------------------------------
Map with route


<img src="https://maps.googleapis.com/maps/api/staticmap?size=512x512&maptype=roadmap\&markers=size:mid%7Ccolor:red%7CMumbai,Maharastra%7CPune,Maharastra%7CNavi+mumbai,Maharastra\&path=Mumbai|Navi+Mumbai|pune">




With diffrent color

<img src="https://maps.googleapis.com/maps/api/staticmap?size=1024x600&maptype=roadmap\&markers=size:mid%7Ccolor:red%7CMumbai,Maharastra%7CPune,Maharastra%7CNavi+mumbai,Maharastra\&path=weight:10%7Ccolor:red%7CMumbai|Navi+Mumbai|pune">

Print Google map with direction

First create google map with direction
Follow below link
http://sforcedc.blogspot.in/2015/06/google-map-and-direction-api-with.html

then convert google map into canvas using HTML2Canvas plugin

http://sforcedc.blogspot.in/2015/06/visualforce-page-screenshots-with.html

after that print the page.

Visualforce page

<apex:page showHeader="false">
    <input type="button" value="Print" onclick="javascript:this.style.display='none';window.print();this.style.display='';" />
 
    <div style="width: 100%;" id="mapDiv">
         <div id="map" style="width: 70%; height: 600px; float: left;margin:0px;color:black;"></div>
         <div id="panel" style="width: 30%; float: left;margin:0px;"></div>
    </div>
 
 
    <script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=true"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
 
    <script type="text/javascript">
        window.onload = function(){
            var directionsService = new google.maps.DirectionsService();
            var directionsDisplay = new google.maps.DirectionsRenderer();
         
            var map = new google.maps.Map(document.getElementById('map'), {
                zoom:7,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
         
            directionsDisplay.setMap(map);
            directionsDisplay.setPanel(document.getElementById('panel'));
         
            var request = {
                origin: 'Mumbai',
                destination: 'Navi Mumbai',
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };
         
            directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                }
            });
            setTimeout(myfunction, 4000);
        }
        function myfunction()
        {
            html2canvas(document.body,
            {
                useCORS: true,
                onrendered: function(canvas)
                {
                    document.body.appendChild(canvas);
                    document.getElementById("mapDiv").style.display = 'none';
                }
            });
        }
   </script>
</apex:page>

Visualforce page screenshots with JavaScript - html2canvas

html2canvas



This script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

In Visualforce page
<apex:page showHeader="false">
<script src="//cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<div>
    Hello world !
</div>

<script>
html2canvas(document.body, 
 {
    useCORS: true,
    onrendered: function(canvas) 
    {
        document.body.appendChild(canvas);
    }
 })
</script>
</apex>

Using Parallax JS

Parallax.js is a dirt simple parallax scrolling effect inspired by Spotify.com and implemented as a jQuery plugin.


Parallax Picture Scrolling


Basic scrolling parallax text 






Google map and Direction API with Visualforce page

Google map API : https://developers.google.com/maps/documentation/javascript/tutorial

Google Direction API: https://developers.google.com/maps/documentation/directions/

Demo: http://jsfiddle.net/vc1zwft0/

Same i want to use in salesforce with dynamic addresses.

So This is the code.

Implement same and write some logic in controller side.

<apex:page showHeader="false" >
<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
   <title>Google Maps API v3 Directions Example</title>
   <script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=true"></script>
</head>
<body style="font-family: Arial; font-size: 12px;">

    <div style="width: 100%;" id="mapDiv">
        <div id="map" style="width: 70%; height: 600px; float: left;margin:0px;color:black;"></div>
        <div id="panel" style="width: 30%; float: left;margin:0px;"></div>
    </div>
    <script type="text/javascript">

     var directionsService = new google.maps.DirectionsService();
     var directionsDisplay = new google.maps.DirectionsRenderer();

     var map = new google.maps.Map(document.getElementById('map'), {
               zoom:7,
               mapTypeId: google.maps.MapTypeId.ROADMAP
     });

     directionsDisplay.setMap(map);
     directionsDisplay.setPanel(document.getElementById('panel'));

     var request = {
       origin: 'Mumbai',
       destination: 'Navi Mumbai',
       travelMode: google.maps.DirectionsTravelMode.DRIVING
     };

     directionsService.route(request, function(response, status) {
       if (status == google.maps.DirectionsStatus.OK) {
         directionsDisplay.setDirections(response);
       }
     });
   </script>
</body>
</apex:page>


Here instead of using hard coded value you can create string at controller level and pass here.

origin: {!start address }
destination:- {end address}

Source :- http://stackoverflow.com/questions/3896871/google-map-driving-direction-source-code-for-their-example