• Siddhesh Chavan
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 6
    Replies
Test class not covering work order before insert trigger.

Trigger:
trigger assignTechnician on Work_Order__c (before insert) {
    
    List<User> userList = new List<User>();
    
    for(Work_Order__c wo : Trigger.New)
    {
     if(wo.Auto_assign_technician__c == TRUE)
     {
      
           userList = [select id from User where City = :wo.City__c AND UserRole.Name = :wo.Order_Type__c];
        
        for(User u : userList)
        {
            wo.OwnerId = u.Id;
        }
    }
    
     }         
}

Test Class:
@isTest
private class assignTechnicianTest {
        static testMethod void validateassignTechnician() {

        Test.startTest();
       
       Work_Order__c wo = new Work_Order__c();
        
        wo.City__c = 'Mumbai';
        wo.Order_Type__c = 'Field Service';
       
        insert wo;
        
        wo.Auto_assign_technician__c = true;
	update wo;
        
        System.assertEquals(wo.Auto_assign_technician__c, true);
        
        List<User> userList = new List<User>();
        UserRole r = new UserRole(name = 'Field Service');
        insert r;
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];

          User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', ProfileId = p.Id,
            LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserName='testservicepro@testorg.com');
            
            u.City = 'Mumbai';
            u.UserRoleId = r.id;
        insert u;
        
       
        Test.stopTest(); 
        
        }
       
}

 
<apex:pagemessages> not working in visualforce page.

Error with details directly redirected to new page
I want to show errormesage in standard error messagebox.
Below is the code details.

If the logedin user doesn't have FLS access then error message should get display on same page.
Visualforce Page:
 
<apex:pageMessages id="pmsg" />

<apex:pageBlock title="Cases">
   <apex:pageBlockButtons >
      <apex:commandButton value="Select "  onclick="return Test();" action="{!caseMethod}"  reRender="pmsg" />
   </apex:pageBlockButtons>

Controller Code:
if (!CaseComment.sObjectType.getDescribe().isDeletable()){
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
          return null;
        }
 delete ChildComment;

 
Hi,
I was submitted my managed package application for security review but it got rejected by giving 
CRUD/FLS Enforcement error. Can any one tell me with example how can i resolve this issue, so i can move further for next procedure.

Thanks,
Namespace Prefix : lntcms

We have "Find Duplicate Cases button" in our app. When we clicked that new visualforce page get opened in new window. Its working fine in managed package org .
But when we installed our package in test org and when we clicked on Find Duplicate Cases button its giving error in new window that page does not exist. New window doesn't append our namespace prefix of package in the begining.

Can you please suggest a solution for same. 
Hi all,

My test class is not covering the code inside for loop.
 
Apex class method:Class Name SampleClass

public PageReference caseMethod() {
        
        List<case> updateCase = new List<case>();
        NewComment = new List<CaseComment>();
       	childattachment = new List<Attachment>();
 
        for(Case cs: selectedCasesComm){
            if(cs.id != idRadio){
                // Querty to retrive child attachment
                childattachment = [Select Id, ParentId, Body, Name from Attachment Where ParentId =: cs.id];
                cs.ParentId = idRadio;
                cs.Status = 'Closed as Duplicate';
                updateCase.add(cs);
                
                //Attachment Merging
                for(Attachment childatt : childattachment)
                {
                    Attachment masterattachment = New Attachment(Name = childatt.Name, Body = childatt.Body);
                    masterattachment.ParentId = idRadio;
                    insert masterattachment;
                }               
                delete childattachment; //Deleting old case attachments               
            }
        } 
        
        update updateCase;   
        PageReference pageReference = new PageReference('/'+500+'/o');
        pageReference.setRedirect(true);
        return pageReference;
    }


Test Class

public static testMethod void testcaseMethod(){
    test.startTest();   
    SampleClass s= new SampleClass ();  
   
    Case c = getTestCase();
    Case cDup = getTestCaseDup();
    
    Attachment cattachment = getTestCaseAttachment();
    Attachment cDupattachment = getTestCaseAttachmentDup();
    
   
   
    List<Case> mergeCasesmethod = new List<Case>();
    List<Attachment> mergeCaseattachment = new List<Attachment>();
    
    mergeCasesmethod.add(c);
    mergeCasesmethod.add(cDup);
    
    mergeCaseattachment.add(cattachment);
    mergeCaseattachment.add(cDupattachment);
   
    
   s.selectedCasesComm = mergeCasesmethod; // Working 
   s.childattachment  = mergeCaseattachment;  //Not covered  
   s.caseMethod(); 
      
   test.stopTest();  
  }

 
Stack Trace MassTransfferOwnershipController.doSearch: line 306, column 1
                   MassCaseTransferTestClass.MassCaseTransferTestClass: line 52, column 1

My Controllers method

 public PageReference doSearch()
    { 
       
        theResultList = new List<wrapperClass>();
        
        //system.debug('IN THE LOOP'+toUserFunc);
        
        if(selectedCaseFiler <> 'Select from list...' && selectedCaseFiler1 == 'Select from list...'&&  selectedCaseFiler2 == 'Select from list...')
        {
            CaseFiler = String.escapeSingleQuotes(selectedCaseFiler);
            CaseFilter = String.escapeSingleQuotes(selectedFilterValue);
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c  FROM Case WHERE OwnerId = :myId AND '+CaseFiler+' = :CaseFilter';
            
        }
        else if(selectedCaseFiler <> 'Select from list...' && selectedCaseFiler1 <> 'Select from list...' &&  selectedCaseFiler2 == 'Select from list...')
        {
              CaseFilerFirst = String.escapeSingleQuotes(selectedCaseFiler);
              CaseFilterFirst = String.escapeSingleQuotes(selectedFilterValue);
              CaseFilerFirst1 = String.escapeSingleQuotes(selectedCaseFiler1);
              CaseFilterFirst1 = String.escapeSingleQuotes(selectedFilterValue1);
             theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c FROM Case WHERE OwnerId = :myId AND '+CaseFilerFirst+' = :CaseFilterFirst AND '+CaseFilerFirst1+' = :CaseFilterFirst1 ';
            system.debug('IF LOOP 2 '+theQ);
        }
        else if(selectedCaseFiler <> 'Select from list...' && selectedCaseFiler1 <> 'Select from list...' &&  selectedCaseFiler2 <> 'Select from list...')
        {
            CaseFilerSecond = String.escapeSingleQuotes(selectedCaseFiler );
            CaseFilterSecond = String.escapeSingleQuotes(selectedFilterValue);
            CaseFilerSecond1 = String.escapeSingleQuotes(selectedCaseFiler1 );
            CaseFilterSecond1 = String.escapeSingleQuotes(selectedFilterValue1);
            CaseFilerSecond2 = String.escapeSingleQuotes(selectedCaseFiler2 );
            CaseFilterSecond2 = String.escapeSingleQuotes(selectedFilterValue2);
              
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c FROM Case WHERE OwnerId = :myId AND '+ CaseFilerSecond +' = :CaseFilterSecond AND '+ CaseFilerSecond1+' = :CaseFilterSecond1 AND '+CaseFilerSecond2+' = :CaseFilterSecond2 ';           
           system.debug('IF LOOP 3 '+theQ);
        }
        else
        {
        
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c FROM Case WHERE OwnerId = '+myId;
        }
        /*
        else
        {
            theQ = 'SELECT id,OwnerId FROM '+selectedObject+' WHERE OwnerId = :myId AND Stage = \''+selectedOpportunityFiler+'\'';
        }
        */
        
        system.debug('THE QUERY IS:-'+theQ);
        for(Case s : Database.Query(theQ))
        {
            system.debug('THE S:-'+s);
            wrapperClass wc = new wrapperClass(s);
            system.debug('THE WC:-'+wc);
            theResultList.add(wc);
        }
        system.debug('THE LIST IS:-'+theResultList);
        if(theResultList.isEmpty())
        {
             ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'NO RECORDS FOUND.'));
             showResult = FALSE;
        }
        showResult = TRUE;
        
        
        return null;
    }

My Test Class

@isTest(SeeAllData=true)
private class MassCaseTransferTestClass
{
    static testMethod void MassCaseTransferTestClass()
    {
        Test.setCurrentPage(Page.MassTransfferOwnershipPage);

       List <User> user= [select id from User where Profile.Name = 'System Administrator'];
        
        String userId;   
        for(User usr: user)
        {
            userId = usr.id;
         }  
        
        
        Case c = new Case(OwnerId=userId,Origin='Phone',Subject='Test Case 1');
        insert c;
        
       
        
        Transffer_Ownership__c testObj = new Transffer_Ownership__c(User__c=userId);
        insert testObj; 
        
       
        ApexPages.Standardcontroller sc = New ApexPages.StandardController(testObj);
        MassTransfferOwnershipController mct = new MassTransfferOwnershipController(sc);    
        
        ApexPages.Standardcontroller sc1 = New ApexPages.StandardController(c);
        MassTransfferOwnershipController mct1 = new MassTransfferOwnershipController(sc1);    
        
        
        
        mct1.selectedCaseFiler = 'Origin';
        mct1.selectedCaseFiler1 = 'Origin';
        mct1.selectedCaseFiler2 = 'Origin';
       
        
        mct.getObjectsList(); //Covered
        mct.getblockChoices(); //Covered
        mct.getCaseFilersList(); //Covered
        mct1.getFiltersList(); //Covered
        
        mct.userPopulate();
        mct.touserPopulate();
        
        
        mct1.getOpportunityFilersList();
       
        mct1.getFiltersList1();
        mct1.getFiltersList2();
        mct1.doSearch();
        mct1.doTransfer(); 
        mct1.doTransfer1(); 
        mct1.finalMethod(); 
        
    }
    
    static testMethod void MassCaseTransferTestClass_2()
    {
    
      List<User> user = [select id from User where Profile.Name = 'System Administrator'];
      
      String userId ;
      for(User usr: user)
        {
            userId  = usr.id;
        }
        
      Case c2 = new Case(OwnerId=userId ,Priority='High',Subject='Test Case 2');
      insert c2;
      
      Transffer_Ownership__c testObj = new Transffer_Ownership__c(User__c=userId);
      insert testObj;
        
        
       
        ApexPages.Standardcontroller sc = New ApexPages.StandardController(testObj);
        MassTransfferOwnershipController mct = new MassTransfferOwnershipController(sc);    
      
      ApexPages.Standardcontroller sc2 = New ApexPages.StandardController(c2);
      MassTransfferOwnershipController mct2 = new MassTransfferOwnershipController(sc2);   

      
 
      mct2.selectedCaseFiler = 'Priority';
      mct2.selectedCaseFiler1 = 'Priority';
      mct2.selectedCaseFiler2 = 'Priority'; 
      
      
       mct.getObjectsList(); //Covered
        mct.getblockChoices(); //Covered
        mct.getCaseFilersList(); //Covered
        mct2.getFiltersList(); //Covered
        
        mct.userPopulate();
        mct.touserPopulate();
        
        
        mct2.getOpportunityFilersList();
       
        
        mct2.getFiltersList1();
        mct2.getFiltersList2();
        mct2.doSearch();
        mct2.doTransfer();
   
       
    }
    
    static testMethod void MassCaseTransferTestClass_3()
    {
    
      List<User> user = [select id from User where Profile.Name = 'System Administrator'];
      
      String userId ;
      
       for(User usr: user)
        {
            userId = usr.id;
        }
      
          
      Case c3 = new Case(OwnerId=userId ,Status='Open',Subject='Test Case 2');
      insert c3;
      
      Transffer_Ownership__c testObj = new Transffer_Ownership__c(User__c=userId);
      insert testObj;
        
        
       
        ApexPages.Standardcontroller sc = New ApexPages.StandardController(testObj);
        MassTransfferOwnershipController mct = new MassTransfferOwnershipController(sc);    
      
      ApexPages.Standardcontroller sc3 = New ApexPages.StandardController(c3);
      MassTransfferOwnershipController mct3 = new MassTransfferOwnershipController(sc3);   

      
 
      mct3.selectedCaseFiler = 'Status';
      mct3.selectedCaseFiler1 = 'Status';
      mct3.selectedCaseFiler2 = 'Status'; 
      
      
       mct.getObjectsList(); //Covered
        mct.getblockChoices(); //Covered
        mct.getCaseFilersList(); //Covered
        mct3.getFiltersList(); //Covered
        
        mct.userPopulate();
        mct.touserPopulate();
        
        
        mct3.getOpportunityFilersList();
       
        
        mct3.getFiltersList1();
        mct3.getFiltersList2();
        mct3.doSearch();
        mct3.doTransfer();
   
    }

}
Hi I am getting  unexpected token: ':' error when i am using follwing query

 String CaseFiler =  ' + selectedCaseFiler + =:' + selectedFilterValue;         
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c  FROM Case WHERE OwnerId = :myId AND :CaseFiler  ';


In above query selectedCaseFiler and selectedFilterValue both are dynamic values
Test class not covering work order before insert trigger.

Trigger:
trigger assignTechnician on Work_Order__c (before insert) {
    
    List<User> userList = new List<User>();
    
    for(Work_Order__c wo : Trigger.New)
    {
     if(wo.Auto_assign_technician__c == TRUE)
     {
      
           userList = [select id from User where City = :wo.City__c AND UserRole.Name = :wo.Order_Type__c];
        
        for(User u : userList)
        {
            wo.OwnerId = u.Id;
        }
    }
    
     }         
}

Test Class:
@isTest
private class assignTechnicianTest {
        static testMethod void validateassignTechnician() {

        Test.startTest();
       
       Work_Order__c wo = new Work_Order__c();
        
        wo.City__c = 'Mumbai';
        wo.Order_Type__c = 'Field Service';
       
        insert wo;
        
        wo.Auto_assign_technician__c = true;
	update wo;
        
        System.assertEquals(wo.Auto_assign_technician__c, true);
        
        List<User> userList = new List<User>();
        UserRole r = new UserRole(name = 'Field Service');
        insert r;
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];

          User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', ProfileId = p.Id,
            LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserName='testservicepro@testorg.com');
            
            u.City = 'Mumbai';
            u.UserRoleId = r.id;
        insert u;
        
       
        Test.stopTest(); 
        
        }
       
}

 
<apex:pagemessages> not working in visualforce page.

Error with details directly redirected to new page
I want to show errormesage in standard error messagebox.
Below is the code details.

If the logedin user doesn't have FLS access then error message should get display on same page.
Visualforce Page:
 
<apex:pageMessages id="pmsg" />

<apex:pageBlock title="Cases">
   <apex:pageBlockButtons >
      <apex:commandButton value="Select "  onclick="return Test();" action="{!caseMethod}"  reRender="pmsg" />
   </apex:pageBlockButtons>

Controller Code:
if (!CaseComment.sObjectType.getDescribe().isDeletable()){
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
          return null;
        }
 delete ChildComment;

 
Hi all,

My test class is not covering the code inside for loop.
 
Apex class method:Class Name SampleClass

public PageReference caseMethod() {
        
        List<case> updateCase = new List<case>();
        NewComment = new List<CaseComment>();
       	childattachment = new List<Attachment>();
 
        for(Case cs: selectedCasesComm){
            if(cs.id != idRadio){
                // Querty to retrive child attachment
                childattachment = [Select Id, ParentId, Body, Name from Attachment Where ParentId =: cs.id];
                cs.ParentId = idRadio;
                cs.Status = 'Closed as Duplicate';
                updateCase.add(cs);
                
                //Attachment Merging
                for(Attachment childatt : childattachment)
                {
                    Attachment masterattachment = New Attachment(Name = childatt.Name, Body = childatt.Body);
                    masterattachment.ParentId = idRadio;
                    insert masterattachment;
                }               
                delete childattachment; //Deleting old case attachments               
            }
        } 
        
        update updateCase;   
        PageReference pageReference = new PageReference('/'+500+'/o');
        pageReference.setRedirect(true);
        return pageReference;
    }


Test Class

public static testMethod void testcaseMethod(){
    test.startTest();   
    SampleClass s= new SampleClass ();  
   
    Case c = getTestCase();
    Case cDup = getTestCaseDup();
    
    Attachment cattachment = getTestCaseAttachment();
    Attachment cDupattachment = getTestCaseAttachmentDup();
    
   
   
    List<Case> mergeCasesmethod = new List<Case>();
    List<Attachment> mergeCaseattachment = new List<Attachment>();
    
    mergeCasesmethod.add(c);
    mergeCasesmethod.add(cDup);
    
    mergeCaseattachment.add(cattachment);
    mergeCaseattachment.add(cDupattachment);
   
    
   s.selectedCasesComm = mergeCasesmethod; // Working 
   s.childattachment  = mergeCaseattachment;  //Not covered  
   s.caseMethod(); 
      
   test.stopTest();  
  }

 
Hi I am getting  unexpected token: ':' error when i am using follwing query

 String CaseFiler =  ' + selectedCaseFiler + =:' + selectedFilterValue;         
            theQ = 'SELECT id,OwnerId,CaseNumber,Origin,Priority,Status,Account.Name,New_Owner__c  FROM Case WHERE OwnerId = :myId AND :CaseFiler  ';


In above query selectedCaseFiler and selectedFilterValue both are dynamic values