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
Waqas AliWaqas Ali 

Test class for contact roles

I am copying my contacts roles of old opportunity to new opportunit and also copying contact roles of opportunity to contact roles of newly created contract. Now how to wite test class for this ?
String theId = ApexPages.currentPage().getParameters().get('id');
List<OpportunityContactRole> oppRoles;
oppRoles = [SELECT Id, OpportunityID, ContactID, IsPrimary, Role FROM OpportunityContactRole WHERE OpportunityId = :theId LIMIT 10000];
            
//copying contact roles of old opportunity to new opportunity

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }
// myContract[0] is also defined and working fine,  code will become long so  here not mentioned, 
/// also copying the contact roles of opportunity to newly created contract

if ( ! oppRoles.isEmpty()){
                    List<ContractContactRole> conRoles = new List<ContractContactRole>();
                      
                      for (OpportunityContactRole oppRole : oppRoles)
                        { 
                             conRoles.add(new ContractContactRole(
                             ContactId = oppRole.ContactId,
                             IsPrimary = oppRole.IsPrimary,
                             Role = oppRole.Role,
                             ContractId = myContract[0].Id));                       
                         }
                         insert conRoles;
              }

Guys any help ?
Best Answer chosen by Waqas Ali
William TranWilliam Tran
Are you looking for test coverage or unit testing to make sure it works.

For test coverage, create a test class to fire off this class/method.

For unit testing, run the scenario to see if you get the results you wanted to see.

thx.

All Answers

William TranWilliam Tran
Are you looking for test coverage or unit testing to make sure it works.

For test coverage, create a test class to fire off this class/method.

For unit testing, run the scenario to see if you get the results you wanted to see.

thx.
This was selected as the best answer
Waqas AliWaqas Ali
Obviously i have test class to fire this class/methods. I have code coverage of 63%. My question was differnent. re-read it please. 
William TranWilliam Tran
Waqas,  since it is obvious you have written your test classes already please clarify, you want your coverage to be 100%? Is that your question? 

I re-read the original question and it is does not specify that you wrote your test class but unable to get it to 100% or that you are not sure how to write you test classes or you are asking whether your code even work at all and need to write test classes to verify your results.

If you need 100% coverage, can you post you test class so that we can analyze and let you know what's missing in it?

Thx.
Waqas AliWaqas Ali

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BQx3

Here is my test class, please read the question over there. I have done with Cotact role now get 100% code coveage how to write the unit test for wrapper class. Thank You.