Salesforce.com Blog: How to Unit Test Recieving Email in Apex: Suppose you have a class like this for recieving email /** * Email services are automated processes that use Apex classes * to process t...
Hello....
Ajax
(1)
Apex Class
(12)
Apex Trigger
(2)
Community
(2)
Home Page
(1)
HTML
(4)
Integration
(3)
JS
(7)
KB
(1)
Label
(1)
Licenses
(1)
Listing
(1)
Log
(1)
OOPs
(5)
Sharing
(1)
Static Resource
(1)
Test Class
(3)
URI
(1)
Visualforce
(10)
Friday 28 February 2014
Wednesday 26 February 2014
Method for updating Lead Rating Count on Custom Object
/*Method for updating Lead Rating Count on Custom Object names 'Configration'*/
- public void leadRatingCount(Map<id,Lead> triggerNewMap)
- {
- Map<String,Integer> objMap=new Map<String,Integer>{'Hot'=>0,'Warm'=>0,'Cold'=>0};
- for(Lead objLead:triggerNewMap.Values())
- {
- objMap.put(objLead.rating,objMap.get(objLead.Rating)+1);
- }
- Configration__c objConfigration=[select Hot_Leads__c,Warm_Leads__c,Cold_Leads__c from Configration__c limit 1];
- System.Debug('Hot value '+objConfigration.Hot_Leads__c+'plus'+objmap.get('Hot'));
- objConfigration.Hot_Leads__c +=objmap.get('Hot');
- objConfigration.Warm_Leads__c +=objmap.get('Warm');
- objConfigration.Cold_Leads__c +=objmap.get('Cold');
- update objConfigration;
- }
Tuesday 25 February 2014
Workflow
What is Workflow?
Workflow automates the following types of actions based on your
organization's processes:
Tasks--Assign a new task to a user, role, or record owner.
Email Alerts--Send an email to one or more recipients you specify.
Field Updates--Update the value of a field on a record.
Outbound Messages--Send a secure, configurable API message (in XML
format) to a designated listener.
For example, workflow can:
Assign follow-up tasks to a support rep one week after a case is updated.
Send sales management an email alert when a sales rep qualifies a large deal.
Change the Owner field on a contract three days before it expires.
Trigger an outbound API message to an external HR system to initiate
the reimbursement process for an approved expense report.
Each workflow rule consists of:
Criteria that cause the workflow rule to run.
Immediate actions that execute when a record matches the criteria. For
example, salesforce.com can automatically send an email that notifies
the account team when a new high-value opportunity is created.
Time-dependent actions that queue when a record matches the criteria,
and execute according to time triggers. For example, salesforce.com
can automatically send an email reminder to the account team if a
high-value opportunity is still open ten days before the close date.
Workflow automates the following types of actions based on your
organization's processes:
Tasks--Assign a new task to a user, role, or record owner.
Email Alerts--Send an email to one or more recipients you specify.
Field Updates--Update the value of a field on a record.
Outbound Messages--Send a secure, configurable API message (in XML
format) to a designated listener.
For example, workflow can:
Assign follow-up tasks to a support rep one week after a case is updated.
Send sales management an email alert when a sales rep qualifies a large deal.
Change the Owner field on a contract three days before it expires.
Trigger an outbound API message to an external HR system to initiate
the reimbursement process for an approved expense report.
Each workflow rule consists of:
Criteria that cause the workflow rule to run.
Immediate actions that execute when a record matches the criteria. For
example, salesforce.com can automatically send an email that notifies
the account team when a new high-value opportunity is created.
Time-dependent actions that queue when a record matches the criteria,
and execute according to time triggers. For example, salesforce.com
can automatically send an email reminder to the account team if a
high-value opportunity is still open ten days before the close date.
Thursday 20 February 2014
Liittle bit abt trigger
Apex Code Best Practices
Abstract
Apex Code is the Force.com programming language used to write custom, robust business logic. As with any programming language,
there are key coding principles and best practices that will help you write efficient, scalable code. This article illustrates many of the key
best practices for writing and designing Apex Code solutions on the Force.com platform.
Best Practice #1: Bulkify your Code
Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. When a batch of
records initiates Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch. For
example, a trigger could be invoked by an Force.com SOAP API call that inserted a batch of records. So if a batch of records invokes the
same Apex code, all of those records need to be processed as a bulk, in order to write scalable code and avoid hitting governor limits.
Here is an example of poorly written code that only handles one record:
The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new
collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the
Trigger.new collection.
Here is a sample of how to handle all incoming records:
Notice how this revised version of the code iterates across the entire Trigger.new collection with a for loop. Now if this trigger is
invoked with a single Account or up to 200 Accounts, all records are properly processed.
Best Practice #2: Avoid SOQL Queries or DML statements inside FOR Loops
The previous Best Practice talked about the importance of handling all incoming records in a bulk manner. That example showed use of
a for loop to iterate over all of the records in the Trigger.new collection. A common mistake is that queries or DML statements are
placed inside a for loop. There is a governor limit that enforces a maximum number of SOQL queries. There is another that enforces a
maximum number of DML statements (insert, update, delete, undelete). When these operations are placed inside a for loop,
database operations are invoked once per iteration of the loop making it very easy to reach these governor limits.
Instead, move any database operations outside of for loops. If you need to query, query once, retrieve all the necessary data in a single
query, then iterate over the results. If you need to modify the data, batch up data into a list and invoke your DML once on that list of data.
Here is an example showing both a query and a DML statement inside a for loop:
view source history
01 trigger accountTestTrggr on Account (before insert, before update) {
02
03 //This only handles the first record in the Trigger.new collection
04 //But if more than one Account initiated this trigger, those additional records
05 //will not be processed
06 Account acct = Trigger.new[0];
07 List<Contact> contacts = [select id, salutation, firstname, lastname, email
08 from Contact where accountId = :acct.Id];
09
10 }
01 trigger accountTestTrggr on Account (before insert, before update) {
02
03 List<String> accountNames = new List<String>{};
04
05 //Loop through all records in the Trigger.new collection
06 for(Account a: Trigger.new){
07 //Concatenate the Name and billingState into the Description field
08 a.Description = a.Name + ':' + a.BillingState
09 }
10
11 }
01 trigger accountTestTrggr on Account (before insert, before update) {
02
03 //For loop to iterate through all the incoming Account records
04 for(Account a: Trigger.new) {
05
06 //THIS FOLLOWING QUERY IS INEFFICIENT AND DOESN'T SCALE
07 //Since the SOQL Query for related Contacts is within the FOR loop, if this trigger is
initiated
08 //with more than 100 records, the trigger will exceed the trigger governor limit
09 //of maximum 100 SOQL Queries.
10
11 List<Contact> contacts = [select id, salutation, firstname, lastname, email
12 from Contact where accountId = :a.Id];
13
14 for(Contact c: contacts) {
15 System.debug('Contact Id[' + c.Id + '], FirstName[' + c.firstname + '],
16 LastName[' + c.lastname +']');
17 c.Description=c.salutation + ' ' + c.firstName + ' ' + c.lastname;
18
19 //THIS FOLLOWING DML STATEMENT IS INEFFICIENT AND DOESN'T SCALE
//Since the UPDATE dml operation is within the FOR loop, if this trigger is initiated
page
Since there is a SOQL query within the for loop that iterates across all the Account objects that initiated this trigger, a query will be
executed for each Account. An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if
this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception.
In this example, because there is a limit of 150 DML operations per request, a governor limit will be exceeded after the 150th contact is
updated.
Here is the optimal way to 'bulkify' the code to efficiently query the contacts in a single query and only perform a single update DML
operation.
Now if this trigger is invoked with a single account record or up to 200 account records, only one SOQL query and one update statement
is executed.
Best Practice #3: Bulkify your Helper Methods
This best practice is similar to the previous one: make sure any code that runs a query or DML operation does it in a bulk manner and
doesn't execute within an iteration or a for loop. Executing queries or DML operations within an iteration adds risk that the governor
limits will be exceeded. This is also true for any helper or utility methods an Apex request executes.
As discussed in the SFDC:ApexGovernorLimits article, governor limits are calculated at runtime. After the request is initiated (Trigger,
Visualforce page, etc.), any Apex code executed in that transaction applies and shares the governor limits. So if a trigger uses some
Apex methods written in a helper class, it's important that those shared Apex methods are properly designed to handle bulk records.
These methods should be written to be invoked with a set of records, especially if the method has a SOQL query or DML operation.
For example, if the Apex method performs a SOQL query, that method should receive a collection (Array, List, Set, etc.) of records so
when it performs the query, it can perform the query for all records in the Apex transaction. Otherwise, if the Apex method is called
individually for each record being processed, the Apex transaction will inefficiently run queries and possibly exceed the allowed number
of queries allowed in that transaction. The same is true for DML statements in Apex methods.
So please make sure any utility or helper methods are efficiently written to handle collections of records. This will avoid unnecessarily
executing inefficient queries and DML operations.
Best Practice #4: Using Collections, Streamlining Queries, and Efficient For Loops
It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and
streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits.
Here is a sample that uses collections inefficiently:
20 //Since the UPDATE dml operation is within the FOR loop, if this trigger is initiated
21 //with more than 150 records, the trigger will exceed the trigger governor limit
22 //of 150 DML Operations maximum.
23
24 update c;
25 }
26 }
27 }
01 trigger accountTestTrggr on Account (before insert, before update) {
02 //This queries all Contacts related to the incoming Account records in a single SOQL query.
03 //This is also an example of how to use child relationships in SOQL
04 List<Account> accountsWithContacts = [select id, name, (select id, salutation, description,
05 firstname, lastname, email from
Contacts)
06 from Account where Id
IN :Trigger.newMap.keySet()];
07
08 List<Contact> contactsToUpdate = new List<Contact>{};
09 // For loop to iterate through all the queried Account records
10 for(Account a: accountsWithContacts){
11 // Use the child relationships dot syntax to access the related Contacts
12 for(Contact c: a.Contacts){
13 System.debug('Contact Id[' + c.Id + '], FirstName[' + c.firstname + '], LastName[' +
c.lastname +']');
14 c.Description=c.salutation + ' ' + c.firstName + ' ' + c.lastname;
15 contactsToUpdate.add(c);
16 }
17 }
18
19 //Now outside the FOR Loop, perform a single Update DML statement.
20 update contactsToUpdate;
21 }
01 trigger accountTrigger on Account (before delete, before insert, before update) {
02
03 //This code inefficiently queries the Opportunity object in two seperate queries
04 List<Opportunity> opptysClosedLost = [select id, name, closedate, stagename
05 from Opportunity where
06 accountId IN :Trigger.newMap.keySet() and StageName='Closed - Lost'];
07
08 List<Opportunity> opptysClosedWon = [select id, name, closedate, stagename
09 from Opportunity where
10 accountId IN :Trigger.newMap.keySet() and StageName='Closed - Won'];
11
12 for(Account a : Trigger.new){
13
14 //This code inefficiently has two inner FOR loops
15 //Redundantly processes the List of Opportunity Lost
16 for(Opportunity o: opptysClosedLost){
17 if(o.accountid == a.id)
18 System.debug('Do more logic here...');
19 }
20
21 //Redundantly processes the List of Opportunity Won
22 for(Opportunity o: opptysClosedWon){
23 if(o.accountid == a.id)
24 System.debug('Do more logic here...');
25 }
26 }
27 }
The main issue with the previous snippet is the unnecessary querying of the opportunity records in two separate queries. Use the
power of the SOQL where clause to query all data needed in a single query. Another issue here is the use of two inner for loops that
redundantly loop through the list of opportunity records just trying to find the ones related to a specific account. Look at the following
revision:
This revised sample only executes one query for all related opportunities and only has one inner for loop to apply the same logic, but
in a much more efficient, governor-friendly manner.
Best Practice #5: Streamlining Multiple Triggers on the Same Object
It is important to avoid redundancies and inefficiencies when deploying multiple triggers on the same object. If developed
independently, it is possible to have redundant queries that query the same dataset or possibly have redundant for statements.
Note that it is very important to detail exactly how governor limits are applied when multiple triggers are deployed on the same object.
For starters, you do not have any explicit control over which trigger gets initiated first. Secondly, each trigger that is invoked does not get
its own governor limits. Instead, all code that is processed, including the additional triggers, share those available resources.
So instead of only the one trigger getting a maximum of 100 queries, all triggers on that same object will share those 100 queries. That
is why it is critical to ensure that the multiple triggers are efficient and no redundancies exist.
Best Practice #6: Querying Large Data Sets
The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you
to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the
use of internal calls to query and queryMore.
For example, if the results are too large, the syntax below causes a runtime exception:
Instead, use a SOQL query for loop as in one of the following examples:
Let the Force.com platform chunk your large query results into batches of 200 records by using this syntax where the SOQL query is in
the for loop definition, and then handle the individual datasets in the for loop logic.
Best Practice #7: Use of the Limits Apex Methods to Avoid Hitting Governor Limits
Apex has a System class called Limits that lets you output debug messages for each governor limit. There are two versions of every
method: the first returns the amount of the resource that has been used in the current context, while the second version contains the
word limit and returns the total amount of the resource that is available for that context.
The following example shows how to embed these types of statements in your code and ultimately determine if or when you are about
to exceed any governor limits. Using either the System Log or Debug Logs, you can evaluate the output to see how the specific code is
performing against the governor limits. Additionally, you can embed logic in the Apex code directly to throw error messages before
reaching a governor limit. The code sample below has an IF statement to evaluate if the trigger is about to update too many
Opportunities.
Here is an example of how you can use a combination of System.debug statements and the Limits Apex class to generate some very
useful output as it relates to governor limits and the overall efficiency of your code.
01 trigger accountTrigger on Account (before delete, before insert, before update) {
02
03 //This code efficiently queries all related Closed Lost and
04 //Closed Won opportunities in a single query.
05 List<Account> accountWithOpptys = [select id, name, (select id, name, closedate,
06 stagename from Opportunities where accountId IN :Trigger.newMap.keySet()
07 and (StageName='Closed - Lost' or StageName = 'Closed - Won'))
08 from Account where Id IN :Trigger.newMap.keySet()];
09
10 //Loop through Accounts only once
11 for(Account a : accountWithOpptys){
12
13 //Loop through related Opportunities only once
14 for(Opportunity o: a.Opportunities){
15 if(o.StageName == 'Closed - Won'){
16 System.debug('Opportunity Closed Won...do some more logic here...');
17 }else if(o.StageName =='Closed - Lost'){
18 System.debug('Opportunity Closed Lost...do some more logic here...');
19 }
20 }
21
22 }
23 }
1 //A runtime exception is thrown if this query returns enough records to exceed your heap limit.
2 Account[] accts = [SELECT id FROM account];
1 // Use this format for efficiency if you are executing DML statements
2 // within the for loop. Be careful not to exceed the 150 DML statement limit.
3 for (List<Account> acct : [SELECT id, name FROM account
4 WHERE name LIKE 'Acme']) {
5 // Your code here
6 update acct;
7 }
01 trigger accountLimitExample on Account (after delete, after insert, after update) {
02
03 System.debug('Total Number of SOQL Queries allowed in this Apex code context: ' +
Limits.getLimitQueries());
04 System.debug('Total Number of records that can be queried in this Apex code context: ' +
Limits.getLimitDmlRows());
05 System.debug('Total Number of DML statements allowed in this Apex code context: ' +
Limits.getLimitDmlStatements() );
06 System.debug('Total Number of CPU usage time (in ms) allowed in this Apex code context: ' +
Limits.getLimitCpuTime());
07
08 // Query the Opportunity object
09 List<Opportunity> opptys =
10 [select id, description, name, accountid, closedate, stagename from Opportunity where
accountId IN: Trigger.newMap.keySet()];
11
And here is a sample output after running the trigger by updating an account record through the user interface. This was generated in a
debug log:
DEBUG|Total Number of SOQL Queries allowed in this Apex code context: 100
DEBUG|Total Number of records that can be queried in this Apex code context: 10000
DEBUG|Total Number of DML statements allowed in this Apex code context: 150
DEBUG|Total Number of CPU usage time (in ms) allowed in this Apex code context: 10000
DEBUG|1. Number of Queries used in this Apex code so far: 1
DEBUG|2. Number of rows queried in this Apex code so far: 0
DEBUG|3. Number of DML statements used so far: 0
DEBUG|4. Amount of CPU time (in ms) used so far: 9
DEBUG|Continue processing. Not going to hit DML governor limits
DEBUG|Going to update 3 opportunities and governor limits will allow 10000
DEBUG|Number of DML statements used so far: 0
DEBUG|Final number of DML statements used so far: 1
DEBUG|Final heap size: 1819
This example illustrates how valuable the Limits Apex class can be when debugging and analyzing the efficiency of your code. It also
demonstrates how you can proactively check if you are going to run into governor limits and better handle those scenarios.
Apex Governor Limit Warning Emails
Additionally, you can enable Apex governor limit warning emails.
When an end-user invokes Apex code that surpasses more than 50% of any governor limit, you can specify a user in your organization to
receive an email notification of the event with additional details. To enable email warnings:
Best Practice #8: Use @future Appropriately
As articulated throughout this article, it is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also
true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and
asynchronous Apex can be found Governors in Apex Code#Synchronous_vs_Asynchronous_Apex. Even though Apex written within an
asynchronous method gets its own independent set of higher governor limits, it still has governor limits. Additionally, no more than ten
@future methods can be invoked within a single Apex transaction.
Here is a list of governor limits specific to the @future annotation:
No more than 10 method calls per Apex invocation
No more than 200 method calls per Salesforce license per 24 hours
The parameters specified must be primitive dataypes, arrays of primitive datatypes, or collections of primitive datatypes.
Methods with the future annotation cannot take sObjects or objects as arguments.
Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods,
nor in the constructor.
It's important to make sure that the asynchronous methods are invoked in an efficient manner and that the code in the methods is
efficient. In the following example, the Apex trigger inefficiently invokes an asynchronous method for each Account record it wants to
process:
12 System.debug('1. Number of Queries used in this Apex code so far: ' + Limits.getQueries());
13 System.debug('2. Number of rows queried in this Apex code so far: ' + Limits.getDmlRows());
14 System.debug('3. Number of DML statements used so far: ' + Limits.getDmlStatements());
15 System.debug('4. Amount of CPU time (in ms) used so far: ' + Limits.getCpuTime());
16
17 //NOTE:Proactively determine if there are too many Opportunities to update and avoid governor
limits
18 if (opptys.size() + Limits.getDMLRows() > Limits.getLimitDMLRows()) {
19 System.debug('Need to stop processing to avoid hitting a governor limit. Too many
related Opportunities to update in this trigger');
20 System.debug('Trying to update ' + opptys.size() + ' opportunities but governor limits
will only allow ' + Limits.getLimitDMLRows());
21 for (Account a : Trigger.new) {
22 a.addError('You are attempting to update the addresses of too many accounts at
once. Please try again with fewer accounts.');
23 }
24 }
25
26 else{
27 System.debug('Continue processing. Not going to hit DML governor limits');
28 System.debug('Going to update ' + opptys.size() + ' opportunities and governor limits will
allow ' + Limits.getLimitDMLRows());
29 for(Account a : Trigger.new){
30 System.debug('Number of DML statements used so far: ' + Limits.getDmlStatements());
31
32
33 for(Opportunity o: opptys){
34 if (o.accountid == a.id)
35 o.description = 'testing';
36 }
37
38 }
39 update opptys;
40 System.debug('Final number of DML statements used so far: ' + Limits.getDmlStatements());
41 System.debug('Final heap size: ' + Limits.getHeapSize());
42 }
43 }
1 1. Log in to Salesforce as an administrator user.
2 2. Click Setup | Manage Users | Users.
3 3. Click Edit next to the name of the user who should receive the email notifications.
4 4. Select the Send Apex Warning Emails option.
5 5. Click Save.
1 trigger accountAsyncTrigger on Account (after insert, after update) {
2 for(Account a: Trigger.new){
// Invoke the @future method for each Account
2/20/14 Apex Code Best Practices - developer.force.com
wiki.developerforce.com/page/Apex_Code_Best_Practices 5/7
Here is the Apex class that defines the @future method:
Since the @future method is invoked within the for loop, it will be called N-times (depending on the number of accounts being
processed). So if there are more than ten accounts, this code will throw an exception for exceeding a governor limit of only ten @future
invocations per Apex transaction.
Instead, the @future method should be invoked with a batch of records so that it is only invoked once for all records it needs to process:
And now the @future method is designed to receive a set of records:
Notice the minor changes to the code to handle a batch of records. It doesn't take a whole lot of code to handle a set of records as
compared to a single record, but it's a critical design principle that should persist across all of your Apex code - regardless if it's
executing synchronously or asynchronously.
Best Practice #9: Writing Test Methods to Verify Large Datasets
Since Apex code executes in bulk, it is essential to have test scenarios to verify that the Apex being tested is designed to handle large
datasets and not just single records. To elaborate, an Apex trigger can be invoked either by a data operation from the user interface or by
a data operation from the Force.com SOAP API. The API can send multiple records per batch, leading to the trigger being invoked with
several records. Therefore, it is key to have test methods that verify that all Apex code is properly designed to handle larger datasets and
that it does not exceed governor limits.
The example below shows you a poorly written trigger that does not handle bulk properly and therefore hits a governor limit. Later, the
trigger is revised to properly handle bulk datasets.
Here is the poorly written contact trigger. For each contact, the trigger performs a SOQL query to retrieve the related account. The invalid
part of this trigger is that the SOQL query is within the for loop and therefore will throw a governor limit exception if more than 100
contacts are inserted/updated.
Here is the test method that tests if this trigger properly handles volume datasets:
3 // Invoke the @future method for each Account
4 // This is inefficient and will easily exceed the governor limit of
5 // at most 10 @future invocation per Apex transaction
6 asyncApex.processAccount(a.id);
7 }
8 }
01 global class asyncApex {
02
03 @future
04 public static void processAccount(Id accountId) {
05 List<Contact> contacts = [select id, salutation, firstname, lastname, email
06 from Contact where accountId = :accountId];
07
08 for(Contact c: contacts){
09 System.debug('Contact Id[' + c.Id + '], FirstName[' + c.firstname + '], LastName[' +
c.lastname +']');
10 c.Description=c.salutation + ' ' + c.firstName + ' ' + c.lastname;
11 }
12 update contacts;
13 }
14 }
1 trigger accountAsyncTrigger on Account (after insert, after update) {
2 //By passing the @future method a set of Ids, it only needs to be
3 //invoked once to handle all of the data.
4 asyncApex.processAccount(Trigger.newMap.keySet());
5 }
01 global class asyncApex {
02
03 @future
04 public static void processAccount(Set<Id> accountIds) {
05 List<Contact> contacts = [select id, salutation, firstname, lastname, email from Contact
where accountId IN :accountIds];
06 for(Contact c: contacts){
07 System.debug('Contact Id[' + c.Id + '], FirstName[' + c.firstname + '], LastName[' +
c.lastname +']');
08 c.Description=c.salutation + ' ' + c.firstName + ' ' + c.lastname;
09 }
10 update contacts;
11 }
12 }
01 trigger contactTest on Contact (before insert, before update) {
02
03 for(Contact ct: Trigger.new){
04
05 Account acct = [select id, name from Account where Id=:ct.AccountId];
06 if(acct.BillingState=='CA'){
07 System.debug('found a contact related to an account in california...');
08 ct.email = 'test_email@testing.com';
09 //Apply more logic here....
10 }
11 }
12
13 }
01 public class sampleTestMethodCls {
02
03 static testMethod void testAccountTrigger(){
04
05 //First, prepare 200 contacts for the test data
06 Account acct = new Account(name='test account');
07 insert acct;
08
09 Contact[] contactsToCreate = new Contact[]{};
10 for(Integer x=0; x<200;x++){
11 Contact ct = new Contact(AccountId=acct.Id,lastname='test');
12 contactsToCreate.add(ct);
13 }
2/20/14 Apex Code Best Practices - developer.force.com
wiki.developerforce.com/page/Apex_Code_Best_Practices 6/7
This test method creates an array of 200 contacts and inserts them. The insert will, in turn, cause the trigger to fire. When this test
method is executed, a System.Exception will be thrown when it hits a governor limit. Since the trigger shown above executes a
SOQL query for each contact in the batch, this test method throws the exception 'Too many SOQL queries: 101'. A trigger can only
execute at most 100 queries.
Note the use of Test.startTest and Test.stopTest. When executing tests, code called before Test.startTest and after
Test.stopTest receive a separate set of governor limits than the code called between Test.startTest and Test.stopTest.
This allows for any data that needs to be setup to do so without affecting the governor limits available to the actual code being tested.
Now let's correct the trigger to properly handle bulk operations. The key to fixing this trigger is to get the SOQL query outside the for
loop and only do one SOQL Query:
Note how the SOQL query retrieving the accounts is now done once only. If you re-run the test method shown above, it will now execute
successfully with no errors and 100% code coverage.
Best Practices #10: Avoid Hardcoding IDs
When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is
essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can
dynamically identify the proper data to operate against and not fail.
Here is a sample that hardcodes the record type IDs that are used in an conditional statement. This will work fine in the specific
environment in which the code was developed, but if this code were to be installed in a separate org (ie. as part of an AppExchange
package), there is no guarantee that the record type identifiers will be the same.
Now, to properly handle the dynamic nature of the record type IDs, the following example queries for the record types in the code, stores
the dataset in a map collection for easy retrieval, and ultimately avoids any hardcoding.
By ensuring no IDs are stored in the Apex code, you are making the code much more dynamic and flexible - and ensuring that it can be
deployed safely to different environments.
Summary
This article covers many of the core Apex coding best practices. These principles should be incorporated into your Apex code in order to
write efficient, scalable code. We discussed how to bulkify your code by handling all incoming records instead of just one. We also
illustrated how to avoid having SOQL queries inside a loop to avoid governor limits. Additionally, there are examples of how to output
helpful governor limit debugging statements, along with several other best practices. By following these principles, you are on a great
path for success with Apex code.
14
15 //Now insert data causing an contact trigger to fire.
16 Test.startTest();
17 insert contactsToCreate;
18 Test.stopTest();
19 }
20 }
01 trigger contactTest on Contact (before insert, before update) {
02
03 Set<Id> accountIds = new Set<Id>();
04 for(Contact ct: Trigger.new)
05 accountIds.add(ct.AccountId);
06
07 //Do SOQL Query
08 Map<Id, Account> accounts = new Map<Id, Account>(
09 [select id, name, billingState from Account where id in :accountIds]);
10
11 for(Contact ct: Trigger.new){
12 if(accounts.get(ct.AccountId).BillingState=='CA'){
13 System.debug('found a contact related to an account in california...');
14 ct.email = 'test_email@testing.com';
15 //Apply more logic here....
16 }
17 }
18
19 }
01 for(Account a: Trigger.new){
02
03 //Error - hardcoded the record type id
04 if(a.RecordTypeId=='012500000009WAr'){
05 //do some logic here.....
06 }else if(a.RecordTypeId=='0123000000095Km'){
07 //do some logic here for a different record type...
08 }
09
10 }
01 //Query for the Account record types
02 List<RecordType> rtypes = [Select Name, Id From RecordType
03 where sObjectType='Account' and isActive=true];
04
05 //Create a map between the Record Type Name and Id for easy retrieval
06 Map<String,String> accountRecordTypes = new Map<String,String>{};
07 for(RecordType rt: rtypes)
08 accountRecordTypes.put(rt.Name,rt.Id);
09
10 for(Account a: Trigger.new){
11
12 //Use the Map collection to dynamically retrieve the Record Type Id
13 //Avoid hardcoding Ids in the Apex code
14 if(a.RecordTypeId==accountRecordTypes.get('Healthcare')){
15 //do some logic here.....
16 }else if(a.RecordTypeId==accountRecordTypes.get('High Tech')){
17 //do some logic here for a different record type...
18 }
19
20 }
2/20/14 Apex Code Best Practices - developer.force.com
wiki.developerforce.com/page/Apex_Code_Best_Practices 7/7
Follow us on
United States | 1-800-no-softw are | Privacy Statement | Security Statement | Terms of Use | Contact Us | Feedback
© Copyright 2000-2013 salesforce.com, inc. Web-based Customer Relationship Management (CRM) Softw are-as-a-Service (SaaS).
All rights reserved Various trademarks held by their respective ow ners.
Salesforce.com, inc. The Landmark @ One Market, Suite 300, San Francisco, CA, 94105, United States
General Enquiries: 415-901-7000 | Fax: 415-901-7040 | Sales: 1-800-no-softw are
References
See the Apex Technical Library page for a comprehensive list of resources about Apex Code.
The documentation has a full list of all Apex Governor Limits.
See the Apex Developer's Guide to learn about the Apex code and syntax.
Governors in Apex Code provides a detailed overview of Governor limits.
An Introduction to Apex Code Test Methods provides another important aspect to developing with Apex.
About the Author
Andrew Albert is a Technical Evangelist at salesforce.com, focusing on the Force.com Platform. He works with ISVs and developers that
are looking to build applications on the Force.com platform.
Category:
Apex - General
Tuesday 18 February 2014
Test Class
Testing Best Practices
Good tests should do the following:
- Cover as many lines of code as possible. Before you can deploy Apex or package it for the Force.com AppExchange, the following must be true.
- In the case of conditional logic (including ternary operators), execute each branch of code logic.
- Make calls to methods using both valid and invalid inputs.
- Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
- Always handle all exceptions that are caught, instead of merely catching the exceptions.
- Use System.assert methods to prove that code behaves properly.
- Use the runAs method to test your application in different user contexts.
- Exercise bulk trigger functionality—use at least 20 records in your tests.
- Use the ORDER BY keywords to ensure that the records are returned in the expected order.
- Not assume that record IDs are in sequential order.
Record IDs are not created in ascending order unless you insert multiple records with the same request. For example, if you create an account A, and receive the ID001D000000IEEmT, then create account B, the ID of account B may or may not be sequentially higher.
- On the list of Apex classes, there is a Code Coverage column. If you click the coverage percent number, a page displays, highlighting the lines of code for that class or trigger that are covered by tests in blue, as well as highlighting the lines of code that are not covered by tests in red. It also lists how many times a particular line in the class or trigger was executed by the test.
- Set up test data:
- Create the necessary data in test classes, so the tests do not have to rely on data in a particular organization.
- Create all test data before calling the starttest method.
- Since tests don't commit, you won't need to delete any data.
- Write comments stating not only what is supposed to be tested, but the assumptions the tester made about the data, the expected outcome, and so on.
- Test the classes in your application individually. Never test your entire application in a single test.
If you are running many tests, consider the following:
- In the Force.com IDE, you may need to increase the Read timeout value for your Apex project. Seehttps://wiki.developerforce.com/index.php/Apex_Toolkit_for_Eclipse for details.
- In the Salesforce user interface, you may need to test the classes in your organization individually, instead of trying to run all of the tests at the same time using the Run All Testsbutton.
Wednesday 12 February 2014
Some Codes
public class ContactTriggerHandler{
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
// updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
// updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{setOfAccountIdFromContact.add(eachCon.AccountId); }
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_Female_Contacts__c,Total_male_Contacts__c from Account
where Id IN : setOfAccountIdFromContact];
List<AggregateResult> lst=[SELECT Gender__c, COUNT(id) FROM Contact where accountid IN:setOfAccountIdFromContact GROUP BY Gender__c];
for(Account objAccount :lstAccount)
{
for(AggregateResult objAggregateResult :lst)
{
if(objAggregateResult.gender__c='male')
{objAccount.Total_Male_Contacts__c=objAggregateResult.COUNT(objAggregateResult.gender__c);}
if(objAggregateResult.gender__c='female')
{ objAccount.Total_Female_Contacts__c=objAggregateResult.total_contact;}
}
}
update lstAccount;
}
}
}public class ContactTriggerHandler{
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
// updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
// updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{setOfAccountIdFromContact.add(eachCon.AccountId); }
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_Female_Contacts__c,Total_male_Contacts__c from Account
where Id IN : setOfAccountIdFromContact];
AggregateResult[] agrResult=[SELECT Gender__c, COUNT(id) total_contacts FROM Contact where accountid IN:setOfAccountIdFromContact GROUP BY Gender__c];
for(Account objAccount :lstAccount)
{
for(AggregateResult objAggregateResult :agrResult)
{
if(objAggregateResult.get('gender__c')='male')
{objAccount.Total_Male_Contacts__c=objAggregateResult.get('total_contacts');}
if(objAggregateResult.gender__c='female')
{ objAccount.Total_Female_Contacts__c=objAggregateResult.get('total_contact');}
}
}
update lstAccount;
}
}
}public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
// updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
// updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0 && setOfgenderFromContact='')
{
for(String objString :setOfgenderFromContact)
{
if(objString='male')
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
}
if(objString='female')
{
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
}
}
update lstAccount;
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_female_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
}
update lstAccount;
update lstAccount2;public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
updateNumberOfGenderContactsOnAccount(triggerOld, triggerOldMap);
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_female_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
update lstAccount;
update lstAccount2;
}
}
}public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
updateNumberOfGenderContactsOnAccount(triggerOld, triggerOldMap);
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_female_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
for(Account objAccount :lstAccount2)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
update lstAccount;
update lstAccount2;
}
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
for(String st:setOfgenderFromContact)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c IN:setOfgenderFromContact) from Account
where Id IN : setOfAccountIdFromContact];
if(setOfgenderFromContact='male')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
}
if(setOfgenderFromContact='female')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
}
update lstAccount;
}
}
}
private void OppurtunityUpdate()
{
Set<ID> setOfAccountID=new Set<ID>();
if(trigger.new!=null)
{
for(Opportunity objOpportunity:trigger.New)
{
if(objOpportunity.AccountID!=null)
{
setOfAccountID.add(objOpportunity.AccountID);
}
}
}
if(setOfAccountId != null && setOfAccountId.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id,name from contacts)
from Account where Id IN : setOfAccountId];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachOpportunity.Total_Contacts__c = (Opportunity)(eachAccount.contacts.size());
}
)
update lstAccount;
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeOpportunityUpdate(list<Opportunity> triggerOld, map<id,Opportunity> triggerOldMap)
{
onUpdateOfrOfOpportunity(triggerOld,triggerOldMap);
}
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
updateNumberOfGenderContactsOnAccount(triggerOld, triggerOldMap);
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
private void onUpdateOfrOfOpportunity(List<Opportunity> triggerOld,Map<id,Opportunity> triggerOldMap)
{
Set<ID> setOfAccountID=new Set<ID>();
if(triggerOld!=null)
{
for(Opportunity objOpportunity:triggerOld)
{
if(objOpportunity.AccountID!=null)
{
setOfAccountID.add(objOpportunity.AccountID);
}
}
}
if(setOfAccountId != null && setOfAccountId.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id,name from contacts)
from Account where Id IN : setOfAccountId];
Opportunity eachOpportunity=new Opportunity();
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachOpportunity.Total_Contacts_Linked__c = (eachAccount.contacts.size());
}
}
update lstAccount;
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
for(String st:setOfgenderFromContact)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c IN:setOfgenderFromContact) from Account
where Id IN : setOfAccountIdFromContact];
if(st=='male')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
}
if(st=='female')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
}
update lstAccount;
}
}
}
}/**
* An apex page controller that exposes the change password functionality
*/
public with sharing class ChangePasswordController {
public String oldPassword {get; set;}
public String newPassword {get; set;}
public String verifyNewPassword {get; set;}
public PageReference changePassword() {
return Site.changePassword(newPassword, verifyNewPassword, oldpassword);
}
public ChangePasswordController() {}
}/**
* An apex page controller that exposes the change password functionality
*/
@IsTest public with sharing class ChangePasswordControllerTest {
@IsTest(SeeAllData=true) public static void testChangePasswordController() {
// Instantiate a new controller with all parameters in the page
ChangePasswordController controller = new ChangePasswordController();
controller.oldPassword = '123456';
controller.newPassword = 'qwerty1';
controller.verifyNewPassword = 'qwerty1';
System.assertEquals(controller.changePassword(),null);
}
}/**
* An apex page controller that exposes the site forgot password functionality
*/
public with sharing class ForgotPasswordController {
public String username {get; set;}
public ForgotPasswordController() {}
public PageReference forgotPassword() {
boolean success = Site.forgotPassword(username);
PageReference pr = Page.ForgotPasswordConfirm;
pr.setRedirect(true);
if (success) {
return pr;
}
return null;
}
}/**
* An apex page controller that exposes the site forgot password functionality
*/
@IsTest public with sharing class ForgotPasswordControllerTest {
@IsTest(SeeAllData=true) public static void testForgotPasswordController() {
// Instantiate a new controller with all parameters in the page
ForgotPasswordController controller = new ForgotPasswordController();
controller.username = 'test@salesforce.com';
System.assertEquals(controller.forgotPassword(),null);
}
}/**
* An apex class that keeps updates of a portal user in sync with its corresponding contact.
Guest users are never able to access this page.
*/
@IsTest public with sharing class MyProfilePageControllerTest {
@IsTest(SeeAllData=true) static void testSetContactFields() {
User u = [select title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
Contact c = new Contact();
MyProfilePageController.setContactFields(c, u);
System.assertEquals(c.firstname, u.firstname, 'firstname should have been set as the firstname of the user for the contact');
System.assertEquals(c.lastname, u.lastname, 'lastname should have been set as the lastname of the user for the contact');
}
@IsTest(SeeAllData=true) static void testSave() {
// Modify the test to query for a portal user that exists in your org
List<User> existingPortalUsers = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null AND UserType='CustomerSuccess'];
if (existingPortalUsers.isEmpty()) {
User currentUser = [select id, title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(currentUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
Contact c = new Contact();
c.LastName = 'TestContact';
insert c;
c.title = currentUser.title;
c.firstname = currentUser.firstname;
c.lastname = currentUser.lastname;
c.email = currentUser.email;
c.phone = currentUser.phone;
c.mobilephone = currentUser.mobilephone;
c.fax = currentUser.fax;
c.mailingstreet = currentUser.street;
c.mailingcity = currentUser.city;
c.mailingstate = currentUser.state;
c.mailingpostalcode = currentUser.postalcode;
c.mailingcountry = currentUser.country;
controller.save();
System.assert(Page.ChangePassword.getUrl().equals(controller.changePassword().getUrl()));
} else {
User existingPortalUser = existingPortalUsers[0];
String randFax = Math.rint(Math.random() * 1000) + '5551234';
System.runAs(existingPortalUser) {
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(existingPortalUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
controller.getUser().Fax = randFax;
controller.save();
System.assert(controller.getIsEdit() == false);
}
// verify that the user and contact were updated
existingPortalUser = [Select id, fax, Contact.Fax from User where id =: existingPortalUser.Id];
System.assert(existingPortalUser.fax == randFax);
System.assert(existingPortalUser.Contact.fax == randFax);
}
}
}public class opcontact{
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public opcontact(boolean isexecuting, integer size)
{
m_isexecuting=isexecuting;
batchsize=size;
}
public void onbeforeinsert(list<opportunity> triggernew,map<id,opportunity> triggernewmap)
{
updatelist(triggernew,triggernewmap);
}
private void updatelist(list<opportunity> triggernew,map<id,opportunity> triggernewmap)
{
set<id> sid=new set<id>();
if(triggernew!=null && triggernew.size()>0)
{
for(opportunity c: triggernew)
{
sid.add(c.accountid);
}}
if(sid!=null && sid.size()>0)
{
map<id,account> mapAccIdToAccount = new map<id,account>([select id,
(select id from contacts)
from account
where Id IN : sid]);
//list<opportunity> opc = [select id,no_of_contacts__c from opportunity];
if(triggernew!= null && triggernew.size() > 0)
{
for(opportunity eachopp : triggernew)
{
if(mapAccIdToAccount.get(eachopp.AccountId) != null)
{
Account objAcc = mapAccIdToAccount.get(eachopp.AccountId);
eachopp.NO_of_contacts__c = objAcc.contacts.size();
}
}
}
}
}
}/**
* An apex page controller that exposes the site login functionality
*/
global with sharing class SiteLoginController {
global String username {get; set;}
global String password {get; set;}
global PageReference login() {
String startUrl = System.currentPageReference().getParameters().get('startURL');
return Site.login(username, password, startUrl);
}
global SiteLoginController () {}
}/**
* An apex page controller that exposes the site login functionality
*/
@IsTest global with sharing class SiteLoginControllerTest {
@IsTest(SeeAllData=true) global static void testSiteLoginController () {
// Instantiate a new controller with all parameters in the page
SiteLoginController controller = new SiteLoginController ();
controller.username = 'test@salesforce.com';
controller.password = '123456';
System.assertEquals(controller.login(),null);
}
}/**
* An apex class that creates a portal user
*/
public with sharing class SiteRegisterController {
// PORTAL_ACCOUNT_ID is the account on which the contact will be created on and then enabled as a portal user.
// you need to add the account owner into the role hierarchy before this will work - please see Customer Portal Setup help for more information.
private static Id PORTAL_ACCOUNT_ID = '001x000xxx35tPN';
public SiteRegisterController () {
}
public String username {get; set;}
public String email {get; set;}
public String password {get; set {password = value == null ? value : value.trim(); } }
public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
private boolean isValidPassword() {
return password == confirmPassword;
}
public PageReference registerUser() {
// it's okay if password is null - we'll send the user a random password in that case
if (!isValidPassword()) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
ApexPages.addMessage(msg);
return null;
}
User u = new User();
u.Username = username;
u.Email = email;
u.CommunityNickname = communityNickname;
String accountId = PORTAL_ACCOUNT_ID;
// lastName is a required field on user, but if it isn't specified, we'll default it to the username
String userId = Site.createPortalUser(u, accountId, password);
if (userId != null) {
if (password != null && password.length() > 1) {
return Site.login(username, password, null);
}
else {
PageReference page = System.Page.SiteRegisterConfirm;
page.setRedirect(true);
return page;
}
}
return null;
}
}/**
* Class containing tests for SiteRegisterController
*/
@IsTest public with sharing class SiteRegisterControllerTest {
@IsTest(SeeAllData=true) static void testRegistration() {
SiteRegisterController controller = new SiteRegisterController();
controller.username = 'test@force.com';
controller.email = 'test@force.com';
controller.communityNickname = 'test';
// registerUser will always return null when the page isn't accessed as a guest user
System.assert(controller.registerUser() == null);
controller.password = 'abcd1234';
controller.confirmPassword = 'abcd123';
System.assert(controller.registerUser() == null);
}
}/**
* Class containing tests for SiteRegisterController
*/
@IsTest public with sharing class SiteRegisterControllerTest {
@IsTest(SeeAllData=true) static void testRegistration() {
SiteRegisterController controller = new SiteRegisterController();
controller.username = 'test@force.com';
controller.email = 'test@force.com';
controller.communityNickname = 'test';
// registerUser will always return null when the page isn't accessed as a guest user
System.assert(controller.registerUser() == null);
controller.password = 'abcd1234';
controller.confirmPassword = 'abcd123';
System.assert(controller.registerUser() == null);
}
}
Select recordtype.id,id,name from contact where recordtype.id='01290000000tdmAAAQ'
SELECT Gender__c, COUNT(id) FROM Contact GROUP BY Gender__c
SELECT Gender__c, COUNT(id) FROM Contact where accountid='0019000000njPGeAAM' GROUP BY Gender__c
0019000000njPGeAAM
list<Account> lstAccount =
[select id, Name, Total_Contacts__c,(select id from contacts) from Account
where Id IN : setOfAccountIdFromContact];
public class ContactTriggerHandler{
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_Female_Contacts__c,Total_male_Contacts__c from Account
where Id IN : setOfAccountIdFromContact];
List<Contact> lstcontact=[SELECT Gender__c, COUNT(id) total_Contact FROM Contact where accountid IN:setOfAccountIdFromContact GROUP BY Gender__c];
for(Account objAccount :lstAccount)
{
for(Contact objContact :lstContact)
{
if(objContact.gender__c='male')
{objAccount.Total_Male_Contacts=objContact.total_contact;}
if(objContact.gender__c='female')
{ objAccount.Total_Female_Contacts=objContact.total_contact;}
}
}
}
update lstAccount;
}
}
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
// updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
// updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{setOfAccountIdFromContact.add(eachCon.AccountId); }
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_Female_Contacts__c,Total_male_Contacts__c from Account
where Id IN : setOfAccountIdFromContact];
List<AggregateResult> lst=[SELECT Gender__c, COUNT(id) FROM Contact where accountid IN:setOfAccountIdFromContact GROUP BY Gender__c];
for(Account objAccount :lstAccount)
{
for(AggregateResult objAggregateResult :lst)
{
if(objAggregateResult.gender__c='male')
{objAccount.Total_Male_Contacts__c=objAggregateResult.COUNT(objAggregateResult.gender__c);}
if(objAggregateResult.gender__c='female')
{ objAccount.Total_Female_Contacts__c=objAggregateResult.total_contact;}
}
}
update lstAccount;
}
}
}public class ContactTriggerHandler{
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
// updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
// updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{setOfAccountIdFromContact.add(eachCon.AccountId); }
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_Female_Contacts__c,Total_male_Contacts__c from Account
where Id IN : setOfAccountIdFromContact];
AggregateResult[] agrResult=[SELECT Gender__c, COUNT(id) total_contacts FROM Contact where accountid IN:setOfAccountIdFromContact GROUP BY Gender__c];
for(Account objAccount :lstAccount)
{
for(AggregateResult objAggregateResult :agrResult)
{
if(objAggregateResult.get('gender__c')='male')
{objAccount.Total_Male_Contacts__c=objAggregateResult.get('total_contacts');}
if(objAggregateResult.gender__c='female')
{ objAccount.Total_Female_Contacts__c=objAggregateResult.get('total_contact');}
}
}
update lstAccount;
}
}
}public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
// updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
// updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0 && setOfgenderFromContact='')
{
for(String objString :setOfgenderFromContact)
{
if(objString='male')
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
}
if(objString='female')
{
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
}
}
update lstAccount;
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_female_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
}
update lstAccount;
update lstAccount2;public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
updateNumberOfGenderContactsOnAccount(triggerOld, triggerOldMap);
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_female_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
update lstAccount;
update lstAccount2;
}
}
}public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
updateNumberOfGenderContactsOnAccount(triggerOld, triggerOldMap);
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c='male') from Account
where Id IN : setOfAccountIdFromContact];
List<Account> lstAccount2 = [select id, Name, Total_Contacts__c,Total_female_Contacts__c,(select id from contacts where gender__c='female') from Account
where Id IN : setOfAccountIdFromContact];
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
for(Account objAccount :lstAccount2)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
update lstAccount;
update lstAccount2;
}
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
for(String st:setOfgenderFromContact)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c IN:setOfgenderFromContact) from Account
where Id IN : setOfAccountIdFromContact];
if(setOfgenderFromContact='male')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
}
if(setOfgenderFromContact='female')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
}
update lstAccount;
}
}
}
private void OppurtunityUpdate()
{
Set<ID> setOfAccountID=new Set<ID>();
if(trigger.new!=null)
{
for(Opportunity objOpportunity:trigger.New)
{
if(objOpportunity.AccountID!=null)
{
setOfAccountID.add(objOpportunity.AccountID);
}
}
}
if(setOfAccountId != null && setOfAccountId.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id,name from contacts)
from Account where Id IN : setOfAccountId];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachOpportunity.Total_Contacts__c = (Opportunity)(eachAccount.contacts.size());
}
)
update lstAccount;
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeOpportunityUpdate(list<Opportunity> triggerOld, map<id,Opportunity> triggerOldMap)
{
onUpdateOfrOfOpportunity(triggerOld,triggerOldMap);
}
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
updateNumberOfGenderContactsOnAccount(triggerOld, triggerOldMap);
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
private void onUpdateOfrOfOpportunity(List<Opportunity> triggerOld,Map<id,Opportunity> triggerOldMap)
{
Set<ID> setOfAccountID=new Set<ID>();
if(triggerOld!=null)
{
for(Opportunity objOpportunity:triggerOld)
{
if(objOpportunity.AccountID!=null)
{
setOfAccountID.add(objOpportunity.AccountID);
}
}
}
if(setOfAccountId != null && setOfAccountId.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id,name from contacts)
from Account where Id IN : setOfAccountId];
Opportunity eachOpportunity=new Opportunity();
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachOpportunity.Total_Contacts_Linked__c = (eachAccount.contacts.size());
}
}
update lstAccount;
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
for(String st:setOfgenderFromContact)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c IN:setOfgenderFromContact) from Account
where Id IN : setOfAccountIdFromContact];
if(st=='male')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
}
if(st=='female')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
}
update lstAccount;
}
}
}
}/**
* An apex page controller that exposes the change password functionality
*/
public with sharing class ChangePasswordController {
public String oldPassword {get; set;}
public String newPassword {get; set;}
public String verifyNewPassword {get; set;}
public PageReference changePassword() {
return Site.changePassword(newPassword, verifyNewPassword, oldpassword);
}
public ChangePasswordController() {}
}/**
* An apex page controller that exposes the change password functionality
*/
@IsTest public with sharing class ChangePasswordControllerTest {
@IsTest(SeeAllData=true) public static void testChangePasswordController() {
// Instantiate a new controller with all parameters in the page
ChangePasswordController controller = new ChangePasswordController();
controller.oldPassword = '123456';
controller.newPassword = 'qwerty1';
controller.verifyNewPassword = 'qwerty1';
System.assertEquals(controller.changePassword(),null);
}
}/**
* An apex page controller that exposes the site forgot password functionality
*/
public with sharing class ForgotPasswordController {
public String username {get; set;}
public ForgotPasswordController() {}
public PageReference forgotPassword() {
boolean success = Site.forgotPassword(username);
PageReference pr = Page.ForgotPasswordConfirm;
pr.setRedirect(true);
if (success) {
return pr;
}
return null;
}
}/**
* An apex page controller that exposes the site forgot password functionality
*/
@IsTest public with sharing class ForgotPasswordControllerTest {
@IsTest(SeeAllData=true) public static void testForgotPasswordController() {
// Instantiate a new controller with all parameters in the page
ForgotPasswordController controller = new ForgotPasswordController();
controller.username = 'test@salesforce.com';
System.assertEquals(controller.forgotPassword(),null);
}
}/**
* An apex class that keeps updates of a portal user in sync with its corresponding contact.
Guest users are never able to access this page.
*/
@IsTest public with sharing class MyProfilePageControllerTest {
@IsTest(SeeAllData=true) static void testSetContactFields() {
User u = [select title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
Contact c = new Contact();
MyProfilePageController.setContactFields(c, u);
System.assertEquals(c.firstname, u.firstname, 'firstname should have been set as the firstname of the user for the contact');
System.assertEquals(c.lastname, u.lastname, 'lastname should have been set as the lastname of the user for the contact');
}
@IsTest(SeeAllData=true) static void testSave() {
// Modify the test to query for a portal user that exists in your org
List<User> existingPortalUsers = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null AND UserType='CustomerSuccess'];
if (existingPortalUsers.isEmpty()) {
User currentUser = [select id, title, firstname, lastname, email, phone, mobilephone, fax, street, city, state, postalcode, country
FROM User WHERE id =: UserInfo.getUserId()];
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(currentUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
Contact c = new Contact();
c.LastName = 'TestContact';
insert c;
c.title = currentUser.title;
c.firstname = currentUser.firstname;
c.lastname = currentUser.lastname;
c.email = currentUser.email;
c.phone = currentUser.phone;
c.mobilephone = currentUser.mobilephone;
c.fax = currentUser.fax;
c.mailingstreet = currentUser.street;
c.mailingcity = currentUser.city;
c.mailingstate = currentUser.state;
c.mailingpostalcode = currentUser.postalcode;
c.mailingcountry = currentUser.country;
controller.save();
System.assert(Page.ChangePassword.getUrl().equals(controller.changePassword().getUrl()));
} else {
User existingPortalUser = existingPortalUsers[0];
String randFax = Math.rint(Math.random() * 1000) + '5551234';
System.runAs(existingPortalUser) {
MyProfilePageController controller = new MyProfilePageController();
System.assertEquals(existingPortalUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
System.assert(controller.getIsEdit() == false, 'isEdit should default to false');
controller.edit();
System.assert(controller.getIsEdit() == true);
controller.cancel();
System.assert(controller.getIsEdit() == false);
controller.getUser().Fax = randFax;
controller.save();
System.assert(controller.getIsEdit() == false);
}
// verify that the user and contact were updated
existingPortalUser = [Select id, fax, Contact.Fax from User where id =: existingPortalUser.Id];
System.assert(existingPortalUser.fax == randFax);
System.assert(existingPortalUser.Contact.fax == randFax);
}
}
}public class opcontact{
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public opcontact(boolean isexecuting, integer size)
{
m_isexecuting=isexecuting;
batchsize=size;
}
public void onbeforeinsert(list<opportunity> triggernew,map<id,opportunity> triggernewmap)
{
updatelist(triggernew,triggernewmap);
}
private void updatelist(list<opportunity> triggernew,map<id,opportunity> triggernewmap)
{
set<id> sid=new set<id>();
if(triggernew!=null && triggernew.size()>0)
{
for(opportunity c: triggernew)
{
sid.add(c.accountid);
}}
if(sid!=null && sid.size()>0)
{
map<id,account> mapAccIdToAccount = new map<id,account>([select id,
(select id from contacts)
from account
where Id IN : sid]);
//list<opportunity> opc = [select id,no_of_contacts__c from opportunity];
if(triggernew!= null && triggernew.size() > 0)
{
for(opportunity eachopp : triggernew)
{
if(mapAccIdToAccount.get(eachopp.AccountId) != null)
{
Account objAcc = mapAccIdToAccount.get(eachopp.AccountId);
eachopp.NO_of_contacts__c = objAcc.contacts.size();
}
}
}
}
}
}/**
* An apex page controller that exposes the site login functionality
*/
global with sharing class SiteLoginController {
global String username {get; set;}
global String password {get; set;}
global PageReference login() {
String startUrl = System.currentPageReference().getParameters().get('startURL');
return Site.login(username, password, startUrl);
}
global SiteLoginController () {}
}/**
* An apex page controller that exposes the site login functionality
*/
@IsTest global with sharing class SiteLoginControllerTest {
@IsTest(SeeAllData=true) global static void testSiteLoginController () {
// Instantiate a new controller with all parameters in the page
SiteLoginController controller = new SiteLoginController ();
controller.username = 'test@salesforce.com';
controller.password = '123456';
System.assertEquals(controller.login(),null);
}
}/**
* An apex class that creates a portal user
*/
public with sharing class SiteRegisterController {
// PORTAL_ACCOUNT_ID is the account on which the contact will be created on and then enabled as a portal user.
// you need to add the account owner into the role hierarchy before this will work - please see Customer Portal Setup help for more information.
private static Id PORTAL_ACCOUNT_ID = '001x000xxx35tPN';
public SiteRegisterController () {
}
public String username {get; set;}
public String email {get; set;}
public String password {get; set {password = value == null ? value : value.trim(); } }
public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
private boolean isValidPassword() {
return password == confirmPassword;
}
public PageReference registerUser() {
// it's okay if password is null - we'll send the user a random password in that case
if (!isValidPassword()) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
ApexPages.addMessage(msg);
return null;
}
User u = new User();
u.Username = username;
u.Email = email;
u.CommunityNickname = communityNickname;
String accountId = PORTAL_ACCOUNT_ID;
// lastName is a required field on user, but if it isn't specified, we'll default it to the username
String userId = Site.createPortalUser(u, accountId, password);
if (userId != null) {
if (password != null && password.length() > 1) {
return Site.login(username, password, null);
}
else {
PageReference page = System.Page.SiteRegisterConfirm;
page.setRedirect(true);
return page;
}
}
return null;
}
}/**
* Class containing tests for SiteRegisterController
*/
@IsTest public with sharing class SiteRegisterControllerTest {
@IsTest(SeeAllData=true) static void testRegistration() {
SiteRegisterController controller = new SiteRegisterController();
controller.username = 'test@force.com';
controller.email = 'test@force.com';
controller.communityNickname = 'test';
// registerUser will always return null when the page isn't accessed as a guest user
System.assert(controller.registerUser() == null);
controller.password = 'abcd1234';
controller.confirmPassword = 'abcd123';
System.assert(controller.registerUser() == null);
}
}/**
* Class containing tests for SiteRegisterController
*/
@IsTest public with sharing class SiteRegisterControllerTest {
@IsTest(SeeAllData=true) static void testRegistration() {
SiteRegisterController controller = new SiteRegisterController();
controller.username = 'test@force.com';
controller.email = 'test@force.com';
controller.communityNickname = 'test';
// registerUser will always return null when the page isn't accessed as a guest user
System.assert(controller.registerUser() == null);
controller.password = 'abcd1234';
controller.confirmPassword = 'abcd123';
System.assert(controller.registerUser() == null);
}
}
Select recordtype.id,id,name from contact where recordtype.id='01290000000tdmAAAQ'
SELECT Gender__c, COUNT(id) FROM Contact GROUP BY Gender__c
SELECT Gender__c, COUNT(id) FROM Contact where accountid='0019000000njPGeAAM' GROUP BY Gender__c
0019000000njPGeAAM
list<Account> lstAccount =
[select id, Name, Total_Contacts__c,(select id from contacts) from Account
where Id IN : setOfAccountIdFromContact];
public class ContactTriggerHandler{
private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
}
/* private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}*/
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_Female_Contacts__c,Total_male_Contacts__c from Account
where Id IN : setOfAccountIdFromContact];
List<Contact> lstcontact=[SELECT Gender__c, COUNT(id) total_Contact FROM Contact where accountid IN:setOfAccountIdFromContact GROUP BY Gender__c];
for(Account objAccount :lstAccount)
{
for(Contact objContact :lstContact)
{
if(objContact.gender__c='male')
{objAccount.Total_Male_Contacts=objContact.total_contact;}
if(objContact.gender__c='female')
{ objAccount.Total_Female_Contacts=objContact.total_contact;}
}
}
}
update lstAccount;
}
}
Subscribe to:
Posts (Atom)