+ Start a Discussion
Andrew AldisAndrew Aldis 

Convert a string variable to a Id in a test class.

I wrote a trigger that parses values from the Description field on our case object.  The purpose is to parse out values from our internal monitoring service to update fields and the appropriate accounts.  The parsing works and the trigger has worked consistantly in our sandbox but I cannot write a test class that passes.  I keep getting an the following error:  "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RmmEmailCaseUpdate: execution of AfterInsert
caused by: System.StringException: Invalid id:  
Trigger.RmmEmailCaseUpdate: line 51, column 1: []"  

The account I am using in the string is correct and always sets the account when I use the trigger in Sandbox but does not work when I write the test class.  My class and test class are below.



trigger RmmEmailCaseUpdate on Case (after insert) {
    
    if (trigger.isAfter){
        if (trigger.isUpdate || trigger.isInsert){
            
            //Create set of Opportunity Ids based on updated Opportunity/s
            
            Set <String> caseIds = new Set <String>();            
            for (Case ca : trigger.new){                
                caseIds.add(ca.ID);
            }            
            Map <String, case>matchingcasecomps = new Map <String, case> ();
            for(case c: [select Id, Subject, Description, SuppliedEmail, RMM_Alert_Date_and_Time__c , RMM_Action_Required__c, RMM_Computer_Name__c, RMM_Error_Message__c , 
                         RMM_Client_Location__c, RMM_Last_Contact_with_Player__c from case where Id in :caseIds])
            {                
                IF( c.SuppliedEmail == 'nocmonitoring@fourwindsinteractive.com' && c.subject.startswith('FWIRMM - Offline Players:')){
                    {
                        string body = c.Description;
                        string sub = c.Subject;
                        string fromAdd = c.SuppliedEmail; 
                        dateTime alertDate = system.now();
                        string action;
                        string computer;
                        string alert;
                        string location;
                        string contact;
                        string accRmm;
                        string accTrimFront;
                        string accTrimRear;                       
                        Id accId;
                                                
                        List<string> parse = body.split('\\n');
                        alert = parse[0];             
                        computer = parse[1];
                        location = parse[3];
                        contact = parse[4];
                        action = parse[5]; 
                        accRmm = parse[8];
                        accTrimFront = accRmm.replaceFirst('\\[(?:rmm)\\:', '');
                        accTrimRear = accTrimFront.replaceFirst('\\:(?:rmm)\\]', '');
                        string accString = accTrimRear;
                       
                        /*List<Account> accQuery = [Select Id from Account Where Id =:accString];
                        FOR(Account accQ: accQuery){
                        IF(accQuery[0] != Null){  
                            accId = [Select Id from Account Where Id =:accString].Id;
                        }else{
                            accId='';
                        }}*/
                        
                        accId = Id.valueOf(accString);
                        c.RMM_Alert_Date_and_Time__c = system.now();
                        c.RMM_Action_Required__c = action;
                        c.RMM_Computer_Name__c = computer;
                        c.RMM_Error_Message__c = alert;
                        c.RMM_Client_Location__c = location;
                        c.RMM_Last_Contact_with_Player__c = contact;
                        c.AccountId = accId;            
                        update c;
                    }

@isTest(seealldata = true)
public class RmmEmailCaseUpdateTest {
static testMethod void RmmEmailCaseUpdate(){


    Case c = new Case(
    subject ='FWIRMM - Offline Players:',
    Description = 'Alert issue - Content Player PC is unreachable \r\n Computer - CLTSD \r\n PC Type/Serial Number - FWPW-QUAD-WIFI-SW / GIG41327 \r\n  \r\n Alert Date and Time - 4/25/2017 3:17:27 AM Client/Location - SpringHill Suites / SpringHill Suites Site \r\n Last contact with Player: ~4/25/2017 12:29:52 AM id 4113~ FAILED \r\n \r\n ACTION REQUIRED - Investigate computer logs. If no fault found, contact client to get PC back online. \r\n \r\n [rmm:0018000000MEgf1:rmm]',
    Origin = 'NOC',
    Status = 'New',
    RecordTypeId = '0123400000046GJ',
    SuppliedEmail = 'nocmonitoring@fourwindsinteractive.com');{
    insert c;  
        }
}     
}
Best Answer chosen by Andrew Aldis
Eric PepinEric Pepin
Parsing the Description is still the issue here. Your system.debug shows that you are almost there, but the first character in accString is a space, and thus the error when trying to convert to Id. In your test method, when you specify Description, remove the spaces from in front of: [rmm:0013000000I0wHY:rmm] (there may be 2 of them). Once the spaces are removed, it will parse correctly and you will have a valid Id.

All Answers

Eric PepinEric Pepin
In your test method, you are inserting a Description that does not parse properly. parse[8] (which is supposed to represent your Id) is actually blank. You will need to clean up the Description in your test method to match that of the actual use case.

parse[0]: [Alert issue - Content Player PC is unreachable ]
parse[1]: [ Computer - CLTSD ]
parse[2]: [ PC Type/Serial Number - FWPW-QUAD-WIFI-SW / GIG41327 ]
parse[3]: [ ]
parse[4]: [ Alert Date and Time - 4/25/2017 3:17:27 AM Client/Location - SpringHill Suites / SpringHill Suites Site ]
parse[5]: [ Last contact with Player: ~4/25/2017 12:29:52 AM id 4113~ FAILED ]
parse[6]: [ ]
parse[7]: [ ACTION REQUIRED - Investigate computer logs. If no fault found, contact client to get PC back online. ]
parse[8]: [ ]
parse[9]: [ [rmm:0018000000MEgf1:rmm]]
Andrew AldisAndrew Aldis
I got the parsing straighted out, and it still does the same thing???
Eric PepinEric Pepin
Unless you post the changes that you made, there will be no way to tell what the new issue is.
Andrew AldisAndrew Aldis
Good point.

trigger RmmEmailCaseUpdate on Case (after insert) {
    
    if (trigger.isAfter){
        if (trigger.isUpdate || trigger.isInsert){
            
            //Create set of Opportunity Ids based on updated Opportunity/s
            
            Set <String> caseIds = new Set <String>();            
            for (Case ca : trigger.new){                
                caseIds.add(ca.ID);
            }            
            Map <String, case>matchingcasecomps = new Map <String, case> ();
            for(case c: [select Id, Subject, Description, SuppliedEmail, RMM_Alert_Date_and_Time__c , RMM_Action_Required__c, RMM_Computer_Name__c, RMM_Error_Message__c , 
                         RMM_Client_Location__c, RMM_Last_Contact_with_Player__c from case where Id in :caseIds])
            {                
                IF( /*c.SuppliedEmail == 'nocmonitoring@fourwindsinteractive.com' && */c.subject.startswith('FWIRMM - Offline Players:')){
                    {
                        //variables
                        string body = c.Description;
                        string sub = c.Subject;
                        string fromAdd = c.SuppliedEmail; 
                        dateTime alertDate = system.now();
                        string action;
                        string computer;
                        string alert;
                        string location;
                        string contact;
                        string accRmm;
                        string accTrimFront;
                        string accTrimRear;   
                        string pcType;
                        Id accId;
                     //parsing and setting variables                     
                        List<string> parse = body.split('\\n');
                        alert = parse[0]; 
                        system.debug('Alert= '+alert);
                        computer = parse[1];
                        system.debug('computer= '+computer);
                        pcType = parse[2];
                        system.debug('pcType= '+pcType);
                        alert = parse[3];
                        system.debug('alert= '+alert);
                        location = parse[4];
                        system.debug('location= '+ location);
                        contact = parse[5]; 
                        system.debug('contact= '+contact);
                        action = parse[7];
                        system.debug('action= '+action);

                        accRmm = parse[9];
                        system.debug('accRmm= '+accRmm);
                        accTrimFront = accRmm.replaceFirst('\\[(?:rmm)\\:', '');
                        system.debug('accTrimFront= '+accTrimFront);
                        accTrimRear = accTrimFront.replaceFirst('\\:(?:rmm)\\]', '');
                        system.debug('accTrimRear= '+accTrimRear);
                        string accString = accTrimRear;
                        system.debug('accString= '+accString);
                       accId = Id.valueOf(accString);
                       system.debug('accId= '+accId);
                       
                        
                        c.RMM_Alert_Date_and_Time__c = alertDate;
                        c.RMM_Action_Required__c = action;
                        c.RMM_Computer_Name__c = computer;
                        c.RMM_Error_Message__c = alert;
                        c.RMM_Client_Location__c = location;
                        c.RMM_Last_Contact_with_Player__c = contact;
                        c.RMM_PC_Type_Computer__c = pcType;
                        c.AccountId = accId;            
                        update c;
                    }
                    
                }
            }        
        }}
}
@isTest(seealldata = true)
public class RmmEmailCaseUpdateTest {
static testMethod void RmmEmailCaseUpdate(){
    Case c = new Case(
    subject ='FWIRMM - Offline Players:',
    Description = 'Alert issue - Content Player PC is unreachable\r\n  Computer - CLTSD \r\n  PC Type/Serial Number - FWPW-QUAD-WIFI-SW / GIG41327 \r\n  Alert Date and Time - 4/25/2017 3:17:27 AM \r\n Client/Location - SpringHill Suites / SpringHill Suites Site \r\n  Last contact with Player: ~4/25/2017 12:29:52 AM id 4113~ FAILED \r\n  \r\n  ACTION REQUIRED - Investigate computer logs. If no fault found, contact client to get PC back online. \r\n  \r\n  [rmm:0013000000I0wHY:rmm]',
    Origin = 'NOC',
    Status = 'New',
    RecordTypeId = '0123400000046GJ',
    SuppliedEmail = 'nocmonitoring@fourwindsinteractive.com');{
    insert c;  
        }
    }     
}
Eric PepinEric Pepin
Parsing the Description is still the issue here. Your system.debug shows that you are almost there, but the first character in accString is a space, and thus the error when trying to convert to Id. In your test method, when you specify Description, remove the spaces from in front of: [rmm:0013000000I0wHY:rmm] (there may be 2 of them). Once the spaces are removed, it will parse correctly and you will have a valid Id.
This was selected as the best answer
Andrew AldisAndrew Aldis
@EricPepin You sir are the man, thank  you so much for your help and enjoy your best answer.