• Anton Flärd
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 2
    Replies
Hi
I'm in a tricky situation and have some questions.
  1. Running test synchronously make my tests pass, which ofcourse I understand since they are not including other test. But the questoin is, why are my Code Coverage not updated when running tests synchronously
  2. Running my test asynchronously is making 3/5 tests fail because of another class failing and that class is (hidden) when I try to open it, so I can't change it... the class failing is shc.BatchEmailSender and shc.shccontroller. Are these general or specific?
  3. I just got access to our Sandbox and I should have full access to everthing there, why are these classes still hidden? Might it be because they might come from AppExchange?
  4. How can I proceed in this matter? I really can't change the way my tests are done since they need to use DML Insert statements to create dummy data. Also tried directly with Database.Insert(sObject) statement but same classes fail.

Thanks in advance.
Hi

I'm in a tricky situation and have some questions.
  1.  Running test synchronously make my tests pass, which ofcourse I understand since they are not including other test. But the questoin is, why are my Code Coverage not updated when running tests synchronously
  2. Running my test asynchronously is making 3/5 tests fail because of another class failing and that class is (hidden) when I try to open it, so I can't change it... the class failing is shc.BatchEmailSender and shc.shccontroller. Are these general or specific?
  3. I just got access to our Sandbox and I should have full access to everthing there, why are these classes still hidden? Might it be because they might come from AppExchange?
  4. How can I proceed in this matter? I really can't change the way my tests are done since they need to use DML Insert statements to create dummy data. Also tried directly with Database.Insert(sObject) statement but same classes fail.
/Anton
 
Hi

I'm trying to extend the Email-To-Case functionality and I'm not sure why it's not working.
The purpose of this is to read two lines from an incoming email and depending on the data from Salesforce, either create a new case or update a case.

I've tried swapping between using EmailMessage and Messaging.InboundEmailHandler but both produce errors and I think it's related to my crappy code. I've just been writing code for 3 days now so I'm very new to APEX/SOQL.

1) I'm pretty sure the RegEx is working since I've tested that in Execute Anonymous Window
2) How can I make this work?
3) In my APEX Class, what should i return when the statements in the code are validated?
4) As it is right now, I don't even know if my code is being triggered. How can I check this?
5) I still need to figure out how to post a message to a user and the Chatter implementation might not be working right now. Is it?

This is my trigger:
trigger op5EmailTrigger on EmailMessage (before insert) {
    op5EmailHandler.getEmailData(Trigger.new);
}

This is my APEX class:
 
global class op5EmailHandler {
    public static void getEmailData(EmailMessage messageList) {
            for(EmailMessage email : messageList) {
            try {
                if(email.fromAddress == 'hidden@for.now') {
                    String emailSubject = email.Subject;
                    String emailBody = email.TextBody;
                    Pattern assetPattern = Pattern.compile('((e)\\: )(.*)');
                    Pattern serialPattern = Pattern.compile('((l)\\: )(.*)');
                    
                    // define separate matchers for asset and serial
                    Matcher assetMatcher = assetPattern.matcher(emailBody);
                    Matcher serialMatcher = serialPattern.matcher(emailBody);
                    
                    if(assetMatcher.find() && serialMatcher.find()) {
                        String asset = assetMatcher.group(3);
                        String serial = serialMatcher.group(3);
                        Asset findAsset = [SELECT Id,AccountId FROM Asset WHERE (Serial_No__c=:serial AND Name=:asset)];
                        
                        String assetAccount = findAsset.AccountId;
                        String assetFound = findAsset.Id;
                        // array, as it may include multiple cases
                        Case[] returnedCases = [SELECT Id,CaseNumber FROM Case WHERE (AssetId=:assetFound AND Origin='Monitoring OP5' AND (Status!='Closed' OR Status!='No fault found') AND Type='Incident')];
                        
                        if(returnedCases.size() == 2) {
                            // create a new case
                            Case newCase = new Case(Subject=emailSubject, AccountId=assetAccount, AssetId=assetFound, Description=emailBody);
                            
                            insert newCase;
                            // post to chatter
                            String multipleCasesExists = 'There is multiple open cases for Asset: ' + assetMatcher.group(1) + ' with S/N: ' + serialMatcher.group(1) + ' and a new case has been created with Case Number: ' + newCase.CaseNumber + '. Please delete the cases that are irrelevant and use one case to track the alerts for this Asset.';
                            ConnectApi.FeedElement sendToChatter = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), '0F925000000049v', ConnectApi.FeedElementType.FeedItem, multipleCasesExists);
                            
                            
                        } else if(returnedCases.size() == 1) {
                            EmailMessage newEmail = new EmailMessage(FromAddress = email.FromAddress,
                                                                     FromName = email.FromName,
                                                                     ToAddress = email.ToAddress,
                                                                     Subject = email.Subject,
                                                                     TextBody = email.TextBody,
                                                                     HtmlBody = email.HtmlBody,
                                                                     Incoming = true,
                                                                     MessageDate = System.now(),     
                                                                     ParentId = returnedCases[0].Id);
                            
                            insert newEmail;
                            
                            
                            // post to chatter
                            String chatterMessage = 'New email from OP5 have been inserted into case: ' + returnedCases[0].CaseNumber + '. Please investigate if further action is needed.';
                            ConnectApi.FeedElement sendToChatter = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), '0F925000000049v', ConnectApi.FeedElementType.FeedItem, chatterMessage);
                            
                            // send email to case owner
                            
                            
                        } else {
                            // create new case
                            Case newCase = new Case(Subject=emailSubject, AccountId=assetAccount, AssetId=assetFound, Description=emailBody);
                            
                            insert newCase;
                            // post to chatter
                            String newCaseFromOP5 = 'New case: ' + newCase.CaseNumber + ' for Asset: ' + assetMatcher.group(1) + ' with S/N: ' + serialMatcher.group(1) + ' has been created, please take action asap.';
                            ConnectApi.FeedElement sendToChatter = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), '0F925000000049v', ConnectApi.FeedElementType.FeedItem, newCaseFromOP5);
                            
                        }
                    }
                }
                return email;
            } catch (Exception e) {
                return email;
            }
        }
    }
}

All I've done is read the error messages produced in the Developer Console. I havn't used the Execute Anonymous Window since I don't know how to with a Email.

Many many thanks in advance!

 
Hi

Seen code samples around the web and on this site either using EmailMessage or Messaging.InboundEmailHandler and I'm not sure which one to use.

What is their differences?
Unable to query the field SerialNumber from table Asset. I'm administrator and all is visible/editable.
Error:
SELECT Id,Name,Account.Name,SerialNumber FROM Asset
                            ^
ERROR at Row:1:Column:29
No such column 'SerialNumber' on entity 'Asset'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

Any ideas?
Hi
I'm in a tricky situation and have some questions.
  1. Running test synchronously make my tests pass, which ofcourse I understand since they are not including other test. But the questoin is, why are my Code Coverage not updated when running tests synchronously
  2. Running my test asynchronously is making 3/5 tests fail because of another class failing and that class is (hidden) when I try to open it, so I can't change it... the class failing is shc.BatchEmailSender and shc.shccontroller. Are these general or specific?
  3. I just got access to our Sandbox and I should have full access to everthing there, why are these classes still hidden? Might it be because they might come from AppExchange?
  4. How can I proceed in this matter? I really can't change the way my tests are done since they need to use DML Insert statements to create dummy data. Also tried directly with Database.Insert(sObject) statement but same classes fail.

Thanks in advance.
Unable to query the field SerialNumber from table Asset. I'm administrator and all is visible/editable.
Error:
SELECT Id,Name,Account.Name,SerialNumber FROM Asset
                            ^
ERROR at Row:1:Column:29
No such column 'SerialNumber' on entity 'Asset'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

Any ideas?