You need to sign in to do that
Don't have an account?
Wajeed Khan
Can anyone give test code coverage for below code please ,thanks in advance
public with sharing class Call_Controller { public Call__c call{get;set;} public Id accountId {get;set;} public List<Account_Affiliation__c> accAf{get;Set;} public List<wrapperClass> wrapClassList{get;set;} public wrapperClass wc{get;set;} public Account acc{get;set;} Set<Id> ParentID = new Set<id>(); Set<id> CallId= new Set<Id>(); public Call_Controller(ApexPages.StandardController ctrl) { if(!test.isRunningTest()) ctrl.addFields(new List<String>{'Account__c'}); wrapClassList = new List<wrapperClass>(); accountId =ApexPages.currentPage().getParameters().get('accId'); accAf = new List<Account_Affiliation__c>(); call = (Call__c)ctrl.getRecord(); if(accountId==null) { accountId=call.Account__c; } else { call.Account__c = accountId; } acc=[select ID,Name from Account where id=:accountId]; accAf=[Select id,Status__c,To__r.name,From__c,To__c From Account_Affiliation__c where From__c=:accountId and Status__c=true]; Set<String> callName=new Set<String>(); if(ctrl.getId()!=null) { try{ List<Call__c> childCall = [select id,Account__r.Name,Call_Date__c,out_Of_Office__c,Comments__c,parent_call__c from call__c where Parent_call__c=:call.Id]; for(Call__c cc : childCall) { wc = new wrapperClass(cc.Account__r.Name,cc.Id); callName.add(cc.Account__r.Name); wc.flag=true; ParentID.add(cc.Account__c); callId.add(cc.Id); wrapClassList.add(wc); } } catch(Exception ex) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()+' at line number '+ex.getLineNumber())); } } for(Account_Affiliation__c af:accAf) { if(!callName.contains(af.To__r.Name)) { wc = new wrapperClass(af.To__r.Name,af.Id); wc.accWrap=af; wrapClassList.add(wc); } } } public class wrapperClass { public boolean flag{get;set;} public Account_Affiliation__c accWrap{get;set;} public String accName{get;set;} public Id Cid{get;set;} public wrapperClass(String Name,id Cid) { this.Cid=Cid; accName=Name; } } //Record Creation for Related Records on Save public void createRelRecordOnSave() { for(wrapperClass wc:wrapClassList) { if(wc.flag==true) { if(!callId.contains(wc.Cid)) { Call__c relCall= new Call__c(); relCall.Account__c=wc.accWrap.To__c; relCall.Out_of_office__c=call.Out_of_office__c; relcall.Parent_Call__c=call.id; relCall.Call_Date__c=call.Call_Date__c; relCall.Comments__c=call.Comments__c; relCall.Status__c='Draft'; insert relCall; } else { Call__c c1=[select id,Account__c,Out_of_office__c,Call_Date__c,Comments__c from Call__c where Account__c=:wc.accWrap.To__c]; c1.Call_Date__c=call.Call_Date__c; c1.Status__c='Draft'; c1.Comments__c=call.Comments__c; c1.Out_of_office__c=call.Out_of_office__c; update c1; } } } } public void clear() { call.Comments__c=null; } //Record Creation for Related Records on Submit public void createRelRecordOnSubmit() { for(wrapperClass wc:wrapClassList) { if(wc.flag==true ) { if(!callId.contains(wc.Cid)) { Call__c relCall= new Call__c(); relCall.Account__c=wc.accWrap.To__c; relCall.Out_of_office__c=call.Out_of_office__c; relcall.Parent_Call__c=call.id; relCall.Call_Date__c=call.Call_Date__c; relCall.Comments__c=call.Comments__c; relCall.Status__c='Submitted'; insert relCall; } else { Call__c c1=[select id,Account__c,Out_of_office__c,Call_Date__c,Comments__c from Call__c where Id = :wc.Cid]; c1.Call_Date__c=call.Call_Date__c; c1.Status__c='Submitted'; c1.Comments__c=call.Comments__c; c1.Out_of_office__c=call.Out_of_office__c; update c1; } } } } //Save Button public PageReference Save() { if(call.Call_Date__c>System.today()) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Enter A future Date')); return null; } else { try{ call.Status__c='Draft'; upsert call; createRelRecordOnSave(); PageReference reRend = new PageReference('/'+AccountID); reRend.setRedirect(true); return reRend; } catch(Exception ex) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()+' at line number '+ex.getLineNumber())); } } return null; } public PageReference cancel() { PageReference reRend = new PageReference('/'+AccountID); reRend.setRedirect(true); return reRend; } //Submit Button public PageReference Submit() { if(call.Call_Date__c>system.today()) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Enter A future Date')); } try{ call.Status__c='Submitted'; upsert call; createRelRecordOnSubmit(); PageReference reRend = new PageReference('/'+AccountID); reRend.setRedirect(true); return reRend; } catch(Exception ex) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()+' at line number '+ex.getLineNumber())); } return null; } }
The below two blog should help you to write the test class :
http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.com/search/label/Test%20Class
Let us know if you still face any further issues.
Thanks,
Shravan