function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rbensonrbenson 

Trigger Help Needed

We have a custom object on the task field called Activity Type for the different types of activites our sales team performs - i.e. adding a new contact, sending a letter, etc.

 

What I am trying to accomplish is writing a class or trigger so that every time a new contact is added a trigger is fired to add a "log a call" and associate the activity type "1 - identify a new contact"

 

Any ideas? I have tried an if, then code and a few other triggers but nothing has worked.

 

Thanks!

Rachel

Jeremy.NottinghJeremy.Nottingh

If you have already started on some Trigger code, post what you have. That will make it easier to offer suggestions on what you still need to do.

 

Jeremy

rbensonrbenson

We have another code that triggers a different activity type when any type of email is sent. I wrote the code for Contact based off of that, but I am sure it is wrong because the trigger below is based on an activity that is already created and the new code needs to create the activity.

 

public class NewContactUpdater {
public static void NewContactPoints(){
List<Task> LTask = [select Activity_Type__c From Task when [Contact.FirstName] is inserted];
for (Task tempt: LTask){
Tempt.Activity_type__c = '1 - Identify HSE or procurement contact for a prospect';
}
update LTask;
}
}

Jeremy.NottinghJeremy.Nottingh

Well, the first thing you need to do is create a Trigger. If you're using the Eclipse IDE (and I recommend installing it if you haven't), make a blank Trigger file in your sandbox with File/New/Apex Trigger. It will ask what the Trigger is called (whatever you like, but make it clear what it does: e.g. "NewTaskOnNewContact", object you need it to fire on (Contact) and when you want it to fire (after insert). It will automatically create a file like this:

 

 

trigger NewTaskOnNewContact on Contact (after insert) {
	
}

 

 

So now, between the curly braces you need to put in your logic. The Apex documentation (included in Help Contents with the Eclipse IDE) has lots of good information to get you going on creating a Trigger.

 

See how far that gets you, and come back if you need more help.

 

Jeremy

rbensonrbenson

Great, thank you.  I will try this.

rbensonrbenson

I downloaded Eclipse 3.6 --- I couldnt find an older version. But it looks like Force.com is not supported on Eclipse 3.6 only 3.4 or 3.5. I keep getting the error below:

 

Cannot complete the install because one or more required items could not be found.

  Software being installed: Force.com IDE 19.0.0.201006111421 (com.salesforce.ide.feature.feature.group 19.0.0.201006111421)

  Missing requirement: Force.com IDE Core 19.0.0.201006111421 (com.salesforce.ide.core 19.0.0.201006111421) requires 'bundle org.eclipse.core.resources [3.3.0,3.6.0)' but it could not be found

  Cannot satisfy dependency:

    From: Force.com IDE 19.0.0.201006111421 (com.salesforce.ide.feature.feature.group 19.0.0.201006111421)

    To: com.salesforce.ide.core [19.0.0,20.0.0)

 

 

Any ideas?

Jeremy.NottinghJeremy.Nottingh

The links for the proper version of Eclipse are all right here:

http://wiki.developerforce.com/index.php/Force.com_IDE

 

This should lead you through getting the right files and installing.

 

Jeremy

rbensonrbenson

Okay so I got a little further. I am getting a compile error:

Error: Compile Error: Only top-level class methods can be declared static at line 2 column 20

 

trigger NewTaskOnNewContact on Contact (after insert) {
public static void NewTaskOnNewContact(){
List<Task> LTask = [select Activity_Type__c From Task where Who.FirstName <> null];
for (Task tempt: LTask){
Tempt.Activity_type__c = '1 - Identify HSE or procurement contact for a prospect'; }
update LTask;
}
}

Jeremy.NottinghJeremy.Nottingh

An Apex Trigger is not a class, and it doesn't define classes. I recommend spending some time reading the Apex Developer's Guide and looking at some sample Triggers to see what this should look like.

 

You will need to get your collection of inserted records using Trigger.new, iterate through them to build a collection of Tasks that will need to be inserted, and then insert those Tasks. Since you're only creating new Tasks, and not looking for Tasks already created, your code listed above will not apply.

 

Start with that blank Trigger, then work from examples to see what needs to be done further. Here's a hint: your first line should look something like

 

list<Contact> newcontacts = Trigger.new;

 

 

Jeremy