0

I have 94% coverage. When trying to run apex tests after deploying I get this error.

System.StringException: Invalid id: null Stack Trace: External entry point Class.cls_Training_Students.addTraining: line 210, column 1 Class.test_Training_Students.test_Training_Students: line 84, column 1

This is the full controller code

public with sharing class cls_Training_Students {

    //****The code was copied and modified from the Questions__c object****
    //****May 31 2016
    //**** It allows the user to select from a list of contacts (shalichim) 
    //**** and add them to the Training__c object

    // Const
    private static final string RECORD_TYPE_SHAL = 'shal';    
    private static final string OBJECT_NAME_FOR_CONTACT_ROLES = 'Additional_Contact_Role__c';

    public Boolean refreshPage { get; set; }
    public String redirectUrl {public get; private set;}
    private ApexPages.StandardController stdController;

    // Inner class
    public class selectedPerson{
        public boolean isSelected {get;set;}
        //public Contact contact {get;set;}
        public Additional_Contact_Role__c contactR {get;set;}
    }

    // Params
    public String currentContacts_CSV {get;set;}
    public set<Id> currentContactIds_Set {get;set;}
    public list<selectedPerson> selectedContacts_List {get;set;}
    public list<selectedPerson> completeContacts4VF_List {get;set;}
    private Training_Resource__c currentQ = new Training_Resource__c();
    private list<Contact> contactsFromQ_List = new list<Contact>();
    private list<Contact> completeContacts_List = new list<Contact>();
    /// moving to be based on Contact Role
    private list<Additional_Contact_Role__c> completeContacts_List2 = new list<Additional_Contact_Role__c>();
    private list<Additional_Contact_Role__c> contactsFromQ_List2 = new list<Additional_Contact_Role__c  >();


    // vars
    list<String> splitContactIds = new list<String>();
    Id shalRT = null;

    public cls_Training_Students(ApexPages.StandardController controller){

        this.stdController = controller;

        currentQ.Id = controller.getId();
        currentQ = [Select Id, Selected_Contacts_Ids_List__c From Training_Resource__c Where Id = :currentQ.Id];

        shalRT = [select id from RecordType where sobjecttype =:OBJECT_NAME_FOR_CONTACT_ROLES  and Name =:RECORD_TYPE_SHAL limit 1].Id;

        redirectUrl = '/' + currentQ.Id;
        init();
    }
//private This method generates the UI to be able to select contacts    
  public void init(){
        currentContacts_CSV = (currentQ.Selected_Contacts_Ids_List__c != '' ? currentQ.Selected_Contacts_Ids_List__c : '');
        currentContactIds_Set = new set<Id>();
        selectedContacts_List = new list<selectedPerson>();
        selectedPerson sp = new selectedPerson();
        completeContacts4VF_List = new list<selectedPerson>();

        // CSV to list
        if (currentContacts_CSV != null && currentContacts_CSV != ''){
            splitContactIds = currentContacts_CSV.split('[,]');
            for (String cId:splitContactIds){
                currentContactIds_Set.add(cId);
            }
        }

        // ronen query test
        Set<Id> relevantContactsIds_Set = new Set<Id>();
        // Select Contact__c, Contact_Name__c, a.Supervisor_Name__c, a.RecordTypeId, a.Months_in_Shlichut__c, a.Melaveh_Name__c, a.Region__c, a.Id, a.Coordinator_Name__c, a.Active__c From Additional_Contact_Role__c a
        Map<Id,Additional_Contact_Role__c> crMap = new Map<Id,Additional_Contact_Role__c>([select contact__c from Additional_Contact_Role__c where recordTypeId = :shalRT ]);

        for (Id acrId : crMap.keySet())
        {
            relevantContactsIds_Set.add(crMap.get(acrId).Contact__c);
        }

         system.debug('relevantContactsIds_Set +++++++++'  + relevantContactsIds_Set.size());
         system.debug('crMap +++++++++'  + crMap.size());

        contactsFromQ_List = [Select Id, Name, Region__c From Contact Where Id IN :currentContactIds_Set];
        contactsFromQ_List2 = [Select Contact__c, Contact_Name__c, a.Supervisor_Name__c, 
                                                                            a.RecordTypeId, a.Months_in_Shlichut__c, a.Melaveh_Name__c, a.Region__c, a.Id, 
                                                                            a.Coordinator_Name__c, a.Active__c 
                                                                            From Additional_Contact_Role__c a 
                                                                            Where a.RecordTypeId = :shalRTId 
                                                                            AND Contact__c IN :currentContactIds_Set];
        Integer maxList = 999-contactsFromQ_List.size();
        if(currentContactIds_Set.size() < 1){
            completeContacts_List = [Select Id, Name, Region__c From Contact Where Id IN :relevantContactsIds_Set limit :maxList];
            completeContacts_List2 = [Select Contact__c, Contact_Name__c, a.Supervisor_Name__c, 
                                                                            a.RecordTypeId, a.Months_in_Shlichut__c, a.Mel_Name__c, a.Region__c, a.Id, 
                                                                            a.Coordinator_Name__c, a.Active__c 
                                                                            From Additional_Contact_Role__c a 
                                                                            Where a.RecordTypeId = :shalRTId 
                                                                            //AND Contact__c IN :currentContactIds_Set
                                                                            limit :maxList];
        }
        else {
            completeContacts_List = [Select Id, Name, Region__c From Contact Where Id IN :relevantContactsIds_Set and Id NOT IN :currentContactIds_Set limit :maxList];
            completeContacts_List2 = [Select Contact__c, Contact_Name__c, a.Supervisor_Name__c, 
                                                                            a.RecordTypeId, a.Months_in_Shlichut__c, a.Melaveh_Name__c, a.Region__c, a.Id, 
                                                                            a.Coordinator_Name__c, a.Active__c 
                                                                            From Additional_Contact_Role__c a 
                                                                            Where a.RecordTypeId = :shalRT 
                                                                            AND Contact__c NOT IN :currentContactIds_Set 
                                                                            limit :maxList];
            }


        // Prep the contacts lists
        for (Additional_Contact_Role__c cr : contactsFromQ_List2){
            sp = new selectedPerson();
            sp.isSelected = true;
            sp.contactR = cr;
            completeContacts4VF_List.add(sp);
        }

        for (Additional_Contact_Role__c cr : completeContacts_List2){
            sp = new selectedPerson();
            sp.isSelected = false;
            sp.contactR = cr;
            completeContacts4VF_List.add(sp);
        }
    }

    public PageReference clearAllContacts()
    {
        currentQ.Selected_Contacts_Ids_List__c = '';

            update currentQ;
            refreshPage = true;
            //redirectUrl = '/' + currentQ.Id;//controller.getId().getUrl();
             init();

        return null; //return new PageReference('/apex/Search_Interviewees?Id=' + currentQ.Id);

    }

    public PageReference saveAllContacts()
    {
        list<String> returnedSelectedPersonsID_StringList = new list<String>();
        for (selectedPerson sp : completeContacts4VF_List){
                returnedSelectedPersonsID_StringList.add(sp.ContactR.Contact__c);
        }

        String tempCSV = '';

        if(returnedSelectedPersonsID_StringList.size() > 0){

            for(String Id : returnedSelectedPersonsID_StringList){
                // First selected person
                if (tempCSV == ''){
                    tempCSV = Id;
                    continue;
                }
                else {
                    tempCSV += ',' + Id;
                }
            }
        }

        currentQ.Selected_Contacts_Ids_List__c = tempCSV;

            update currentQ;
            refreshPage = true;

            init();

            return null; //new PageReference('/apex/Search_Interviewees?Id=' + currentQ.Id);

    }

    public PageReference updateContactSelection()
    {

        string cId = ApexPages.currentPage().getParameters().get('cId');
        string state = ApexPages.currentPage().getParameters().get('isChecked');
        if (state == 'true')
            // if this is the first selection
            if (currentQ.Selected_Contacts_Ids_List__c == null || currentQ.Selected_Contacts_Ids_List__c == '')
                currentQ.Selected_Contacts_Ids_List__c = ApexPages.currentPage().getParameters().get('cId');
            else
                currentQ.Selected_Contacts_Ids_List__c += ',' + ApexPages.currentPage().getParameters().get('cId');
        else
        {
            if (currentQ.Selected_Contacts_Ids_List__c.startsWith(cId))
                currentQ.Selected_Contacts_Ids_List__c = currentQ.Selected_Contacts_Ids_List__c.replace( cId+',','');
            else
                currentQ.Selected_Contacts_Ids_List__c = currentQ.Selected_Contacts_Ids_List__c.replace(','+ cId,'');
        }


            update currentQ;
            return null;
    }

public PageReference addTraining()

    {
// This method save the selected contacts to the training__c object
// It determines the list by looking at the field Selected_Contacts_Ids_List__c
        init();

        if ( currentQ.Selected_Contacts_Ids_List__c == Null ||  currentQ.Selected_Contacts_Ids_List__c == '')
        {
            List <training__c> rd = [Select ID FROM training__c WHERE Training_Resource__c = :currentQ.Id];
            delete rd;
            return null;
        }

            Training__c[] ts = new Training__c[]{};  

            if(currentQ.Selected_Contacts_Ids_List__c.containsNone(',')  )
            {
                ts.add(new Training__c(shal__c = currentContacts_CSV,Training_Resource__c=currentQ.Id));                            
            }else
            {    
                for (String cId:splitContactIds)
                    {
                        ts.add(new Training__c(shal__c = cId,Training_Resource__c=currentQ.Id));                
                    }
            }

            List <training__c> rd = [Select ID FROM training__c WHERE Training_Resource__c = :currentQ.Id];
            delete rd;

            insert ts;  
            init();

        return null;

    }       


}

Line 210 in the VF controller just runs init() which is the following method

  public void init(){
        currentContacts_CSV = (currentQ.Selected_Contacts_Ids_List__c != '' ? currentQ.Selected_Contacts_Ids_List__c : '');
        currentContactIds_Set = new set<Id>();
        selectedContacts_List = new list<selectedPerson>();
        selectedPerson sp = new selectedPerson();
        completeContacts4VF_List = new list<selectedPerson>();

Line 84 in the test class says

     Test.stopTest();

What is causing the error?

  • 2
    could you maybe post a bit more of your code? Which Id are you using? Also be sure to only use data you have created in your testclass => make sure your test code runs org-independant! – Nielsm Jun 6 '16 at 11:26
  • what type of currentContacts_CSV is? – Patlatus Jun 6 '16 at 11:54
  • Is it possible that your CSV file isn't available to the org you're deploying to?? – crmprogdev Jun 6 '16 at 12:32
  • I posted the full controller above. – Apex N-u-b Jun 6 '16 at 12:37
  • 1
    I added back in the parts of your test class you removed. I think you need to include your test class code. Rarely does the source of a problem occur on the line it's reported at. – crmprogdev Jun 6 '16 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.