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.

We are building an email handler to import transcripts from a mibew-based chat system into our salesforce instance.

This is the code:

/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class emailHandlerChat implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();


    //  try 
        {
            string myEmailText = email.plainTextBody;
            String[] emailBodyArray = email.plainTextBody.split('\n', 0);

            integer lineNumber = 0;
            boolean foundStart = false;
            string emailLine;
            boolean contactExists;
            string transcript;

            map <String, String> params = new map <String, String>();

            // iterate over the headers till we get to the start of the message
            while (emailLine != '--START--')
            {
                emailLine = emailBodyArray[lineNumber];
                lineNumber++;
            }

            // next line contains the email address
            emailLine = emailBodyArray[lineNumber];
            String[] lineArray = emailLine.split(':', 0);
            String VisitorEmailAddress = lineArray[lineArray.size() - 1];
            VisitorEmailAddress = VisitorEmailAddress.trim();
            system.debug ('>>>' + VisitorEmailAddress);

            // see if we have this person in our database already
            /*
            String soql = 'SELECT Id FROM Contact WHERE Email = : \'' + string.escapeSingleQuotes(VisitorEmailAddress) + '\' limit 1';
            system.debug('>>>' + soql + '<<<');
            list<contact> c = Database.query(soql);
            */
            list<Contact> c = [SELECT Name FROM Contact WHERE Email = 'xx____@gmail.com'];

            if (c.isEmpty() == false)  // i.e. the result set is not empty
            {
                contactExists = true;
                system.debug('>>> Contact exists');
            }
            else
            {
                contactExists = false;
                system.debug('>>> Contact does not exist');
            }

            // now just concatenate the rest of the file into the transcript
            while (lineNumber < emailBodyArray.size())
            {
                transcript += emailBodyArray[lineNumber];
                lineNumber++;
                system.debug(lineNumber);
            }

            // finished with the email - now use our local variables
            if (contactExists)
            {
                ChatTranscript__c ct = new ChatTranscript__c (Contact__c = c[0].Id, Transcript__c = transcript);
                insert ct;
            }
            else
            {
                // need to create a lead with a transcript
                // May also need to make sure the transcript stays with the 
                // lead when it gets converted.
            }
  /*            
                        // add the chat transcript to the existing contact record
                        // !!! except we don't have the transcript yet !!!
                        ChatTranscript__c ct = new ChatTranscript__c (Contact__c = c[0].Id, Transcript__c = 'Test');
                        //ct.Contact = c[0].Id;
                    }
                    else
                    {
                        // create a new lead with the transcript attached
                    }
*/          
/*
            for (integer i = 0; i < emailBodyArray.size(); i++)
            {
                string emailLine = emailBodyArray[i];
                if (emailLine == '--START--') {
                    foundStart = true;
                }

                if (foundStart) lineNumber++;

                if (lineNumber == 2)
                // extract visitor's email address
                { 
                    String[] line3array = emailLine.split(':', 0);
                    String VisitorEmailAddress = line3array[line3array.size() - 1];
                    system.debug ('>>>' + VisitorEmailAddress);

                    // see if we have this person in our database already
                    String soql = 'SELECT Id FROM Contact WHERE Email = \'' + VisitorEmailAddress + '\'';
                    list<contact> c = database.query(soql);
                    if (c.isEmpty() == false)  // i.e. the result set is not empty
                    {
                        // add the chat transcript to the existing contact record
                        // !!! except we don't have the transcript yet !!!
                        ChatTranscript__c ct = new ChatTranscript__c (Contact__c = c[0].Id, Transcript__c = 'Test');
                        //ct.Contact = c[0].Id;
                    }
                    else
                    {
                        // create a new lead with the transcript attached
                    }

                }                   
  */
          return result;             

        }






    }
}

I'd send you screen shot pics of what the workbench query looks like & the sfdc debug log, but as this is my first post here, I am not allowed to post images.

So the question is - why can we successfully run the query in workbench but it does not work when we test it in SFDC?

Any suggestions?

share|improve this question
    
What exactly is the question? –  Lex Jan 17 '13 at 14:38
add comment

1 Answer

up vote 2 down vote accepted

I am assuming the problem is with this SOQL query...

String soql = 'SELECT Id FROM Contact WHERE Email = : \'' + string.escapeSingleQuotes(VisitorEmailAddress) + '\' limit 1';

You can change it to this...

VisitorEmailAddress = String.escapeSingleQuotes(VisitorEmailAddress);
String soql = 'SELECT Id FROM Contact WHERE Email = :VisitorEmailAddress limit 1';

Even though the variable is within the String, Apex will find the referenced variable.

But really, you don't need to be using Dynamic SOQL, you could just do this to query the Contact.

List<Contact> contacts = [SELECT Id FROM Contact WHERE Email = :VisitorEmailAddress LIMIT 1];
share|improve this answer
    
Thanks Kevin! We managed to have a breakthrough because of this: For Apex code saved using Salesforce.com API version 24.0 and later, use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test didn’t create. Starting with Apex code saved using Salesforce.com API version 24.0,... salesforce.com/us/developer/docs/apexcode/Content/… SFDC is always growing and changing! Thanks for helping us stay on top! –  Paul Weaver Jan 17 '13 at 14:38
add comment

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.