function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KeerthanaKeerthana 

post a question

Can anyone help me with the test class for below controller. I am trying to get the values of the vf page on save of a new record. standard controller will be null at that point but i need to access the values so I can save it.

public CA_SamsAccountController(ApexPages.StandardController std) {
      samsid = ApexPages.currentPage().getParameters().get('id');
       accid = ApexPages.currentPage().getParameters().get('AccountId');
       if(samsid!=null)
        cont =  (CA_SamsAccount__c)std.getRecord();  --- this line gives me error whenever I try to run the test class if i remove the above if condition
        }
        
        public PageReference save() { 
            string acctype = bc.getSamsAccountTypes();
            cont.CA_Sams_Account_Name__c = str;
            cont.CA_Account__c = acc.id;
            cont.CA_Account_name__c = accname;
            cont.CA_ApiLastName__c = '.api';
            cont.CA_Country__c = selectedCountry;
            cont.CA_contract__c = contractid;
            cont.CA_Parent_ID__c = selectedParent;
            cont.CA_MisPassword__c = temp1;
            cont.CA_ApiPassword__c = temp;
            cont.CA_AccountTypeId__c = acctype;
            con = [select lastname,firstname,phone,email from contact where id=: cont.CA_Contact__c limit 1];   --- I always have cont as null and hence not able to fetch contact
            cont.CA_First_Name__c = con.firstname;
            cont.CA_Last_Name__c = con.lastname;
            cont.CA_Telephone__c = con.phone;
            cont.CA_Email__c = con.email;
            insert cont;
        }
        
         public string getApiorgname()
    {
       string apiorgname = '';
       string api = cont.CA_Sams_Account_Name__c;  --- same issues goes here as well
       list<CA_SamsAccount__c> clist = [select CA_ApiFirstName__c from CA_SamsAccount__c where CA_ApiFirstName__c=: api];
       if(clist.size()>0)
           for(CA_SamsAccount__c c : clist)
                uniquename.add(c.CA_ApiFirstName__c);
       apiorgname = checkpattern(api);
       apiorgname = apiorgname + '.api';
       system.debug('api==='+ apiorgname);
       return apiorgname;
    }

Any quick work around is highly appreciated.
Thanks, Priya
Board salesforceBoard salesforce
Hi Priya ,

What is the error you are getting ?
Have you inserted the contact ?

Thanks ,
Board
KeerthanaKeerthana
Hi,

Yes I have inserted contact. Here is my test class:

@istest
public class CA_SamsAccountController_test{
    static testmethod void mytest(){
        Account a = new account(name = 'test');
        insert a;
        contact c = new contact (lastname='abc', firstname = 'ABC', phone ='987656');
        insert c;
         System.currentPageReference().getParameters().put('AccountId', a.id);
        
        CA_SamsAccountController cs = new CA_SamsAccountController(null);
        cs.save();
        cs.ContactPopulated();
       list<SelectOption> testoptions = cs.getParent();
        list<SelectOption> testoptions1 = cs.getCountry();
        cs.selectedCountry = 'none';
        cs.selectedParent = 'none';
    }
     static testmethod void mytest1(){
        Account a = new account(name = 'test');
        insert a;
         string samsacc = a.name.replace(' ','_').tolowercase();
        contact c = new contact (lastname='abc', firstname = 'ABC', phone ='987656');
        insert c;
        CA_SamsAccount__c sacc= new CA_SamsAccount__c(CA_Account__c = a.id, CA_Contact__c = c.id, CA_Country__c = 'test', CA_Parent_ID__c = 'test', CA_Sams_Account_Name__c = samsacc );
        insert sacc;
        System.currentPageReference().getParameters().put('id', sacc.id);
         System.currentPageReference().getParameters().put('AccountId', a.id);
        
        apexPages.standardController newCntr=new apexPages.standardController(sacc);
        CA_SamsAccountController cs = new CA_SamsAccountController(newCntr);
        cs.save();
        cs.ContactPopulated();
       list<SelectOption> testoptions = cs.getParent();
        list<SelectOption> testoptions1 = cs.getCountry();
        cs.selectedCountry = 'none';
        cs.selectedParent = 'none';
         cs.str = samsacc;
         cs.getApiorgname();
         cs.getAPIpassword();
         cs.getCountry();
         cs.getMisorgname();
         cs.getMISpassword();
         cs.getParent();
    }
}
KeerthanaKeerthana
Error message:
System.NullPointerException: Attempt to de-reference a null object
Board salesforceBoard salesforce
Mention the line Number ,in which method the null pointer exception occuring?
this error refers that you are passing null value.

Thanks ,
Board
KeerthanaKeerthana
Hi,

Its throwing error in below line.. I need to set contact value for standard controller. Since on new record creation the controller value would be null I need some guidance how do i send the contact look up id


  if(samsid!=null)
        cont =  (CA_SamsAccount__c)std.getRecord();  --- this line gives me error whenever I try to run the test class if i remove the above if condition