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
sam_Adminsam_Admin 

Test Class Fails

Hi folks,

     Iam just a step away to move my code to prod, my test class fails, here is my trigger and class, iam not sure how to fix it up..

Trigger:

 

trigger Opportunities on Opportunity (after insert) {
 
    Opportunities o = new Opportunities();
    o.SetContactRoleDefaults(Trigger.new);
 
}

Class:

 

public class Opportunities {
 
    // Default Constructor
    public Opportunities()
    {
    }
 
    // Sets default values on the Contact Role record created during Conversion. Called on AFTER INSERT
    public void SetContactRoleDefaults(Opportunity[] opptys)
    {
        /***************
        * Variables
        ***************/
        set<ID> set_opptyIDs = new set<ID>();
 
        /***************
        * Initial Loop
        ***************/
        // Get a set of the Opportunity IDs being affected.
        for(Opportunity o:opptys) {
            set_opptyIDs.add(o.id);
        }
 
        /***************
        * Process Records
        ***************/
        // Update the Contact Role record to be Primary and the Decision Maker
        list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
        for(OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs]) {
            ocr.IsPrimary = true;
            ocr.Role = 'Primary Contact';
            list_opptyContactRolesToUpdate.add(ocr);

        }
 
        if (list_opptyContactRolesToUpdate.size() > 0) {
            update list_opptyContactRolesToUpdate;
        }
 
    }
}

 

Test Class:

@isTest
private class Opportunities {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Set<Id> optyIds = new Set<Id>();
       
    list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
        
      Account acc = new Account(Name = 'test', phone = '1234567890');
      insert acc;
      
      Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id ,Amount = 50,     

                                    CurrencyIsoCode = 'USD', CloseDate = Date.newInstance(2009,02,01), StageName = 'Closed',

                                    ForecastCategoryName = 'omitted', SystemType__c = 'Other', SystemQty__c = 0);
      insert opp;
      
      Account acc1 = new Account(Name = 'test', phone = '1234567890');
      insert acc1;
      
      Contact con = new Contact(LastName = 'Testing', AccountId = 'acc1.Id');
      insert con;
      
      OpportunityContactRole ocr = new OpportunityContactRole(IsPrimary = true, Role = 'Primary Contact', OpportunityId =

                                                              opp.Id, ContactId = 'con.Id');
      insert ocr;
      
        
    }
}

 

when i run the test it fails and i get this msg


System.StringException: Invalid id: acc1.Id

 

The class is 66% covered it is not covered in the highlighted part, so all i want to make sure the test class doesn't fails and it covers the highlighted lines, any help is appreciated guys.

 

TIA1

 


Best Answer chosen by Admin (Salesforce Developers) 
Jeremy-KraybillJeremy-Kraybill

Remove the quotes around 'acc1.Id' -- you're currently passing a string literal, not the actual Id value.

 

Jeremy Kraybill

All Answers

Jeremy-KraybillJeremy-Kraybill

Remove the quotes around 'acc1.Id' -- you're currently passing a string literal, not the actual Id value.

 

Jeremy Kraybill

This was selected as the best answer
sam_Adminsam_Admin

Hey ,

   Thx for that but now my test class is 2% coverage which is worst for me to deploy it, could you drag me out from this .

 

 

public class Opportunities {
 
    // Default Constructor
    public Opportunities()
    {
    }
 
    // Sets default values on the Contact Role record created during Conversion. Called on AFTER INSERT
    public void SetContactRoleDefaults(Opportunity[] opptys)
    {
        /***************
        * Variables
        ***************/
        set<ID> set_opptyIDs = new set<ID>();
 
        /***************
        * Initial Loop
        ***************/
        // Get a set of the Opportunity IDs being affected.
        for(Opportunity o:opptys) {
            set_opptyIDs.add(o.id);

        }
 
        /***************
        * Process Records
        ***************/
        // Update the Contact Role record to be Primary and the Primary Contact
        list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
        for(OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs]) {
            ocr.IsPrimary = true;
            ocr.Role = 'Primary Contact';
            list_opptyContactRolesToUpdate.add(ocr);

        }
 
        if (list_opptyContactRolesToUpdate.size() > 0) {
            update list_opptyContactRolesToUpdate;

        }
 
    }
}
NK123NK123

Show us your updated test code.

sam_Adminsam_Admin

Here is the updated test code

 

@isTest
private class Opportunities {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Set<Id> optyIds = new Set<Id>();
       
    list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
        
      Account acc = new Account(Name = 'test', phone = '1234567890');
      insert acc;
      
      Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id ,Amount = 50,     

                                    CurrencyIsoCode = 'USD', CloseDate = Date.newInstance(2009,02,01), StageName = 'Closed',

                                    ForecastCategoryName = 'omitted', SystemType__c = 'Other', SystemQty__c = 0);
      insert opp;
      
      Account acc1 = new Account(Name = 'test', phone = '1234567890');
      insert acc1;
      
      Contact con = new Contact(LastName = 'Testing', AccountId = acc1.Id);
      insert con;
      
      OpportunityContactRole ocr = new OpportunityContactRole(IsPrimary = true, Role = 'Primary Contact', OpportunityId =

                                                              opp.Id, ContactId = con.Id);
      insert ocr;
      
        
    }
}

NK123NK123

Looks like the test is completely skipped. Can you also post the test result log.

~NK

sam_Adminsam_Admin

Hi,

   Thx for standing by and helping me to resolve this issue, iam Admin and this is pretty much new to me, so my log is too big and i even can't post it here it says limit exceeded i guess i can't post more than 20000 characters, Any idea

sam_Adminsam_Admin

I guess i figured out , here is the log

 

10:17:21.886|CODE_UNIT_STARTED|[EXTERNAL]|01qQ0000000CuZc|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kKqc]
10:17:21.887|METHOD_ENTRY|[1]|01pQ00000009R3l|Opportunities.Opportunities()
10:17:21.887|METHOD_EXIT|[1]|Opportunities
10:17:21.887|CONSTRUCTOR_ENTRY|[3]|01pQ00000009R3l|<init>()
10:17:21.887|CONSTRUCTOR_EXIT|[3]|<init>()
10:17:21.887|METHOD_ENTRY|[4]|01pQ00000009R3l|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
10:17:21.887|METHOD_ENTRY|[21]|SET.add(ANY)
10:17:21.887|METHOD_EXIT|[21]|SET.add(ANY)
10:17:21.887|SOQL_EXECUTE_BEGIN|[29]|Aggregations:0|select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs
10:17:21.893|SOQL_EXECUTE_END|[29]|Rows:0
10:17:21.893|METHOD_ENTRY|[35]|LIST.size()
10:17:21.893|METHOD_EXIT|[35]|LIST.size()
10:17:21.893|METHOD_EXIT|[4]|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
10:17:22.453|CUMULATIVE_LIMIT_USAGE
10:17:22.453|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 2 out of 150
Number of DML rows: 2 out of 10000
Number of script statements: 35 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10

10:17:22.453|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
10:17:22.453|STATIC_VARIABLE_LIST|
Opportunities:o:4

10:17:22.453|CUMULATIVE_LIMIT_USAGE_END

10:17:21.893|CODE_UNIT_FINISHED|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kKqc]
10:17:21.918|ENTERING_MANAGED_PKG|MD

NK123NK123

Based on your log, looks like the system is calling the method:

10:17:21.893|METHOD_EXIT|[4]|Opportunities.SetCont​actRoleDefaults(LIST<Opportunity>)

Can you put some statements like these (System.debug('test1');) in your SetCont​actRoleDefaults method so that we can see in test results how much code getting processed.

 

~NK

 

sam_Adminsam_Admin

Here is the log when i kept the system debug and executed the test class

 

10:36:42.123|CODE_UNIT_STARTED|[EXTERNAL]|01qQ0000000CuZc|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kKx9]
10:36:42.123|METHOD_ENTRY|[1]|01pQ00000009R3l|Opportunities.Opportunities()
10:36:42.123|METHOD_EXIT|[1]|Opportunities
10:36:42.123|CONSTRUCTOR_ENTRY|[3]|01pQ00000009R3l|<init>()
10:36:42.123|CONSTRUCTOR_EXIT|[3]|<init>()
10:36:42.123|METHOD_ENTRY|[4]|01pQ00000009R3l|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
10:36:42.123|METHOD_ENTRY|[21]|SET.add(ANY)
10:36:42.123|METHOD_EXIT|[21]|SET.add(ANY)
10:36:42.123|METHOD_ENTRY|[22]|System.debug(ANY)
10:36:42.123|USER_DEBUG|[22]|DEBUG|test1
10:36:42.123|METHOD_EXIT|[22]|System.debug(ANY)
10:36:42.123|SOQL_EXECUTE_BEGIN|[30]|Aggregations:0|select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs
10:36:42.163|SOQL_EXECUTE_END|[30]|Rows:0
10:36:42.163|METHOD_ENTRY|[37]|LIST.size()
10:36:42.163|METHOD_EXIT|[37]|LIST.size()
10:36:42.163|METHOD_EXIT|[4]|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
10:36:41.852|CUMULATIVE_LIMIT_USAGE
10:36:41.852|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 2 out of 150
Number of DML rows: 2 out of 10000
Number of script statements: 36 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10

10:36:41.852|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
10:36:41.852|STATIC_VARIABLE_LIST|
Opportunities:o:4

10:36:41.852|CUMULATIVE_LIMIT_USAGE_END

10:36:42.163|CODE_UNIT_FINISHED|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kKx9]

NK123NK123

Hi,

 

Looks like you added the debug statement only one time. For your own test purpose, I suggest adding that statement throughout that method to know the flow of the system. You can take these statements out or comment it after you are done with your testing if you like, but for now, you should place many statements within that method to check the flow of the program execution.

 

~NK

 

sam_Adminsam_Admin

Hi NK,

    Thx again, actually i got this code from here,

 

http://sfdc.arrowpointe.com/2009/02/06/set-defaults-for-opportunity-contact-roles-when-converting/comment-page-1/#comment-65306

 

Iam just not using his test class because previously i have a test class so i want to include this same code in my previous test class , but even if i use his test class and execute it iam just getting only 2% and iam not even quite sure about setting up debug logs, the code works perfect but problem with test class.

 

TIA!

NK123NK123

Can you add that debug statement (System.debug('Opp Id: ' + o.id);) in the following for loop code:


   for(Opportunity o:opptys)

{            set_opptyIDs.add(o.id);       

       // System.debug('Opp Id: ' + o.id);

}

 

Looks like the debug log shows the o record returned from that query: It could be that the Id's are not present or passed to this method.

 

10:36:42.123|SOQL_EXECUTE_BEGIN|[30]|Aggregations:​0|select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs
10:36:42.163|SOQL_EXECUTE_END|[30]|Rows:0

 

~NK

sam_Adminsam_Admin

Hi i added and here is the debug log

 

11:21:41.191|CODE_UNIT_STARTED|[EXTERNAL]|01qQ0000000CuZc|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kL0Q]
11:21:41.191|METHOD_ENTRY|[1]|01pQ00000009R3l|Opportunities.Opportunities()
11:21:41.191|METHOD_EXIT|[1]|Opportunities
11:21:41.191|CONSTRUCTOR_ENTRY|[3]|01pQ00000009R3l|<init>()
11:21:41.191|CONSTRUCTOR_EXIT|[3]|<init>()
11:21:41.191|METHOD_ENTRY|[4]|01pQ00000009R3l|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
11:21:41.191|METHOD_ENTRY|[21]|SET.add(ANY)
11:21:41.192|METHOD_EXIT|[21]|SET.add(ANY)
11:21:41.192|METHOD_ENTRY|[22]|System.debug(ANY)
11:21:41.192|USER_DEBUG|[22]|DEBUG|Opp Id: 006Q0000007kL0QIAU
11:21:41.192|METHOD_EXIT|[22]|System.debug(ANY)
11:21:41.192|SOQL_EXECUTE_BEGIN|[31]|Aggregations:0|select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs
11:21:41.236|SOQL_EXECUTE_END|[31]|Rows:0
11:21:41.236|METHOD_ENTRY|[38]|LIST.size()
11:21:41.236|METHOD_EXIT|[38]|LIST.size()
11:21:41.236|METHOD_EXIT|[4]|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
11:21:41.446|CUMULATIVE_LIMIT_USAGE
11:21:41.446|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 2 out of 150
Number of DML rows: 2 out of 10000
Number of script statements: 36 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10

11:21:41.446|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
11:21:41.446|STATIC_VARIABLE_LIST|
Opportunities:o:4

11:21:41.446|CUMULATIVE_LIMIT_USAGE_END

11:21:41.236|CODE_UNIT_FINISHED|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kL0Q]

NK123NK123

Since the role record for the opportunity doesn't exists during the after insert opportunity trigger, So, its obvoius now, that your code for role didn't get executed:

 

 list<OpportunityContactRole> list_opptyCon​tactRolesToUpdate = new list<OpportunityContactRol​e>();
        for(OpportunityContactRole ocr:[select Id,​IsPrimary,Role from OpportunityContactRole where O​pportunityId in :set_opptyIDs]) {
            ocr.IsPrimary = true;
            ocr.Role = 'Primary Contact';
            list_opptyContactRolesToUpdate.add(ocr​);

        }

 

So, I think calling of this method (SetContactRoleDefaults) from trigger is not helping you with executing the above code:

 

trigger Opportunities on Opportunity (after insert​) {
 
    Opportunities o = new Opportunities();
    o.SetContactRoleDefaults(Trigger.new);
 
}

 

-- I would suggest you to rethink how you are using this overall setup.

Your goal is to setup the OpportunityContactRole for new opportunity to "Primary Contact" and you can do this directly from the trigger itself. Basically you can create the OpportunityContactRole  record in the above trigger and set the role there. Instead of the current setup which is not correct.

 

~NK

 

sam_Adminsam_Admin

Hi,

   Actually the role does exist and not sure why iam getting this problem, iam not quite sure to write only trigger without class, but is it possible to include test class in the exisitng apex class itself?

NK123NK123

I am writing a test code to call this method in question. I was not able to test in my system, so you need to review and revise if needed.

 

List<Opportunity > set_oppty = new List<Opportunity >();
set_oppty.add(opp);

Opportunities o = new Opportunities();   
o.SetContactRoleDefaults(set_oppty);

 

Add the above code towards the end of your test code after this lines:

 

OpportunityContactRole ocr = new OpportunityContactRole(IsPrimary = true, Role = 'Primary Contact', OpportunityId = opp.Id, ContactId = 'con.Id');
insert ocr;

 

-----

 

 

All this code is doing is putting the test new opportunity into the list and passing it to the method SetContactRoleDefaults

 

~NK

 

sam_Adminsam_Admin

Getting this error

 

Method does not exist or incorrect signature: [Opportunities].SetContactRoleDefaults(LIST<Opportunity>)

NK123NK123

We have been back and forth many times. I need to visualize the code. Can you show the code where you put the new test code. I want to see bigger picture here...

Provide the full test code and I will comment about it after review.

 

sam_Adminsam_Admin

Here is the test code which i have , i just executed it and it doesn't gave me the error which i told u in my previous msg but still it's giving me 2%

 

@isTest
private class OpportunitiesTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Set<Id> optyIds = new Set<Id>();
        set<ID> set_opptyIDs = new set<ID>();
       
    list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
        
      Account acc = new Account(Name = 'test', phone = '1234567890');
      insert acc;
      
      Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id ,Amount = 50,     
                                        CurrencyIsoCode = 'USD', CloseDate = Date.newInstance(2009,02,01), StageName = 'Closed',
                                        ForecastCategoryName = 'omitted', SystemType__c = 'Other', SystemQty__c = 0);
      insert opp;
      
      Account acc1 = new Account(Name = 'test', phone = '1234567890');
      insert acc1;
      
      Contact con = new Contact(LastName = 'Testing', AccountId = acc1.Id);
      insert con;
      
      OpportunityContactRole ocr = new OpportunityContactRole(IsPrimary = true, Role = 'Primary Contact', OpportunityId =opp.Id, ContactId = con.Id);
      insert ocr;
      
      List<Opportunity > set_oppty = new List<Opportunity >();
      set_oppty.add(opp);

    Opportunities o = new Opportunities();   
    o.SetContactRoleDefaults(set_oppty);
   
        
    }
}

NK123NK123

Now, test code looks good. Can you run this and send the debug log as you did in the past.

~NK

 

sam_Adminsam_Admin

Here is the log

 

12:55:31.698|CODE_UNIT_STARTED|[EXTERNAL]|01p40000000GtNB|OpportunitiesTest.myUnitTest
12:55:31.698|METHOD_ENTRY|[23]|01p40000000GtNB|OpportunitiesTest.OpportunitiesTest()
12:55:31.698|METHOD_EXIT|[23]|OpportunitiesTest
12:55:31.699|DML_BEGIN|[33]|Op:Insert|Type:Account|Rows:1
12:55:31.821|DML_END|[33]
12:55:31.821|METHOD_ENTRY|[36]|Date.newInstance(Integer, Integer, Integer)
12:55:31.821|METHOD_EXIT|[36]|Date.newInstance(Integer, Integer, Integer)
12:55:31.821|DML_BEGIN|[38]|Op:Insert|Type:Opportunity|Rows:1

 

12:55:32.145|CODE_UNIT_STARTED|[EXTERNAL]|01qQ0000000CuZc|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kL5p]
12:55:32.145|METHOD_ENTRY|[1]|01pQ00000009R3l|Opportunities.Opportunities()
12:55:32.145|METHOD_EXIT|[1]|Opportunities
12:55:32.145|CONSTRUCTOR_ENTRY|[3]|01pQ00000009R3l|<init>()
12:55:32.145|CONSTRUCTOR_EXIT|[3]|<init>()
12:55:32.145|METHOD_ENTRY|[4]|01pQ00000009R3l|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
12:55:32.145|METHOD_ENTRY|[21]|SET.add(ANY)
12:55:32.145|METHOD_EXIT|[21]|SET.add(ANY)
12:55:32.145|SOQL_EXECUTE_BEGIN|[31]|Aggregations:0|select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs
12:55:32.163|SOQL_EXECUTE_END|[31]|Rows:0
12:55:32.164|METHOD_ENTRY|[38]|LIST.size()
12:55:32.164|METHOD_EXIT|[38]|LIST.size()
12:55:32.164|METHOD_EXIT|[4]|Opportunities.SetContactRoleDefaults(LIST<Opportunity>)
12:55:31.800|CUMULATIVE_LIMIT_USAGE
12:55:31.800|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 2 out of 150
Number of DML rows: 2 out of 10000
Number of script statements: 34 out of 200000
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10

12:55:31.800|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
12:55:31.800|STATIC_VARIABLE_LIST|
Opportunities:o:4

12:55:31.800|CUMULATIVE_LIMIT_USAGE_END

12:55:32.164|CODE_UNIT_FINISHED|Opportunities on Opportunity trigger event AfterInsert for [006Q0000007kL5p]

sam_Adminsam_Admin

Hi NK,

    Now iam using the test class what exactly the other guy has posted in his site for this trigger but even iam getting 2%

 

this is the test class

 

 

@isTest
private class Opportunities_Test {
 
    static testMethod void SetContactRoleDefaults_Test() {
 
        // Create a Lead
        Lead l = new Lead();
        l.lastname = 'Lastname';
        l.firstname = 'FirstName';
        l.company = 'Company';
        insert l;
 
        // Convert the Lead
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(l.id);
 
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
 
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
 
        // Query Contact Role Records and Asserts all was set correctly.
        for (OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId = :lcr.getOpportunityId()]) {
            system.AssertEquals('Primary Contact', ocr.Role);
            system.AssertEquals(true, ocr.IsPrimary);
        }
 
    }
 

 

NK123NK123

Ok, I just recreated all the code in my sandbox and tested with that. I am getting 100% on both the trigger and the class file.

Here is the screenshots.

 

Code Coverage Help for this Page
Opportunities (Code Covered: 100%)
 line source
 1  trigger Opportunities on Opportunity (after insert)
 2  {
 3   Opportunities o = new Opportunities();
 4   o.SetContactRoleDefaults(Trigger.new);
 5   }

 

 

Code Coverage Help for this Page
Opportunities (Code Covered: 100%)
 line source
 1  public class Opportunities {
 2  
 3  // Default Constructor
 4  public Opportunities()
 5  {
 6  }
 7  
 8   // Sets default values on the Contact Role rec​ord created during Conversion. Called on AFTER INS​ERT
 9   public void SetContactRoleDefaults(Opportunity[] opptys)
 10   {
 11   /***************
 12   * Variables
 13   ***************/
 14   set<ID> set_opptyIDs = new set<ID>();
 15  
 16   /***************
 17   * Initial Loop
 18   ***************/
 19   // Get a set of the Opportunity IDs being ​affected.
 20   for(Opportunity o:opptys) {
 21   set_opptyIDs.add(o.id);
 22   }
 23  
 24   /***************
 25   * Process Records
 26   ***************/
 27   // Update the Contact Role record to be Pr​imary and the Decision Maker
 28   list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
 29  
 30   for(OpportunityContactRole ocr : [Select o.SystemModstamp, o.Role, o.OpportunityId, o.LastModifiedDate, o.LastModifiedById, o.IsPrimary, o.IsDeleted, o.Id, o.CreatedDate, o.CreatedById, o.ContactId From OpportunityContactRole o
 31   where o.OpportunityId in :set_opptyIDs])
 32   {
 33   ocr.IsPrimary = true;
 34   ocr.Role = 'Primary Contact';
 35   list_opptyContactRolesToUpdate.add(ocr);
 36   }
 37  
 38   if (list_opptyContactRolesToUpdate.size()>0){update list_opptyContactRolesToUpdate;}
 39  
 40   }
 41  }

 

 

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class OpportunitiesTest {

    static testMethod void myUnitTest() {
        
         // TO DO: implement unit test
        Set<Id> optyIds = new Set<Id>();
        set<ID> set_opptyIDs = new set<ID>();
       
    list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
        
      Account acc = new Account(Name = 'test', phone = '1234567890');
      insert acc;
      
      Opportunity opp = new Opportunity(Name = 'test', AccountId = acc.Id ,Amount = 50, CloseDate = Date.newInstance(2009,02,01), StageName = 'Closed',
                                        ForecastCategoryName = 'omitted' );
      insert opp;
      
      Account acc1 = new Account(Name = 'test', phone = '1234567890');
      insert acc1;
      
      Contact con = new Contact(LastName = 'Testing', AccountId = acc1.Id);
      insert con;
      
      OpportunityContactRole ocr = new OpportunityContactRole(IsPrimary = true, Role = 'Primary Contact', OpportunityId =opp.Id, ContactId = con.Id);
      insert ocr;
      
      List<Opportunity > set_oppty = new List<Opportunity >();
      set_oppty.add(opp);

    Opportunities o = new Opportunities();   
    o.SetContactRoleDefaults(set_oppty);
        
    }
}
sam_Adminsam_Admin

NK,

   when i executed the same code in one of my sandbox i got 100% coverage but when i executed in the sandbox which iam working iam getting 2% so i was looking at the log and searched for the word Exception and i got this, do you know how to overcome this..Once again i appreciate for standing by and helping me out with this.

 

 

13:49:36.557|METHOD_ENTRY|[46]|WebServiceCallout.invoke(APEX_OBJECT, APEX_OBJECT, MAP, LIST)
13:49:36.557|EXCEPTION_THROWN|[46]|System.TypeException: Methods defined as TestMethod do not support Web service callouts, test skipped
13:49:36.557|METHOD_EXIT|[46]|WebServiceCallout.invoke(APEX_OBJECT, APEX_OBJECT, MAP, LIST)
13:49:36.557|METHOD_EXIT|[11]|partnerSoapSforceCom.Soap.login(String, String)
13:49:36.560|FATAL_ERROR|System.TypeException: Methods defined as TestMethod do not support Web service callouts, test skipped

NK123NK123

Hi,

 

When I first commented on this post, if you notice, I mentioned that the test are getting skipped. And you have never provided this log till now.

In any case, tests are run ran because it clearly says that the "Methods defined as TestMethod do not support Web service callouts, test skipped"

 

It means that you are calling some web service in your test code or some dependent code.

 

~Nk

 

sam_Adminsam_Admin

yea later i recognized it and i was looking for word exception then i came to know this....:) so is there any way to avoid this.

 

Thanks

 

sam_Adminsam_Admin

NK,

   I deleted the class which is making an exception, so iam good to go .

 

Again thank you so much for helping me out

 

 

NK123NK123

I am glad that I could help. Please close this post.