• Ravi karlapudi
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hello Gurus,
I had a trigger(generates an email on department name change) and a test class related to this trigger.  The coverage was 100%. I changed the trigger to fire only when the profile is <> System Administrator.  The trigger is working fine as expected but the Test class is failing.  Here is the trigger code and followed by Test class code.
--------------------------------------------------------------
trigger SendEmailAfterDeptCodeUpdate on Department__c (after update) {
Profile p = [ Select Id, Name from Profile where Id =: UserInfo.getProfileId() ];
        if( !p.Name.equalsIgnoreCase( 'System Administrator' ) ) 
        {
  List<Id> lstdeptsToUpdate=new List<Id>(); 
for(Department__c dept : trigger.new)
{
    If (dept.Name !=trigger.oldMap.get(dept.Id).Name)
    {
    lstdeptsToUpdate.add(dept.Id);
    }
}
    
    If(lstdeptsToUpdate.size()>0)
    {
    Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
    
   // string header = '*TR Customer Code, *Customer Name,*TR Department ID,*Original Department Code,*New Department Code,*SubBranchCode,TR Customer Id \n';
 string header = 'SubBranchCode,TRCustomerId,TRDepartmentID,OriginalDepartmentCode,NewDepartmentCode,TRCustomerCode \n';

    string finalstr = header ;
    String Emailbody;
        
    
    string csvname= 'template_to_update_WO_inv_dept.txt';
    csvAttc.setFileName(csvname);
    
    system.debug('header:' + header);
     String messageBody,priorvalue;   
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    //email.setSubject('Department Code change in Salesforce');     
   for (Department__c dep1 : [Select id, Name,Service_Entity__r.Customer_Name__c,TR_Customer_Code__c,trDepartmentID__c,Service_Entity__r.Sub_Branch__c,Service_Entity__r.Sub_Branch__r.Sub_Branch_Code__c,Service_Entity__r.TR_Customer_Id__c from Department__c where id in : lstdeptsToUpdate and Service_Entity__r.Send_To_TR__c=True])
        {
     priorvalue=trigger.oldMap.get(dep1.Id).Name; 
     //string recordString = dep1.TR_Customer_Code__c + ','+ dep1.Service_Entity__r.Customer_Name__c+','+dep1.trDepartmentID__c +','+priorvalue+','+dep1.Name+','+dep1.Service_Entity__r.Sub_Branch__r.Sub_Branch_Code__c +','+dep1.Service_Entity__r.TR_Customer_Id__c +'\n';
     string recordString = dep1.Service_Entity__r.Sub_Branch__r.Sub_Branch_Code__c+','+dep1.Service_Entity__r.TR_Customer_Id__c+','+dep1.trDepartmentID__c +','+priorvalue+','+dep1.Name +','+ dep1.TR_Customer_Code__c  +'\n';

          finalstr = finalstr +recordString;
      
         messageBody = '<html><body>';
         messageBody = messageBody + 'Here are the details for the change in Department Code in Salesforce ';
         messageBody = messageBody + '<br>';
         messageBody = messageBody + '<br>';            
         messageBody = messageBody + 'TR Customer Code: '+ dep1.TR_Customer_Code__c;
         messageBody = messageBody + '<br>';
         messageBody = messageBody + 'TR Department ID: ' + dep1.trDepartmentID__c;
         messageBody = messageBody + '<br>';
               
         messageBody = messageBody + 'Original Department Code: ' + priorvalue;
         messageBody = messageBody + '<br>';
         messageBody = messageBody + 'New Department Code: ' + dep1.Name;
         messageBody = messageBody + '<br>';
         messageBody = messageBody + '<br>';            
         messageBody = messageBody + 'Additional details are available in the attached excel. Please update the Work Order and Invoicing details with the new department code.';
         messageBody = messageBody + '<br>';
         messageBody = messageBody + '<br>';            
         messageBody = messageBody + 'Assign ticket to - Data Management Backend group';
    
         messageBody = messageBody + '</body></html>';       
    
    blob csvBlob = Blob.valueOf(finalstr);    
    csvAttc.setBody(csvBlob);

    system.debug('----');    
    email.setSubject('Department Code change in Salesforce updated to ' + dep1.Name ); 
    email.setHtmlBody(messageBody);

    email.setToAddresses(new String[]{'raos@gmail.com'});
    email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
    system.debug('email...' + email );
    if(email != null)
      {
       Messaging.sendEmail(new Messaging.singleEmailMessage[] {email}); 
       //   system.debug('Inside send email');
      } 
    }        
}
        }
}
--------------------------------------------------------------
@IsTest
public class SendEmailAfterDeptCodeUpdateTest {
static testMethod void SendEmailAfterDeptCodeUpdateTest()
    {
        Profile p = [ Select Id, Name from Profile where name='System Administrator' limit 1 ];
        //Profile p = [ Select Id, Name from Profile where name='Standard Sales User' limit 1 ];
        User standardSales = new User(profileId = p.id, username ='standarduserAdmin@peak.com', email = 'standarduser123@peak.com', 
                                     emailencodingkey = 'UTF-8', localesidkey ='en_US', languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles',
                                    alias='nuser123', lastname='lastname123');
        insert standardSales;
        system.runAs(standardSales)
        {   /*Account a=new Account(Name='Test1',BillingCountry='United States',BillingState='Indiana');
            insert a;
         
            Acquisition__c ac= new Acquisition__c(Name='Test Ac');
            insert ac;
         
            Service_Entity__c Se1=new Service_Entity__c(Customer_Name__c='Testa',Legal_Entity__c='Archive Systems Inc.',Account__c=a.id,AcquisitionDB__c=ac.id);
            insert Se1;*/
            
            Department__c dept1= new Department__c(Name='Test Dept',Department_Name__c='Sample Dept',  CurrencyIsoCode='USD',Service_Entity__c='a0t1W00000Un1kR'); //Se1.Id);
            test.startTest();
            insert dept1;
            test.stopTest();
        }


    }
    static testMethod void updateDepartmentTest1(){
                
        Profile p2 = [ Select Id, Name from Profile where name='Onboarding Team' limit 1 ];
        User standardSales2 = new User(profileId = p2.id, username ='standarduserAdmin123@peak.com', email = 'standarduseranc@peak.com', 
                                     emailencodingkey = 'UTF-8', localesidkey ='en_US', languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles',
                                    alias='nuser123', lastname='lastname1231');
        insert standardSales2;

       system.runAs(standardSales2)
        {   Department__c dept11= new Department__c(Name='Test Dept123',Department_Name__c='Sample Dept123',  CurrencyIsoCode='USD',Service_Entity__c='a0t1W00000Un1qH'); //Se1.Id);
            insert dept11  ;   
            test.startTest();
            dept11.Name='Test Dept1243';
            Update dept11;
            test.stopTest();
        }   
    }
    static testMethod void updateDepartmentTest(){
        Profile p = [ Select Id, Name from Profile where name='System Administrator' limit 1 ];
        User standardSales = new User(profileId = p.id, username ='standarduserAdmin@peak.com', email = 'standarduser123@peak.com', 
                                     emailencodingkey = 'UTF-8', localesidkey ='en_US', languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles',
                                    alias='nuser123', lastname='lastname123');
        insert standardSales;
        system.runAs(standardSales){
            Department__c dept2= new Department__c(Name='Test Dept2',Department_Name__c='Sample Dept2',  CurrencyIsoCode='USD',Service_Entity__c='a0t1W00000Un4UM'); //Se1.Id);
            insert dept2;
            
            test.startTest();
            dept2.Name='Test Dept3';
            Update dept2;
            test.stopTest();
        }
    }
}
--------------------------------------------------------------

I am getting the following error message

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Stack trace is showing this message:
Class.SendEmailAfterDeptCodeUpdateTest.updateDepartmentTest1: line 39, column 1

I have highlighted the line 39.  The new method that i have include is updateDepartmentTest1().

Let me know the mistake I am doing.

Thanks
Rao
  • May 21, 2019
  • Like
  • 0