Ads

Showing posts with label SOQL. Show all posts
Showing posts with label SOQL. Show all posts

Thursday 22 August 2013

How to undelete records in Salesforce?

Salesforce keep record you delete to Recycle Bin for 15 days with maximum record of 25 times the Megabytes (MBs) in your storage. For example, if your organization has 1 GB of storage then your limit is 25 times 1000 MB or 25,000 records. If your organization reaches its Recycle Bin limit, Salesforce automatically removes the oldest records if they have been in the Recycle Bin for at least two hours.

You can manually undelete records by click Recycle Bin icon in bottom left menu. Undelete account will include contact, case, opportunity tagged to that Account. But, if you have many records or you have specific record Id or you want to undelete records with some conditions, manually delete is not an option, use Apex code for it.

You not really need to be good developer to utilize Apex code. Here we go:

1. Open Developer Console
From your name in top right, look for Developer Console











2.From Developer Console
Go to Debug menu and select Open Execute Anonymous Window











3. Enter SOQL 
Example:
Account[] a = [SELECT Id FROM Account WHERE name = 'singtel' ALL ROWS];
undelete a;
Important to add ALL ROWS, otherwise deleted records will be not return on query.






4. Monitor Apex Debug Log
Go to Monitor - Logs - Debug Logs (in Salesforce menu, not Developer console)
Add user to monitor, default it will capture 20 log request for the users

5. Execute 
Click Execute button in Enter Apex Code window

6. Back to Debug Log
Select latest debug log and click View to monitor

Saturday 25 May 2013

How to update record using SOQL?

Example:

Account act = [SELECT Id, Description__c FROM Account WHERE id = '001A000000vtDZo'];
act.Description__c = 'Fully product based account';

Update act;

Friday 3 May 2013

Like operator in SOQL query in JAVA and C#

String sql = 'SELECT Name,Id FROM Account WHERE Name Like '%' + acctName + '%'';

Here 'acctName' is a variable.

Tuesday 16 April 2013

SOQL Statements

SOQL statements estimate to a list of sales force Objects, a single sObject, or an Integer for count method queries. For example, we can get details of Account named as ‘Acme’.
SOQL: List accObject = [select id, name from account where name = 'Acme'];
From this list, you can access individual elements.
Code:
if (!accObject.isEmpty()) {
// Execute commands
}
Description:
As above code checks the list of Account object contains value. Following example of code creates a new contact for the first account with the number of employees greater than 10:
Code:
Contact c = new Contact (account = [select name, address from account where NumberofEmployees > 5 limit 1]);
c.FirstName = 'Raees';
c.LastName = 'Ahmed';
insert c;
Description:
Above code inserting contacts where account of employee greater than 5 and limit is 1.
Working with SOQL Aggregate Functions
Aggregate functions in SOQL as same as in SQL, such as SUM () and MAX (), allow you to roll up and summarize your data in the query.
SOQL:
AggregateResult[] groupResult = [SELECT AVG(Amount)aver FROM Opportunity];
Object avgAmount = groupedResults[0].get('aver');
Description:
Note that any query that includes the aggregate function returns its results in an array of AggregateResult objects. AggregateResult is a read-only sObject and the only used for query results.
The following example demonstrates a SOQL query for loop used to mass update records. Suppose you want to change last name of contact across all records of contact:
Code:
public void masUpdate() {
for (List contacts:[Select FirstName, LastName From Contact]) {
if (contacts.FirstName == 'ryan' && contacts.LastName == 'ander') {
contacts.LastName = 'raees';
}//end of if
} //end for
update contacts; //update contact
} // end of class

SOQL Advance Features?

1. Group by feature:
Salesforce Developers are the aware of with “GROUP BY” word in SQL (Stucture Query Language)/SOQL (Salesforce Object Query Language). This key word returns count of records which are selected.
We can put the count to filter the records. My requirement is that if email is duplicates in contact’s record then I need to exclude those records. Following is the code for that, may it will help you too.
SOQL Code:
AggregateResult[] groupedRs = [Select c.Email from Contact c where email =emailed@gmail.com’ group by Email having count(Email)<2];
Description:
As above SOQL checks the email address and group by Email field count is less than 2. So using the above SOQL we could checks how much records are fetching in result set.
2. Salesforce Object initialization feature:
Up till now process to update any sobject was to first query that object using its Ids, and than we used to update that list.
Now if we are having Id of sobject than we need not do any SOQL. For ex-
Code:
Opportunity tempObj = new Opportunity (id = '0063000000Xh92q');
tempObj.name='Test 123';
Update tempObj;
Description:
This above code will update opportunity with Id=’ '0063000000Xh92q'’. Using this approach we can save many SOQL queries.

SOQL Injection?

SOQL injection is a technique by which user effects your application to execute the database methods and you did not intend by passing SOQL statements into your script. Means any user can hack your Database or do fake login in to your secure account without any knowing your password.
This occur in an Apex script whenever your application relies on end user input to the construct a dynamic SOQL statement and you do not handle the input properly. This is the most secure thing you should know about your code of
To prevent SOQL injection, use the escapeSingleQuotes (like ‘\’) method in the Dynamic SOQL. This method adds the escape character (\) to all single quotation marks in a string that is passed in from any user. The method ensures that all single quotation marks are treated as enclosing strings, instead of database commands.
Code:
public Account[] getAccountInfo() {
    String userInput = Apexpages.currentPage().getParameters().get('nameofAccount');
    Account[] accs = database.query('SELECT name,address,city FROM Account WHERE name = \'' + userInput + '\'');
    return accs;
}
Description:
Above code explain it self user enters Account name and Dynamic SOQL used this name and returns the information about Account.
However if there is hacker user enter Account name like ‘Accoun1’ or ‘xxxxx’ so he can get your secure Account information. We can prevent this write the Class as “with sharing”.