• Jagjeet Singh 13
  • NEWBIE
  • 30 Points
  • Member since 2016

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 2
    Likes Given
  • 0
    Questions
  • 7
    Replies
Hi,
I wrote an apex class for one Salesforce email service. I need that when an email arrive, case is created and email's attachemnts attach to the case created. When I test it, doesn't trhow errors or expetions, but also doesn't create case and attachment. Someone could please check my code and give me some tips please?

Here is my code
global class casewithattachments implements Messaging.InboundEmailHandler {
    
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ 
// Create an inboundEmailResult object for returning the result of the Force.com Email Service 
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); 
//List of email's Attachments
List <Attachment> attch = new List<Attachment>(); 
// Initialize the Contact ID, so if no Contact is found, a case is still created
Id vconID;     
//Case objects to be created 
Case newCase = new Case();   
// Try to lookup any contacts based on the email from address 
// If there is more than 1 contact with the same email address 
// an exception will be thrown and the catch statement will be called 
try { 
    Contact vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1]; 
// Add a new Case to the contact record we just found above - if not Contact found, value remains null 
    vconID = vCon.ID; 
// Insert the new Case and it will be created and appended to the contact record 
//newCase.Description = myPlainText;  
    newCase.Description = email.htmlBody; 
    newCase.Priority = 'Medium';
    newCase.Status = 'New';
    newCase.Subject = email.subject;
    newCase.Origin = 'Email'; 
    newCase.ContactId = vconID;
    insert newCase;
    System.debug('New Case Object: ' + newCase );     

// If there is an exception with the query looking up and the exception will be written to the Apex Debug logs 
catch (System.QueryException e) { 
    System.debug('Query Issue: ' + e);


    try{     
        for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
        attch.add(new Attachment(Name = tAttachment.fileName,Body = Blob.valueOf(tAttachment.body),ParentId = newCase.Id));} 

    
        for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
          attch.add(new Attachment(Name = bAttachment.fileName,Body = bAttachment.body,ParentId = newCase.Id));  } 
        System.debug('Attachemnt attached' );
    }
    catch (System.NullPointerException e) { 
            System.debug('Attachment Issue: ' + e);
}    
    
insert attch;
// Set the result to true, no need to send an email back to the user with an error message 
result.success = true; // Return the result for the Force.com Email Service 
return result; } 
}
Hi,
I wrote an apex class for one Salesforce email service. I need that when an email arrive, case is created and email's attachemnts attach to the case created. When I test it, doesn't trhow errors or expetions, but also doesn't create case and attachment. Someone could please check my code and give me some tips please?

Here is my code
global class casewithattachments implements Messaging.InboundEmailHandler {
    
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ 
// Create an inboundEmailResult object for returning the result of the Force.com Email Service 
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); 
//List of email's Attachments
List <Attachment> attch = new List<Attachment>(); 
// Initialize the Contact ID, so if no Contact is found, a case is still created
Id vconID;     
//Case objects to be created 
Case newCase = new Case();   
// Try to lookup any contacts based on the email from address 
// If there is more than 1 contact with the same email address 
// an exception will be thrown and the catch statement will be called 
try { 
    Contact vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1]; 
// Add a new Case to the contact record we just found above - if not Contact found, value remains null 
    vconID = vCon.ID; 
// Insert the new Case and it will be created and appended to the contact record 
//newCase.Description = myPlainText;  
    newCase.Description = email.htmlBody; 
    newCase.Priority = 'Medium';
    newCase.Status = 'New';
    newCase.Subject = email.subject;
    newCase.Origin = 'Email'; 
    newCase.ContactId = vconID;
    insert newCase;
    System.debug('New Case Object: ' + newCase );     

// If there is an exception with the query looking up and the exception will be written to the Apex Debug logs 
catch (System.QueryException e) { 
    System.debug('Query Issue: ' + e);


    try{     
        for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
        attch.add(new Attachment(Name = tAttachment.fileName,Body = Blob.valueOf(tAttachment.body),ParentId = newCase.Id));} 

    
        for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
          attch.add(new Attachment(Name = bAttachment.fileName,Body = bAttachment.body,ParentId = newCase.Id));  } 
        System.debug('Attachemnt attached' );
    }
    catch (System.NullPointerException e) { 
            System.debug('Attachment Issue: ' + e);
}    
    
insert attch;
// Set the result to true, no need to send an email back to the user with an error message 
result.success = true; // Return the result for the Force.com Email Service 
return result; } 
}
 I have a button that initiates this url: ....../apex/VSPageStandardControllerPagePainting?engId={!Engagement__c.Id }
 
In my Visualforce page,  how to I capture the engId so I can then use it in the code?    I think I use the following in some way,  but how?
 
 What is the complete code syntax?  
 
System.currentPageReference().getParameters().get('engId')
 
Your help is greatly appreciated.
Hi All,

My requirement is i have a "Description" field which contains 90chars. I need to store the first 45 chars in one formula field and next 45 chars in another formula field.

I tried with one i.e Formulafield1: TRIM(LEFT( Description , 45)). It is working fine.

Can i know any solution for the second formula field to display next 45 chars.

Thanks
 
hi,

I am new to Salesforce and trying to build app 'Build a Conference Management App'. I had written a simple class to send email in my previous trailhead modules and was able to receive email then but for some reason not receiving any email now.

Could anyone please help track the issue?

public with sharing class  EmailManager {
    //Public Method
        public static void sendMail(String address, String subject, String body) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};         
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
  }

I am trying to test above method by writing below code using 'Execute Anonymous Window' 

String address = 'adnansun2002@yahoo.co.in';
String subject = 'Speaker Confirmation';
String body = 'Thank you for speaking at the conference.';
EmailManager.sendMail(address, subject, body);


 

When someone takes the time/effort to repspond to your question, you should take the time/effort to either mark the question as "Solved", or post a Follow-Up with addtional information.  

 

That way people with a similar question can find the Solution without having to re-post the same question again and again. And the people who reply to your post know that the issue has been resolved and they can stop working on it. 

Hi All,

My requirement is i have a "Description" field which contains 90chars. I need to store the first 45 chars in one formula field and next 45 chars in another formula field.

I tried with one i.e Formulafield1: TRIM(LEFT( Description , 45)). It is working fine.

Can i know any solution for the second formula field to display next 45 chars.

Thanks
 

Posting this in order to help others who, months from now, might Google "OP_WITH_INVALID_USER_TYPE_EXCEPTION" and find this explanation.

 

We wrote an Apex trigger on the User object, to insert a custom object record anytime a user updates their Chatter status.  This was done to fulfill a client's requirement to audit all Chatter activity.

 

The trigger worked fine, until one day the client signed up some Chatter Free users.  When such a user tried to update their status, they got a pop-up with an OP_WITH_INVALID_USER_TYPE_EXCEPTION error.

 

We scratched our collective heads for awhile.  After all, Apex triggers run in "system mode," right?  That is supposed to mean that "object and field-level permissions of the current user are ignored."  And yet this trigger seemed like it was running in "user mode," enforcing restrictions based on who the current user was.

 

The root cause turned out to be that a Chatter Free user cannot be the owner of a custom object record, and SFDC by default sets the current user as a new record's first owner.  We discovered this when we realized, via experiment, that Apex triggers fired as the result of actions by Chatter Free users could definitely update an existing record, but were having problems creating records.

 

So the simple solution was to explicitly set the owner of the new record to some fully-licensed user prior to inserting it.