You need to sign in to do that
Don't have an account?
Salesforce Developer 60
Test Class for Account and ContactRole Extension
This is my controller. Can anyone suggests me how to add test class for this controller. And one more question in this line AccountContactRole cont=new AccountContactRole(Role='None',ContactId='0032800000CIMN0', IsPrimary=false, AccountId=getAccount().id); am hardcode the contactId. So how to remove this from here.
public class AccountAndcontactrolesEditExtensionV2{ private ApexPages.StandardController std; // the associated AccountContactRoles public List<AccountContactRole> AccountContactRoles; // the chosen AccountContactRole id - used when deleting a AccountContactRole public Id chosenAccountContactRoleId {get; set;} public AccountAndcontactrolesEditExtensionV2(ApexPages.StandardController stdCtrl) { std=stdCtrl; } public Account getAccount() { return (Account) std.getRecord(); } private boolean updateAccountContactRoles() { boolean result=true; if (null!=AccountContactRoles) { List<AccountContactRole> updConts=new List<AccountContactRole>(); try { update AccountContactRoles; } catch (Exception e) { String msg=e.getMessage(); integer pos; // if its field validation, this will be added to the messages by default if (-1==(pos=msg.indexOf('FIELD_CUSTOM_VALIDATION_EXCEPTION, '))) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg)); } result=false; } } return result; } public PageReference saveAndExit() { boolean result=true; result=updateAccountContactRoles(); if (result) { // call standard controller save return std.save(); } else { return null; } } public PageReference save() { Boolean result=true; PageReference pr=Page.contactRole2; if (null!=getAccount().id) { result=updateAccountContactRoles(); } else { pr.setRedirect(true); } if (result) { // call standard controller save, but don't capture the return value which will redirect to view page std.save(); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved')); } pr.getParameters().put('id', getAccount().id); return pr; } public void newAccountContactRole() { if (updateAccountContactRoles()) { AccountContactRole cont=new AccountContactRole(Role='None',ContactId='0032800000CIMN0', IsPrimary=false, AccountId=getAccount().id); insert cont; // null the AccountContactRoles list so that it is rebuilt AccountContactRoles=null; } } public void deleteAccountContactRole() { if (updateAccountContactRoles()) { if (null!=chosenAccountContactRoleId) { AccountContactRole cont=new AccountContactRole(Id=chosenAccountContactRoleId); delete cont; // null the AccountContactRoles list so that it is rebuilt AccountContactRoles=null; chosenAccountContactRoleId=null; } } } public List<AccountContactRole> getAccountContactRoles() { if ( (null!=getAccount().id) && (AccountContactRoles == null) ) { AccountContactRoles=[SELECT Id,Role,AccountId,IsPrimary,ContactId FROM AccountContactRole WHERE AccountId = : getAccount().ID ORDER BY CreatedDate]; } return AccountContactRoles; } }
As for testing this, there is a lot to cover here. I would recommend that you read over this article [1] to plan your tests and read over this article [2] to familiarize yourself with testing controller extensions. If after reading those you have any specific questions, please feel free to ask them.
[1] http://blog.deadlypenguin.com/blog/testing/strategies/
[2] https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm