Sunday, 13 August 2017

                   Remove duplicate items from a List:

Today I'am going to demonstrate how to remove duplicate values in List. Please add below code in your anonymous window and check how it works.

List<integer> orginalresult=new List<integer>();
orginalresult.add(1); // 0th position
orginalresult.add(2);//1st position
orginalresult.add(2);
orginalresult.add(10);
orginalresult.add(11);
orginalresult.add(3);
orginalresult.add(4);
orginalresult.add(4);
orginalresult.add(1);
orginalresult.add(5);
system.debug('@@@ originalList@@@'+orginalresult);
set<integer> setresult=new set<integer>();
List<integer> Listresult=new List<integer>();
setresult.addAll(orginalresult);
system.debug(setresult);
Listresult.addAll(setresult);
system.debug(Listresult);

Problem: when converted to a Set and then back to a List using addAll(), the order doesn't remain.
Solution:
//To maintain order, you need to iterate over the elements:
for(integer s:orginalresult){
    if(setresult.add(s)){
        Listresult.add(s);
    }
    }
system.debug('@@@ListResult @@@@'+Listresult);   

Wednesday, 2 August 2017

Using Apex:Variable in Visualforce to control rendering of HTML Markup


We mostly use apex:variable tag for declaring complex expressions or variables in visual force page. But the same can be used very easily to control the rendering too.



<apex:page controller="ApexVarController1">
<div>
        <!-- apex:variable tag to control the rendering for Markup --> 
<apex:variable
value="anything will go here" var="flag"
rendered="{!flag}">
<h1> Flag value is true </h1><br />
</apex:variable> 
<!-- apex:variable tag used to not render for a Markup --> 
<apex:variable value="anything will go here" var="flag1" rendered="{!flag1}">
<h1>Flag value is false</h1><br />
</apex:variable>
</div>
</apex:page>
--------------------------------------------------------------------------------------------------
public class ApexVarController1 {
    public boolean flag { get;set;}
    public boolean flag1{get;set;}
    
    public ApexVarController1 (){
       flag=true;
       flag1=false;
    }

}
--------------------------------------------------------------------------------------------------------

Sunday, 16 July 2017

                                    FREEZE USERS IN SALESFORCE

When administering Salesforce, you can at times run into scenarios where a particular users’s account should no longer have access. An example of that is when you have reason to believe that a user’s account has been compromised or someone has just left the company and you want to prevent them from logging in. Normally you would just disable the user; however, if the user is part of a custom heirachary field - there are more steps involved. A nice solution in the meantime us to just use the "Freeze" function.

After the user is frozen - the Freeze button changes to an Unfreeze button which performs (well, you get the idea).
Note: When you freeze a user, it does not free up a user license. In order to actually free up a license you have to perform an actual deactivation of the user.


For more Information please refer  Link:
                      http://blog.shivanathd.com/2013/10/Freeze-User-In-Salesforce.html

Thursday, 13 July 2017

Tricky sales force Questions 

1. How to delete the User from Salesforce
  salesforce does not allow to delete any user, however you can deactivate the user.

2.If one object in Salesforce have 2 triggers which runs “before insert”. Is there any way to control the sequence of execution of these triggers?

 Salesforce.com has documented that trigger sequence cannot be predefined. As a best practice create one trigger per object and use comment blocks to separate different logic blocks. By having all logic in one trigger you may also be able to optimize on your SOQL queries.

3.How to enable truncate custom object feature in Salesforce ?
Ans : Navigate to “App Setup | User Interface” and select “Enable Custom Object Truncate”.

Sometimes while deleting record it gives error “Object cannot be Deleted”. What is the reason for this kind of error ?

This is generic error message prompted by Salesforce many times, which is not well informative. To get informative message, we can try to delete same record in “Developer Console”. In Developer Console Debug log, we will get exact error message.

Interesting !!!

Example:
Lets say there is one record which is parent of more than 2000 records and grand parent of 5000 records. In such scenario from developer console it gives error something like “record cannot be deleted because it has many associated objects” However in User Interface, it will just display that “Object cannot be deleted."


4.Why CSS is not working in PDF created by Visualforce ?

1.Use “apex:stylesheet” tag to import external CSS file
2.Wrap “Style” tag inside “Head” tag in Visualforce
You have to cross verify the things.If you mentions these propery your css should work

5.whats Test.isRunningTest() ?
Use Test.isRunningTest() in your code to identify that context of class is Test or not. You can use this condition with OR (||) to allow test classes to enter inside code bock. It is very handy while testing for webservices, we can generate fake response easily.


                                                                                                                          Continues ...

Converting Lowercase string to Uppercase & Upper case string to Lower case

/* Here I'am converting lower case to Uppercase  */

string x='ajay';
string uppercase=x.touppercase();     //Converting string to Upper case
system.debug('uppercase ====>'+uppercase);

/* Here I'am converting Uppercase to lower case */
string y='SACHIN';
string Lowercase=y.tolowercase();
system.debug('lowercase====>'+Lowercase);

How to get all Salesforce records even from recycle bin or Achieved Activities using SOQL query?


we cannot do it directly within the Query Editor of developer console.If I run the query in query editor what will happen?!!....I hope you got the answer. But there's a workaround for we can test it  in Developer Console. The workaround is to use the "Execute Anonymous Window".

1. Open "Enter Apex Code" window by selecting "Debug" -> "Open Execute Anonymous Window" menu
2. Enter your query there and output the result to the log. Check the "Open Log" checkbox.
List<contact> clist = [SELECT name FROM contact ALL ROWS];
System.debug(JSON.serialize(clist));
3. Click "Execute" to run your code and check the log file for your result.

Special Note:
               Deleted data is stored in the Recycle Bin for 15 days.


Interesting Link:
                  https://salesforce.stackexchange.com/questions/4020/whats-the-actual-meaning-of-all-rows-in-soql

Wednesday, 5 July 2017

 Tricky Error : Cannot Create Master-Detail Relationship

                                        Great things are done by a series of small things brought together. 
                                                                                                                         -Vincent Van Gogh


Some times we will get below error message while creating master detail relationship..

whats that error massage?

"You cannot create a new Master-Detail relationship on an existing custom object if records already exist. You must first create a Lookup relationship, populate the lookup field with data in all records, and then change the relationship type to Master-Detail. "

Have you encounter this error before then its great!!!!. Today I'am going to explain solution about that error

Tricky solution:

Master - Detail relationship requires that the Detail Record (Child) ALWAYS have a Master (Parent) record since the detail record inherits security and ownership from the parent record.
Therefore, if you want to create a Master - Detail relationship between existing objects, you need to make sure that all the existing records for the child object have a lookup value to the parent object BEFORE you can create the Master Detail relationship.

1. Create a lookup from the child object to the parent object.
2. Populate ALL the records with a valid lookup value to the parent.
3. Change the Lookup relationship to a Master - Detail relationship. This is only allowed if ALL RECORDS HAVE A VALID LOOKUP.

Use this query to confirm to check if child record exist arent:
SELECT count()  from Child_object__c;


For detailed Information Visit Below Link:
http://www.tutorialkart.com/salesforce/cannot-create-master-detail-relationship/