Play Games

Search This Blog

Friday, April 28, 2017

Unsupported MetaData Types using MetaData API

We cannot retrieve or deploy the following metadata types using MetaData API
1)Case Contact Roles
2)Case Feed Layouts
3)Case Team Roles
4)Console Layouts
5)Email Services
6)Lead Settings
7)Site.com
8)Web-to-Lead and many more.
Go through this Linkfor full list.

Loading Test Data in test class from static resource in Salesforce.

Step 1: create a .csv file for the records you want to load.
If you want to load Account records,then in the first row,you need to give field names(Api Name only) and then you can add as many rows(records) you want to add.

Step 2: Create a static resource and then upload that .csv file(for example:Accounts)

Step 3: In your test class,you can use Test.loadData() to populate test Accounts.
Sample Code:
@isTest
private class TestingLOadData {
     static testmethod void testLoadData() {
        // Using Test.loadData method,we are loading Account records.
        List<sObject> lstAccount = Test.loadData(Account.sObjectType, 'Accounts');
        Account objAccount1 = (Account)lstAccount[0];
        System.debug('Account Record:'+objAccount1);
    }
}
Output: Once you run this test class,you can check debug logs

Saturday, April 22, 2017

How to start and stop Timer/Counter in visualforce page

Sample Code:
<apex:page  sidebar="false" showHeader="false" id="page1">
<apex:form id="form1">
<script>
var startTimer;
function stopTime() {
    clearInterval(startTimer);
}
function startTime() {
    var sec = 0;
    startTimer = setInterval(
    function () {
        sec = sec + 1;
        document.getElementById("displayTime").innerHTML =  sec + " Seconds!!!";
    }, 1000);
}
</script>
<body >
<div><b>Timer :</b><b id="displayTime" style="Color:red;"></b></div>
<input type="button" value="Start Timer" onclick="startTime()"/>
<input type="button" value="Stop Timer" onclick="stopTime()"/>
</body>
</apex:form>
</apex:page>
Output:


My first simple Game developed in Salesforce - Salesforce Globe 4u



Do you want to play ? Click Here
Source Code:
Step 1: Create Apex Class.
public class MemoryCheckController {
    public integer counter {get;set;}
    public integer totalCleared {get;set;}
    public Integer secCount {get;set;}

    public Set<Integer> alreadyUsed {get;set;}
    public List<Integer> shuffledNumbersByCh {get;set;}
    Public List<List<Integer>> numberList {get;set;}
    public MemoryCheckController () {
        totalCleared = 1;
        shuffleNumbers();
    }
    public void shuffleNumbers() {
        //set the range of numbers from to N
        Integer lsitSize = 25;
        alreadyUsed= new Set<Integer>();
        shuffledNumbersByCh = new List<Integer>();
        while(shuffledNumbersByCh .size() < lsitSize){
            shuffledNumbersByCh .add(getRandom(lsitSize));
        }
        for(integer i=0; i< shuffledNumbersByCh.size(); i++) {
            shuffledNumbersByCh[i] = shuffledNumbersByCh[i]+1;
        }
        system.debug('Final shuffled Numbers'+shuffledNumbersByCh );
        numberList = new List<List<Integer>>();
        List<Integer> rowList = new List<Integer>();
        Integer j =0;
        for(integer i =0; i<=24; i++) {
            J= i+1;
            rowList.add(shuffledNumbersByCh[i]);
            if(Math.Mod(j,5) ==0) {
                numberList.add(rowList);
                rowList = new List<Integer>();
            }
        }
    }
    public Integer getRandom(Integer shuffledMaxNumber){
        Integer randomNum;
        do {
            randomNum = (math.random() * shuffledMaxNumber).intValue();
        } while (alreadyUsed.contains(randomNum));
        alreadyUsed.add(randomNum);
        return randomNum;
    }
    public void updateCount() {
        counter = counter +1;
        totalCleared = totalCleared +1;
        system.debug('counter counter '+counter );
    }
}
Step 2: Create visualforce Page
<apex:page controller="MemoryCheckController" sidebar="false" showHeader="false" id="page1">
<apex:form id="form1">
<style>
h1 {
    color: red;
}
.grid-cell {
    width: 50px;
    height: 50px;
    border-radius: 3px;
    text-align:center;
    text-color:red;
    background: rgb(82, 128, 201);
}
</style>
<script>
var startTimer;
function fun(rowIndex,columnIndex) {
    var myTable = document.getElementById('NumberTable');
    var previousValue = document.getElementById('page1:form1:displayCounter').value;
    var currentValue = myTable.rows[rowIndex].cells[columnIndex].innerHTML ;
    if(currentValue == 1) {
        startTime();
    }
    if(currentValue == +previousValue +1) {
        myTable.rows[rowIndex].cells[columnIndex].innerHTML = '';
        var totalClear = document.getElementById('page1:form1:displayCounter1').value;
        if(totalClear == 25) {
            var sec = document.getElementById("displayTime").innerHTML;
            document.getElementById("displayTime").innerHTML = "Great ..You cleared all the numbers in "+sec+". Refresh the Browser Url to start new Game";
            stopTime();
        }
        updateCounter();
    }
 
}
function stopTime() {
    clearInterval(startTimer);
}
function startTime() {
    var sec = 0;
    startTimer = setInterval(
    function () {
        sec = sec + 1;
        document.getElementById("displayTime").innerHTML =  sec + " Seconds!!!";
    }, 1000);
}
</script>
<body >
<table  border="1" cellpadding="0" cellspacing ="0" id="table1" style="padding-left:5px;">
<tr><td>
<table >
<tr><td >
<b style="color:red;">Game Instructions : </b>Touch all the numbers from 1 to 25 in Sequence to empty this Grid
The Counter will get started as soon as you start the game.Try to finish as early as possible.
</td><td><apex:image url="{!$Resource.KSK}" width="100%" height="70%"/></td>
</tr>
</table>
</td></tr>
<tr><td>Timer :<b id="displayTime" style="Color:red;"></b></td></tr>
<tr><td>
<table border="1" cellpadding="0" cellspacing = "0" id="NumberTable" width="100%" >
<apex:variable var="rowIndex" value="{!0}"/>
<apex:repeat value="{!numberList}" var="rowNums">
<tr style="border:1px"><apex:variable var="columnIndex" value="{!0}"/>
    <apex:repeat value="{!rowNums}" var="num">
        <td class="grid-cell" onClick="fun('{!rowIndex}','{!columnIndex}')"> {!num}</td>
        <apex:variable var="columnIndex" value="{!columnIndex+1}"/>
    </apex:repeat><apex:variable var="rowIndex" value="{!rowIndex+1}"/>
</tr>
</apex:repeat>
</table>
</td></tr></table>

<apex:inputtext value="{!counter}" id="displayCounter" style="display:none;"/>
<apex:inputtext value="{!totalCleared}" id="displayCounter1" style="display:none;"/>
</body>
<button onclick="window.location.reload()" id="startGame" style="display:none;">Start New Game</button>
<apex:actionFunction action="{!updateCount}" name="updateCounter" rerender="displayCounter,displayCounter1">
</apex:actionFunction>
</apex:form>
</apex:page>
Step3 : Create one public site in Salesforce and add vf page and controller to it.
Thats it.You can get that vf page url to play the simple Game.
Output :





Thursday, April 20, 2017

Increment Apex:variable in Visualforce Page

Solution:
Intitialize apex:variable Index like this <apex:variable var="Index" value="{!0}"/>.
To Increment Index variable by 1 use the following code
<apex:variable var="Index" value="{!Index+1}"/>
Example:
Visualforce Page:
<apex:page>
<body>
<apex:variable var="Index" value="{!0}"/>
Apex Variable Index initial Value:{!Index} <br/>
<apex:variable var="Index" value="{!Index+1}"/>
Apex Variable Index value after Incrementing:{!Index}
</body>
</apex:page>
Output:


Accessing Static Resource image in Visualforce Page

If you upload image as static resource with static resource name as "MyCompanyLogo",then you can access it like this
<apex:image url="{!$Resource.MyCompanyLogo}" width="50" height="50"/>
Example: 
Visualforce Page:
<apex:page>
Here is your Company Logo <br/>
<apex:image url="{!$Resource.MyCompanyLogo}" width="250" height="250"/>
</apex:page>
Output:

Wednesday, April 19, 2017

Display Numbers from 1 to 25 in a table with 5 elements in each Row in Visualforce Page.

Apex Class:
public class DisplayNumberController {
    Public List<List<Integer>> numberList {get;set;}
    public DisplayNumberController () {
        numberList = new List<List<Integer>>();
        List<Integer> rowList = new List<Integer>();
        for(integer i =1; i<=25; i++) {
            rowList.add(i);
            if(Math.Mod(i,5) ==0) {
                numberList.add(rowList);
                rowList = new List<Integer>();
            }
        }
        system.debug('DisplayNumbers:'+numberList);
    }
}
Visualforce Page:
<apex:page controller="DisplayNumberController">
<style>
h1 {
    color: red;
}
.grid-cell {
    width: 50px;
    height: 50px;
    border-radius: 3px;
    text-align:center;
    text-color:red;
    background: rgb(82, 128, 201);
}
</style>
<body>
<table border="1" cellpadding="0" cellspacing = "0">
<apex:repeat value="{!numberList}" var="rowNums">
<tr>
    <apex:repeat value="{!rowNums}" var="num">
        <td class="grid-cell"> <h1>{!num}</h1></td>
    </apex:repeat>
</tr>
</apex:repeat>
</table>
</body>
</apex:page>
Output: