+ Start a Discussion
MaheemSamMaheemSam 

Test Class is failing while deploying in production with caused by: System.StringException: Invalid id:

Hi, 

 Below is the code which is working and has 94% code coverage in sandbox. When I try to deply the changeset to production. Please suggest me to fix this issue
 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityApprovelMatrixTrigger: execution of AfterInsert caused by: System.StringException: Invalid id: Test Sudhir Ac External entry point Trigger.OpportunityApprovelMatrixTrigger: line 22, column 1: [] 
Stack Trace: Class.OpportunityTriggerUtilsTest.OpportunityLineItemTriggerUtils_test1: line 44, column 1


System.DmlException: Update failed. First exception on row 0 with id 0063400001AcWEMAA3; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityApprovelMatrixTrigger: execution of AfterUpdate caused by: System.StringException: Invalid id: Test Sudhir Ac External entry point Class.OpportunityTriggerUtils.processOptySharingOppTeamUpdate: line 127, column 1 Trigger.OpportunityApprovelMatrixTrigger: line 27, column 1: [] 
Stack Trace: Class.OpportunityTriggerUtilsTest.OpportunityLineItemTriggerUtils_test2: line 80, column 1
 
  Trigger
trigger OpportunityApprovelMatrixTrigger on Opportunity (before insert, before update, after insert, after Update) {
      if(Trigger.isAfter) {
        if(Trigger.isInsert) {
            OpportunityTriggerUtils.processOptySharingOppTeam(Trigger.new); 
        }

        if(Trigger.isUpdate) {                            
            OpportunityTriggerUtils.processOptySharingOppTeamUpdate(Trigger.newMap, Trigger.oldMap);
        }
   }

  Class 
public class OpportunityTriggerUtils {

   public static void processOptySharingOppTeamUpdate(Map<id, Opportunity> newMap, Map<id, Opportunity> oldMap) {   //Karthi - you have to call this method from trigger, which is not called anywhere now.
        List<Opportunity> optyLst = new List<Opportunity>();
        for(Opportunity newRec : newMap.values()){
            Opportunity oldRec = oldMap.get(newRec.id);
            if(newRec.AccountID != oldRec.AccountID || newRec.Partner_Account__c != oldRec.Partner_Account__c)
                optyLst.add(newRec);
        }
        if(!optyLst.isEmpty())    
            processOptySharingOppTeam(optyLst);   
    }
    
    public static void processOptySharingOppTeam(List<Opportunity> newLst) { 
        List<id> OpptIds = new List<id>();
        list<OpportunityTeamMember> Otmlst = new List<OpportunityTeamMember>();
        Set<id> actIdset = new Set<id>(); 
        Set<id> paractIdset = new Set<id>(); 
        String memRolename = 'Global Account Shared Owner'; 
        
        for(Opportunity opp : newLst) {
            OpptIds.add(opp.id); 
        }
        
        list<Opportunity> Oplst = [select id,name,account.Top_Parent_Account__c,partner_account__r.Top_Parent_Account__c
                                    from opportunity where id in :OpptIds];
        
        for(Opportunity Op : Oplst){  
            actIdset.add(Op.account.Top_Parent_Account__c);
            paractIdset.add(Op.partner_account__r.Top_Parent_Account__c);                                                     
        }  
        
        list<Opportunity_Sharing__c> actOplst = new list<Opportunity_Sharing__c>([select id, Account_Names__c, Key_Text__c, Partner_Account_Names__c,
                                                                                  User__c,Access_Level__c,Global_Account_Rep__c,Top_Account_ID__r.id, Top_Partner_Account_ID__r.id,
                                                                                  User__r.name
                                                                                  from Opportunity_Sharing__c
                                                                                  where 
                                                                                  Top_Account_ID__r.id <> NULL and
                                                                                  Top_Account_ID__r.id in :actIdset and
                                                                                  User__c <> null and
                                                                                  Access_Level__c <> null
                                                                                 ]);  
        
        list<Opportunity_Sharing__c> paractOplst = new list<Opportunity_Sharing__c>([select id, Account_Names__c, Key_Text__c, Partner_Account_Names__c,
                                                                                     User__c,Access_Level__c,Global_Account_Rep__c,Top_Account_ID__r.id, Top_Partner_Account_ID__r.id,
                                                                                     User__r.name
                                                                                     from Opportunity_Sharing__c
                                                                                     Where Top_Partner_Account_ID__r.id <> null and 
                                                                                     Top_Partner_Account_ID__r.id in :paractIdset and
                                                                                     User__c <> null and
                                                                                     Access_Level__c <> null]);
        for(Opportunity Ops : Oplst){
            //Account 
            for(Opportunity_Sharing__c rec : actOplst){
                if(Ops.account.Top_Parent_Account__c == rec.Top_Account_ID__r.id){
                    system.debug('Opportunity Name: ' + ops.name + '  ' +  ops.id + '  ' +'Opportunity Sharing: ' + + rec.User__r.name);      
                    if(rec.User__c <> null && rec.Access_Level__c <> null){                      
                        OpportunityTeamMember otm = new OpportunityTeamMember(OpportunityId = ops.id,UserId=rec.User__c,OpportunityAccessLevel=rec.Access_Level__c
                                                                              ,TeamMemberRole= memRolename );  
                        Otmlst.add(otm);           
                    }
                    
                }
            }  
            //Partner Account 
            for(Opportunity_Sharing__c rec : paractOplst){
                if(Ops.partner_account__r.Top_Parent_Account__c == rec.Top_Partner_Account_ID__r.id){
                    system.debug('Opportunity Name: ' + ops.name + '  ' +  ops.id + '  ' +'Opportunity Sharing: ' + + rec.User__r.name);    
                    if(rec.User__c <> null && rec.Access_Level__c <> null){                      
                        OpportunityTeamMember otm = new OpportunityTeamMember(OpportunityId = ops.id,UserId=rec.User__c,OpportunityAccessLevel=rec.Access_Level__c
                                                                              ,TeamMemberRole= memRolename ); 
                        Otmlst.add(otm);           
                    }            
                }
            }  
        }
        
        if(!Otmlst.Isempty()){ 
            upsert Otmlst; 
        }  
        
    }   
 
}

Test Class
@isTest(seealldata=true)
public class OpportunityTriggerUtilsTest
{


 public static testMethod void OpportunityLineItemTriggerUtils_test1(){
  
   Account Act = new Account( Name = 'Test Sudhir Ac',Website='www.sudhir.com',Industry='Legal',BillingStreet='894', BillingCity='sunnyvalley', BillingState='CA', 
                             BillingPostalCode='997',BillingCountry='United States',Customer_Status__c='Current Customer');  

  insert Act; 
   
   Account Pact = [select id from account where recordtype.name = '.Partner' limit 1];
   
   User usr = [select id from user where id = '00580000004geN2' limit 1]; 
  
  Opportunity_Sharing__c ops = new Opportunity_Sharing__c (
          Key_Text__c = 'GMASJ',
          Top_Account_ID__c = Act.id,
          Top_Partner_Account_ID__c  = Pact.id,
          User__c = usr.id,
          Access_Level__c   = 'Edit',
          Global_Account_Rep__c = 'CAM' 
          );
          
  insert ops;        
 
  Contact C = [select id from contact limit 1];
      
  Opportunity opp = new Opportunity(
             AccountId=Ops.Top_Account_ID__c,
             StageName='Omit from Forecast',
             Amount = 999,
             Name = 'Test Sudhir',
             CloseDate = Date.today(),
             Market_Segmentation__c = 'Education',
             End_User_Industry__c = 'Education',
             End_Customer_Country__c = 'United States',
             Deal_Type__c='Refresh',
             Primary_Opportunity_Contact__c =  c.id,
             Partner_Account__c = ops.Top_Partner_Account_ID__c
             );
             
       insert opp;    
       
  Opportunity_Sharing__c ops1 = [select Top_Account_ID__c from Opportunity_Sharing__c where Top_Account_ID__c <> NULL limit 1];
  
  opp.accountid = ops1.Top_Account_ID__c;
  
  update opp;
          
 
  }

 public static testMethod void OpportunityLineItemTriggerUtils_test2(){
 
   Account Act = new Account( Name = 'Test Sudhir Ac',Website='www.sudhir.com',Industry='Legal',BillingStreet='894', BillingCity='sunnyvalley', BillingState='CA', 
                             BillingPostalCode='997',BillingCountry='United States',Customer_Status__c='Current Customer');  

  insert Act; 
    
   opportunity opp = [select id,accountid, Partner_Account__c from opportunity where closedate > today limit 1];
   
   //Opportunity_Sharing__c ops1 = [select Top_Account_ID__c from Opportunity_Sharing__c where Top_Account_ID__c <> NULL limit 1];
   
   User usr = [select id from user where id = '00580000004geN2' limit 1]; 
   
   Opportunity_Sharing__c ops = new Opportunity_Sharing__c (
          Key_Text__c = 'GMASJ',
          Top_Account_ID__c = Act.id,
          User__c = usr.id,
          Access_Level__c   = 'Edit',
          Global_Account_Rep__c = 'CAM' 
          );
          
    insert ops;  
  
   opp.accountid = ops.Top_Account_ID__c;
   
   update opp;
   
  
 }
 
 
   public static testmethod void testautolead()
{
 Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Inquiry',country='United States');

insert lead;                

 
}

}


 
Pradeep SinghPradeep Singh
Hi, You are using hardcoded IDs in your testclass. Please remove and try to query user using name or username if the user exists in production with same Name or Username.
MaheemSamMaheemSam
Hi Pradeep, 

     I removed hardcodded values it was only with user soql now i changed it to User usr = [select id from user where firstname = 'sudhir' limit 1]; 

   Issue is happening while inserting opportunity in line 44 
@isTest(seealldata=true)
public class OpportunityTriggerUtilsTest
{


 public static testMethod void OpportunityLineItemTriggerUtils_test1(){
  
   Account Act = new Account( Name = 'Test Sudhir Ac',Website='www.sudhir.com',Industry='Legal',BillingStreet='894', BillingCity='sunnyvalley', BillingState='CA', 
                             BillingPostalCode='997',BillingCountry='United States',Customer_Status__c='Current Customer');  

  insert Act; 
   
   Account Pact = [select id from account where recordtype.name = '.Partner' limit 1];
   
   User usr = [select id from user where id = '00580000004geN2' limit 1]; 
  
  Opportunity_Sharing__c ops = new Opportunity_Sharing__c (
          Key_Text__c = 'GMASJ',
          Top_Account_ID__c = Act.id,
          Top_Partner_Account_ID__c  = Pact.id,
          User__c = usr.id,
          Access_Level__c   = 'Edit',
          Global_Account_Rep__c = 'CAM' 
          );
          
  insert ops;        
 
  Contact C = [select id from contact limit 1];
      
  Opportunity opp = new Opportunity(
             AccountId=Ops.Top_Account_ID__c,
             StageName='Omit from Forecast',
             Amount = 999,
             Name = 'Test Sudhir',
             CloseDate = Date.today(),
             Market_Segmentation__c = 'Education',
             End_User_Industry__c = 'Education',
             End_Customer_Country__c = 'United States',
             Deal_Type__c='Refresh',
             Primary_Opportunity_Contact__c =  c.id,
             Partner_Account__c = ops.Top_Partner_Account_ID__c
             );
             
       insert opp;    
       
  Opportunity_Sharing__c ops1 = [select Top_Account_ID__c from Opportunity_Sharing__c where Top_Account_ID__c <> NULL limit 1];
  
  opp.accountid = ops1.Top_Account_ID__c;
  
  update opp;
          
 
  }

 public static testMethod void OpportunityLineItemTriggerUtils_test2(){
 
   Account Act = new Account( Name = 'Test Sudhir Ac',Website='www.sudhir.com',Industry='Legal',BillingStreet='894', BillingCity='sunnyvalley', BillingState='CA', 
                             BillingPostalCode='997',BillingCountry='United States',Customer_Status__c='Current Customer');  

  insert Act; 
    
   opportunity opp = [select id,accountid, Partner_Account__c from opportunity where closedate > today limit 1];
   
   //Opportunity_Sharing__c ops1 = [select Top_Account_ID__c from Opportunity_Sharing__c where Top_Account_ID__c <> NULL limit 1];
   
   User usr = [select id from user where id = '00580000004geN2' limit 1]; 
   
   Opportunity_Sharing__c ops = new Opportunity_Sharing__c (
          Key_Text__c = 'GMASJ',
          Top_Account_ID__c = Act.id,
          User__c = usr.id,
          Access_Level__c   = 'Edit',
          Global_Account_Rep__c = 'CAM' 
          );
          
    insert ops;  
  
   opp.accountid = ops.Top_Account_ID__c;
   
   update opp;
   
  
 }
 
 
   public static testmethod void testautolead()
{
 Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Inquiry',country='United States');

insert lead;                

 
}

}


Below is the debug log erorr details
 
02:59:44.512 (10512500629)|CUMULATIVE_LIMIT_USAGE_END

02:59:44.434 (10513726009)|CODE_UNIT_FINISHED|OpportunityApprovelMatrixTrigger on Opportunity trigger event AfterInsert
02:59:44.434 (10534089657)|DML_END|[44]
02:59:44.434 (10534218527)|EXCEPTION_THROWN|[44]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityApprovelMatrixTrigger: execution of AfterInsert

caused by: System.StringException: Invalid id: Test Sudhir Ac

External entry point
Trigger.OpportunityApprovelMatrixTrigger: line 22, column 1: []
02:59:44.434 (10535615985)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityApprovelMatrixTrigger: execution of AfterInsert

caused by: System.StringException: Invalid id: Test Sudhir Ac

External entry point
Trigger.OpportunityApprovelMatrixTrigger: line 22, column 1: []

Class.OpportunityTriggerUtilsTest.OpportunityLineItemTriggerUtils_test1: line 44, column 1
02:59:44.434 (10535660543)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityApprovelMatrixTrigger: execution of AfterInsert

caused by: System.StringException: Invalid id: Test Sudhir Ac

External entry point
Trigger.OpportunityApprovelMatrixTrigger: line 22, column 1: []

Please suggest this issue to fix. 

Thanks
Sudhir