Skip to main content

I commented the homework code (including things I don't understand very well).

One thing I'm unsure of is how to try running code. I suppose we need some dummy data in our database before we can try running code? (Maybe not an issue for those of you who are real Salesforce peeps! :-)

Re: sample code on the web: I would say that if you can find sites that you trust it's probably more useful than if you just have to search around.

Anyway, here's my commented code:

//before a contact is updated or deleted, insert a "lead"

//(I'm not sure what a "lead" is)

trigger insertLead on Contact (before update, before delete) {

//create a new, empty set of IDs

Set<Id> aId = new Set<Id>();

//create a new, empty lead

Lead myLead = new Lead();

//loop through each contact (I'm unfamiliar with this )

//per developer documentation,

//Trigger.new 'returns a list of the new versions of the sObject records'

//developer documentation also says that "all triggers are bulk triggers by default"

//so I would say that Trigger.new is returning a series of contacts,

//and those contacts are being put in a variable called "opp" one at a time

//so that we can do something based on the information in each Contact

//but I DON'T know what "new" versions of an sObject means

for (Contact opp : Trigger.new )

{

//add a new ID to the ID set using the Contact Account ID field

aId.add(opp.AccountId);

//get the names out of current accounts; make a list of these

//so does a contact have an ID field that is an account ID so that you can join

//contacts and accounts?

List<Account> acc = [select Name from Account where Id in:aId];

//get the first and last names out of the current contacts; make a list of these

List<Contact> con = [select LastName,FirstName from Contact where accountId = :aId];

//now loop through the list of accounts to get all the company names

for(Account a: acc)

{

//set the company name in the "lead" to the company name from the account

myLead.Company =

a.Name

;

}

//not loop through the list of contacts to get all the first and last names

for(Contact c: con)

{

//set the first and last name in the "lead" to the first and last name from the contact

myLead.LastName = c.LastName;

myLead.FirstName = c.FirstName;

}

//put the lead into the database

//hmmm...does a "lead" have a unique id field? I'm wondering how this works in the DB

insert myLead;

}

}

//when a contact is updated or deleted, insert a "lead"

trigger insertLeadCorrected on Contact (before update, before delete) {

Set<Id> aId = new Set<Id>();

Lead myLead = new Lead();

//do one thing for an update

if (Trigger.isUpdate)

{

//go through all the "new" versions of Contact

for (Contact opp : Trigger.new )

{

aId.add(opp.AccountId);

List<Account> acc = [select Name from Account where Id in:aId];

List<Contact> con = [select LastName,FirstName from Contact where AccountId = :aId];

//put the company name (from account) and first and last names (from contact)

//into the new "lead"

for(Account a: acc)

{

myLead.Company =

a.Name

;

}

for(Contact c: con)

{

myLead.LastName = c.LastName;

myLead.FirstName = c.FirstName;

}

//and insert the lead in the DB

insert myLead;

}

}

//do a different thing for a delete

else

{

//if it's a delete, go through all the "old" versions of Contact

//(again, not sure what "new" and "old" versions are!)

for (Contact opp : Trigger.old )

{

//code from here is exactly the same as code in "if" part.

//I wonder if there's a way to write this only once and have just the

//Trigger.new and Trigger.old part in the "if" statement?

aId.add(opp.AccountId);

List<Account> acc = [select Name from Account where Id in:aId];

List<Contact> con = [select LastName,FirstName from Contact where AccountId = :aId];

//put the company name (from account) and first and last names (from contact)

//into the new "lead"

for(Account a: acc){

myLead.Company =

a.Name

;

}

for(Contact c: con)

{

myLead.LastName = c.LastName;

myLead.FirstName = c.FirstName;

}

//and insert into the DB

insert myLead;

}

}

}

0/9000