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

Trigger to create a case from an email keeps erroring

I am having a really hard time with one piece of code. I have created a class to take incoming emails and create a case. A trigger will fire before insert that reads the email and updates fields in the case with the appropriate values. I keep getting an error on the following code.


integer  EmailAddressMatchIdx  =  c.Description.indexOf(  EMAILADDRESS_TXT  );
string  EmailAddress_Value  = c.Description.substring(  EmailAddressMatchIdx  + EMAILADDRESS_TXT.length()  );
c.Consumer_Contact_Email__c = EmailAddress_Value;

integer  IPAddressMatchIdx  =  c.Description.indexOf(  IPADDRESS_TXT  );
string  IPAddress_Value  = c.Description.substring(  IPAddressMatchIdx  + IPADDRESS_TXT.length()  );
c.IP_Address__c = IPAddress_Value;
integer  Address1MatchIdx  =  c.Description.indexOf(  ADDRESS1_TXT  );
string  Address1_Value  = c.Description.substring(  Address1MatchIdx  + ADDRESS1_TXT.length()  );
c.Address_1__c = Address1_Value;

integer  Address2MatchIdx  =  c.Description.indexOf(  ADDRESS2_TXT  );
string  Address2_Value  = c.Description.substring(  Address2MatchIdx  + ADDRESS2_TXT.length()  );
c.Address_2__c = Address2_Value;

This is the error that I am getting:
 >>> >>> (Undelivered): 554 System.DmlException: Insert failed. First >>> exception on row 0; first error: >>> CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CheckFieldValues: data changed >>> by trigger for field Consumer Contact Email: invalid email address: >>> | Address 1: (Senders address) | Address >>> 2: (Recipient Address) | End:End | this is the actual test of the

>>> script Class.cases.handleInboundEmail: line 41, column


Is your code able to catch the email in your org.

(using Emailservice - i faced a loddds of problem using Emailservice)


so my question here is does your code on email catch is working -


if yes then  please check the permissions for this object

The error you provided means


You do not have permission to create, update, or activate the specified record.



* there was a error in - So in production was treating this lonnnng email

   id as spam in your case


The email opens a case in my org using email services. My goal is to have the trigger read the email and fill in the appropriate fields in the case. Example: Email says the incident reason is Order not Recieved, then the field Incident Reason is filled in with Order not Recieved.


The object Cases has full permissions.


That doesn't make sence, that email address is the one given to me by Salesforce when I set up the email services in Sandbox.


I have a friend helping me with this. His theory is that when the trigger reads the email it doesn't know when to stop looking for the values.


This is the entire trigger code:



trigger CheckFieldValues on Case (before insert) {

private String  PRIMARYINVOICE_TXT = 'Primary Invoice Number:';
private String  PRIMARYINVOICE_STOP = '|Primary Invoice Number:';
private String  CLIENT_TXT  =  'Client with Issue:';
private String  CLIENT_STOP  =  '|Client with Issue:';
private String  PHONENUMBER_TXT  =  'Consumer Contact Phone Number:';
private String  PHONENUMBER_STOP  =  '|Consumer Contact Phone Number:';
private String  EMAILADDRESS_TXT  =  'Consumer Contact Email:';
private String  EMAILADDRESS_STOP =  '|Consumer Contact Email:';
private String  IPADDRESS_TXT  =  'IP Address:';
private String  IPADDRESS_STOP  =  '|IP Address:';
//private String  ORDERAMOUNT_TXT  =  'Order Amount:';
//private String  ORDERAMOUT_STOP  =  '|Order Amount:';
private String  ADDRESS1_TXT  =  'Address 1:';
private String  ADDRESS1_STOP  =  '|Address 1:';
private String  ADDRESS2_TXT  =  'Address 2:';
private String  ADDRESS2_STOP  =  '|Address 2:';
private String ENDOFBODY = '| End:End';

    for (Case c : {
        if (c.Subject == null) {
            c.addError('Subject cannot be blank!'); 
(c.Priority = 'P2');
(c.IncidentAssignTo__c = 'Customer Service');
(c.subject = 'Credit Verification 1');

integer  PrimaryInvoiceMatchIdx  =  c.Description.indexOf(  PRIMARYINVOICE_TXT  );
string  PrimaryInvoice_Value  = c.Description.substring(  PrimaryInvoiceMatchIdx  + PRIMARYINVOICE_TXT.length()  );
c.Primary_Invoice_Number__c = PrimaryInvoice_Value;

integer  ClientMatchIdx  =  c.Description.indexOf(  CLIENT_TXT  );
string  Client_Value  = c.Description.substring(  ClientMatchIdx  + CLIENT_TXT.length()  );
c.Client__c = Client_Value;

//integer  OrderAmountMatchIdx  =  c.Description.indexOf(  ORDERAMOUNT_TXT  );
//string  OrderAmount_Value  = c.Description.substring(  OrderAmountMatchIdx  + ORDERAMOUNT_TXT.length()  );
//c.Order_Amount__c = Decimal.valueOf(OrderAmount_Value);

integer  PhoneNumberMatchIdx  =  c.Description.indexOf(  PHONENUMBER_TXT  );
string  PhoneNumber_Value  = c.Description.substring(  PhoneNumberMatchIdx  + PHONENUMBER_TXT.length()  );
c.Consumer_Contact_Phone_Number__c = PhoneNumber_Value;

integer  EmailAddressMatchIdx  =  c.Description.indexOf(  EMAILADDRESS_TXT  );
string  EmailAddress_Value  = c.Description.substring(  EmailAddressMatchIdx  + EMAILADDRESS_TXT.length()  );
c.Consumer_Contact_Email__c = EmailAddress_Value;

integer  IPAddressMatchIdx  =  c.Description.indexOf(  IPADDRESS_TXT  );
string  IPAddress_Value  = c.Description.substring(  IPAddressMatchIdx  + IPADDRESS_TXT.length()  );
c.IP_Address__c = IPAddress_Value;
integer  Address1MatchIdx  =  c.Description.indexOf(  ADDRESS1_TXT  );
integer  StopIdx  =  c.Description.indexOf(  ADDRESS2_STOP  );
//string  Address1_Value  = c.Description.substring(  Address1MatchIdx  + ADDRESS1_TXT.length(), StopIdx –(Address1MatchIdx  + ADDRESS1_TXT.length()) );
//c.Address_1__c = Address1_Value;

integer  Address2MatchIdx  =  c.Description.indexOf(  ADDRESS2_TXT  );
string  Address2_Value  = c.Description.substring(  Address2MatchIdx  + ADDRESS2_TXT.length()  );
c.Address_2__c = Address2_Value;

Rasmus MenckeRasmus Mencke
Are you trying to send an outbound email from the trigger? or is this just inbound emails you are processing?

Just inbound emails are being processed. We have an order tool for the company and we are going to set it up where the order tool sends an email template that I write up containing the information from the order. The email will then create the case and populate the fields. The email template is below:


Subject Line: Credit Verification Body of EmailCreated By: (CS User Name) Incident Assign To: Client with Issue: Primary Invoice Number:  Order Amount: Consumer Contact Phone Number: IP Address: Consumer Contact Email: Address 1:(Senders address) Address 2:(Recipient Address)




Can somebody assist with the comment above?


You do not have permission to create, update, or activate the specified record.


* there was a error in - So in production was treating this lonnnng email

   id as spam in your case

How do I fix these two issues?

The last test didn't yield any additional errors but these two.