You need to sign in to do that
Don't have an account?
uday uday chavan
test class for vf page controller class
how write test class for this
vf page contoller:
public with sharing class ChangePrimaryContactCtrl {
private Id recordId;
public Opportunity optyRecord {get;set;}
public List<OppConRoleWrapper> primaryContactWrapperList {get;set;}
public Contact newConRecord {get;set;}
public Boolean showNewContactScreen {get;set;}
public String index {get;set;}
public String titleInputString {get;set;}
public ChangePrimaryContactCtrl() {
recordId = apexpages.currentpage().getparameters().get('id');
primaryContactWrapperList = new List<OppConRoleWrapper> ();
newConRecord = new Contact();
showNewContactScreen = false;
optyRecord = new Opportunity();
index = '';
titleInputString = '';
if(String.isNotBlank(recordId) ) {
queryAllContactRoles();
}
}
public Pagereference doCancel() {
Pagereference pgr = new Pagereference('/'+recordId);
return pgr;
}
public void save() {
Savepoint sp = Database.setSavepoint();
if (String.isBlank(index)) {
return;
}
Integer indexValue = Integer.valueOf(index);
try {
Contact con = new Contact();
if (!primaryContactWrapperList.isEmpty() && primaryContactWrapperList.size() > indexValue) {
con = primaryContactWrapperList[indexValue].conRecrd;
}
// for (OppConRoleWrapper wrapper : primaryContactWrapperList) {
// if(wrapper.isSelected) {
// con = wrapper.conRecrd;
// break;
// }
// }
if(String.isBlank(con.id)) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select atleast one contact.'));
return ;
}
if (optyRecord.Primary_Contact__c == con.id) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Please change the contact before saving. Click Back to exit.'));
return ;
}
//Update the Opportunity
update new Opportunity(Id=optyRecord.Id,Primary_contact__c = con.id);
//update the Custom Account Role
if(String.isNotBlank(optyRecord.Account_Role__c)) {
update new Account_Role__c (Id=optyRecord.Account_Role__c,Primary_Contact__c = con.id);
}
queryAllContactRoles();
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Primary Contact Updated'));
} catch (Exception ex) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Message: '+ex.getMessage() + ' at Line Number: '+ex.getLineNumber()));
Database.rollback(sp);
return;
}
}
public void CreateNewContact() {
showNewContactScreen = true;
}
public void doCancelMakePrimary() {
showNewContactScreen = false;
}
public void makePrimary() {
Savepoint sp = Database.setSavepoint();
try {
if(String.isBlank(newConRecord.LastName)) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Last Name cannot be blank.'));
return;
}
if(String.isBlank(newConRecord.Email)) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Email cannot be blank.'));
return;
}
newConRecord.AccountId = optyRecord.AccountId;
newConRecord.Is_From_Change_Primary_Contact__c = true;
insert newConRecord;
//Update the Opportunity
update new Opportunity(Id=optyRecord.Id,Primary_contact__c = newConRecord.id);
//update the Custom Account Role
if(String.isNotBlank(optyRecord.Account_Role__c)) {
update new Account_Role__c (Id=optyRecord.Account_Role__c,Primary_Contact__c = newConRecord.id);
}
if (String.isNotBlank(titleInputString)) {
List<AccountContactRelation> acrList = [SELECT id FROM AccountContactRelation WHERE AccountId = :optyRecord.AccountId AND ContactId = : newConRecord.id Order by CreatedDate DESC LIMIT 1];
if (!acrList.isEmpty()) {
acrList[0].Title__c = titleInputString;
update acrList;
}
}
queryAllContactRoles();
showNewContactScreen = false;
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'New Primary Contact Added & Updated.'));
newConRecord = new Contact();
titleInputString = '';
}catch(Exception ex) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Message: '+ex.getMessage() + ' at Line Number: '+ex.getLineNumber()));
Database.rollback(sp);
}
}
public void queryAllContactRoles() {
primaryContactWrapperList = new List<OppConRoleWrapper>();
List<OppConRoleWrapper> tempWrapperList = new List<OppConRoleWrapper>();
optyRecord = [SELECT id,Name,AccountId,Account.Name,CloseDate,Primary_Contact__c,Account_Role__c
FROM Opportunity WHERE ID = :recordId];
List<Contact> listAllContactForAccount = [SELECT id,Phone,Email,Description,Name FROM Contact WHERE AccountId = :optyRecord.AccountId];
if (!listAllContactForAccount.isEmpty()) {
Map<Id,String> mapConIdToTitle = new Map<Id,String>();
List<AccountContactRelation> listACR = [SELECT id,Title__c,ContactId,Contact.Phone,Contact.Email,Contact.Description,Contact.Name FROM AccountContactRelation WHERE ContactId IN :listAllContactForAccount ];
for (AccountContactRelation acr : listACR) {
OppConRoleWrapper wrap = new OppConRoleWrapper();
wrap.isSelected = false;
if(optyRecord.Primary_Contact__c == acr.ContactId) {
wrap.isSelected = true;
}
Contact con = new Contact();
con.Id = acr.ContactId;
wrap.conName = acr.Contact.Name;
con.Phone = acr.Contact.Phone;
con.Email = acr.Contact.Email;
con.Description = acr.Contact.Description;
wrap.conRecrd = con;
wrap.title = acr.Title__c;
tempWrapperList.add(wrap);
}
//sorting logic
for(OppConRoleWrapper wrap :tempWrapperList) {
if(wrap.isSelected) {
primaryContactWrapperList.add(wrap);
break;
}
}
for (OppConRoleWrapper wrap :tempWrapperList) {
if(!wrap.isSelected) {
primaryContactWrapperList.add(wrap);
}
}
}
}
public Class OppConRoleWrapper {
public Boolean isSelected {get;set;}
public Contact conRecrd {get;set;}
public String title {get;set;}
public String conName {get;set;}
}
}
test class for this
vf page contoller:
public with sharing class ChangePrimaryContactCtrl {
private Id recordId;
public Opportunity optyRecord {get;set;}
public List<OppConRoleWrapper> primaryContactWrapperList {get;set;}
public Contact newConRecord {get;set;}
public Boolean showNewContactScreen {get;set;}
public String index {get;set;}
public String titleInputString {get;set;}
public ChangePrimaryContactCtrl() {
recordId = apexpages.currentpage().getparameters().get('id');
primaryContactWrapperList = new List<OppConRoleWrapper> ();
newConRecord = new Contact();
showNewContactScreen = false;
optyRecord = new Opportunity();
index = '';
titleInputString = '';
if(String.isNotBlank(recordId) ) {
queryAllContactRoles();
}
}
public Pagereference doCancel() {
Pagereference pgr = new Pagereference('/'+recordId);
return pgr;
}
public void save() {
Savepoint sp = Database.setSavepoint();
if (String.isBlank(index)) {
return;
}
Integer indexValue = Integer.valueOf(index);
try {
Contact con = new Contact();
if (!primaryContactWrapperList.isEmpty() && primaryContactWrapperList.size() > indexValue) {
con = primaryContactWrapperList[indexValue].conRecrd;
}
// for (OppConRoleWrapper wrapper : primaryContactWrapperList) {
// if(wrapper.isSelected) {
// con = wrapper.conRecrd;
// break;
// }
// }
if(String.isBlank(con.id)) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select atleast one contact.'));
return ;
}
if (optyRecord.Primary_Contact__c == con.id) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Please change the contact before saving. Click Back to exit.'));
return ;
}
//Update the Opportunity
update new Opportunity(Id=optyRecord.Id,Primary_contact__c = con.id);
//update the Custom Account Role
if(String.isNotBlank(optyRecord.Account_Role__c)) {
update new Account_Role__c (Id=optyRecord.Account_Role__c,Primary_Contact__c = con.id);
}
queryAllContactRoles();
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Primary Contact Updated'));
} catch (Exception ex) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Message: '+ex.getMessage() + ' at Line Number: '+ex.getLineNumber()));
Database.rollback(sp);
return;
}
}
public void CreateNewContact() {
showNewContactScreen = true;
}
public void doCancelMakePrimary() {
showNewContactScreen = false;
}
public void makePrimary() {
Savepoint sp = Database.setSavepoint();
try {
if(String.isBlank(newConRecord.LastName)) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Last Name cannot be blank.'));
return;
}
if(String.isBlank(newConRecord.Email)) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Email cannot be blank.'));
return;
}
newConRecord.AccountId = optyRecord.AccountId;
newConRecord.Is_From_Change_Primary_Contact__c = true;
insert newConRecord;
//Update the Opportunity
update new Opportunity(Id=optyRecord.Id,Primary_contact__c = newConRecord.id);
//update the Custom Account Role
if(String.isNotBlank(optyRecord.Account_Role__c)) {
update new Account_Role__c (Id=optyRecord.Account_Role__c,Primary_Contact__c = newConRecord.id);
}
if (String.isNotBlank(titleInputString)) {
List<AccountContactRelation> acrList = [SELECT id FROM AccountContactRelation WHERE AccountId = :optyRecord.AccountId AND ContactId = : newConRecord.id Order by CreatedDate DESC LIMIT 1];
if (!acrList.isEmpty()) {
acrList[0].Title__c = titleInputString;
update acrList;
}
}
queryAllContactRoles();
showNewContactScreen = false;
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'New Primary Contact Added & Updated.'));
newConRecord = new Contact();
titleInputString = '';
}catch(Exception ex) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Message: '+ex.getMessage() + ' at Line Number: '+ex.getLineNumber()));
Database.rollback(sp);
}
}
public void queryAllContactRoles() {
primaryContactWrapperList = new List<OppConRoleWrapper>();
List<OppConRoleWrapper> tempWrapperList = new List<OppConRoleWrapper>();
optyRecord = [SELECT id,Name,AccountId,Account.Name,CloseDate,Primary_Contact__c,Account_Role__c
FROM Opportunity WHERE ID = :recordId];
List<Contact> listAllContactForAccount = [SELECT id,Phone,Email,Description,Name FROM Contact WHERE AccountId = :optyRecord.AccountId];
if (!listAllContactForAccount.isEmpty()) {
Map<Id,String> mapConIdToTitle = new Map<Id,String>();
List<AccountContactRelation> listACR = [SELECT id,Title__c,ContactId,Contact.Phone,Contact.Email,Contact.Description,Contact.Name FROM AccountContactRelation WHERE ContactId IN :listAllContactForAccount ];
for (AccountContactRelation acr : listACR) {
OppConRoleWrapper wrap = new OppConRoleWrapper();
wrap.isSelected = false;
if(optyRecord.Primary_Contact__c == acr.ContactId) {
wrap.isSelected = true;
}
Contact con = new Contact();
con.Id = acr.ContactId;
wrap.conName = acr.Contact.Name;
con.Phone = acr.Contact.Phone;
con.Email = acr.Contact.Email;
con.Description = acr.Contact.Description;
wrap.conRecrd = con;
wrap.title = acr.Title__c;
tempWrapperList.add(wrap);
}
//sorting logic
for(OppConRoleWrapper wrap :tempWrapperList) {
if(wrap.isSelected) {
primaryContactWrapperList.add(wrap);
break;
}
}
for (OppConRoleWrapper wrap :tempWrapperList) {
if(!wrap.isSelected) {
primaryContactWrapperList.add(wrap);
}
}
}
}
public Class OppConRoleWrapper {
public Boolean isSelected {get;set;}
public Contact conRecrd {get;set;}
public String title {get;set;}
public String conName {get;set;}
}
}
test class for this
Check this link which covers most area for writing a test class if you are having any issues:
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Thanks!
I would suggest if you face any specific issue while writing test class you can post that here so that community can help you better.
please share your code where you are stuck, after that, I will try to help you.
you can learn from this about test class :
https://developer.salesforce.com/forums/?id=9060G000000Bj8MQAS
Thank you!
Regards,
Suraj Tripathi