Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm looking to write an Apex script, but I literally know nothing about programming in Salesforce (with the exception of what I've read in the last hour). I want to write a script that does the following:

When a contact is created, check to see if a lead exists with the same email address. Only run script if lead source = "ExampleLeadSrc". If lead with matching email exists, change the status of the lead to Converted. I need this to run every time a contact is created, or on a short timed interval, like 10 minutes.

Any pointers on how to get this done? Also, is it safe to test code in Salesforce Sandbox? I'd like to make sure that I don't crash Salesforce if there are any mistakes in my code.

Thanks for your help!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

To get started with Programming on the Salesforce platform, please refer to a different post - Learning development with Force platform. Any good books?:


You are going to want to start with Force.com Fundamentals. This doesn't focus on Apex/Visualforce, but rather than base platform. Coding in Salesforce doesn't happen right away. You need to have a great understanding of the platform before you jump into the code, otherwise you will code things that you really don't need to code.

Once you do that, you can begin to learn Apex using Salesforce's provider instructional material. Specifically, the Force.com Workbook, Apex Workbook, and then supplimenting that with the Cookbook.

As well as learning Apex, take some time to figure out Visualforce as well, the two go hand in hand.

For reference:

Just as a final note, check out Clean Code by Robert Martin. It has nothing to do with Apex/Visualforce, but it is a great read for any programmer.


Now, to address your concern of crashing Salesforce, there is absolutely no way you could do that. This is true for several reasons:

So, long story short, have fun trying out your code with absolutely no worries. You will not be the straw that breaks the camel's back!


Let's take a look at your original problem. For this type of work, you will need to write a trigger. For a quick tutorial on how to do something like this, try the following quick tutorial provided by Salesforce. To help you get started, I will give you some tips:

For your scenario, you will need to run an after insert trigger on the contact. This trigger will then run every single time after a contact is inserted.

trigger ContactLeadConversionTrigger on Contact (after insert) {

}

Now we need to add some logic to that trigger. Based on your feedback, it would be something like:

trigger ContactLeadConversionTrigger on Contact (after insert) {
    List<String> conEmails = new List<String>();
    for(Contact con:Trigger.new){
        conEmails.add(con.Email);
    }
}

Here we have added logic to create a list of all contact email addresses. Now, you may be thinking, wouldn't there only be one contact email address? Well actually Salesforce triggers are run in batch, meaning that if you are using a data loader there could be thousands of contacts being updated at once. In that scenario, your trigger only runs once and loops over the list of contacts in the Trigger.new. Learn more about Trigger Context Variables to understand the purpose of Trigger.new.

From here, you are going to need to get any leads that match this criteria. Let's use Salesforce Object Query Language (SOQL) and what we already wrote to build this out:

trigger ContactLeadConversionTrigger on Contact (after insert) {
    List<String> conEmails = new List<String>();
    for(Contact con:Trigger.new){
        conEmails.add(con.Email);
    }

    List<Lead> leads = [
        SELECT 
            Id, Status, Source, Email 
        FROM 
            Lead 
        WHERE 
            Email IN :conEmails 
            AND Source = 'ExampleLeadSrc'
    ];
}

Using the proper SOQL SELECT syntax, we were able to get a List of leads. We were able to filter these leads by using a WHERE clause.

Finally, we need to make sure those leads are correctly updated. Let's update the Status and make sure we update those leads:

trigger ContactLeadConversionTrigger on Contact (after insert) {
    List<String> conEmails = new List<String>();
    for(Contact con:Trigger.new){
        conEmails.add(con.Email);
    }

    List<Lead> leads = [
        SELECT 
            Id, Status, Source, Email 
        FROM 
            Lead 
        WHERE 
            Email IN :conEmails 
            AND Source = 'ExampleLeadSrc'
    ];

    for(Lead singleLead:leads){
        singleLead.Status = 'Converted';
    }

    update leads;
}

In this final iteration we looped over all of the leads we got, updated their Status in memory and then committed those changes to the database using the update DML statement. Please read up on Apex Data Manipulation Language (DML) Operations to understand exactly how changes are committed.

Now that you have your trigger up and running, you will need to test it. Salesforce provides a good introduction to unit test methods.

Finally, please note that the code above was written purely on this site and I have no idea if it will actually compile. I did it purely to help give you the beginning direction on the type of thing you want to do. What I have written was for educational purposes only and I would suggest you using it as a mere guide rather than trying to get it to compile from scratch. Good luck and welcome to the Salesforce Stackexchange!


Finally, this type of question generally isn't answered that well on this site. This question is rather broad for this type of format (Q/A style). Please read up on some Tips and Tricks for Asking a Good Question?

share|improve this answer
2  
This is an incredibly thorough explanation. Thank you so much for your help. I can honestly say you've given me everything I need to get started with this project. I only wish I could give you an upvote... –  Jeremiah Prummer Feb 8 '13 at 19:15
    
No worries buddy. I'm glad I could help. Stick around the site a bit and you will be able to upvote in no time! –  Jesse Altman Feb 8 '13 at 19:17
1  
@JeremiahPrummer Upvoted for this great explanation! –  Mohith Shrivastava Feb 8 '13 at 19:21
1  
Ha, thanks Mohith! I have a couple other Stackexhange accounts and one of the worst things about setting up another one is having to get to 15 reputation to upvote on every account. –  Jeremiah Prummer Feb 8 '13 at 19:22
    
Just wanted to let you know that I finally got around to testing this today. It works beautifully, just had to add IsConverted =false to the WHERE clause. –  Jeremiah Prummer Feb 11 '13 at 22:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.