+ Start a Discussion
mbrewmbrew 

Force.com IDE - Before Insert and Before Update Trigger and Test Class

I've got a trigger I wrote and put in our sandbox, it works great and production has all the same fields.  So now I have to build the class to test it. I thought I had it and it errors out instead.

 

The trigger is as follows and does work...

 

 

trigger OwnerLinkUpdate on Opportunity (before insert, before update ) {
 for (Opportunity a :Trigger.NEW) {
    a.Owner_Link__c = a.OwnerId;
    a.TM_Descr__c = a.Description.substring(1, 100); 
    }
}

 

trigger OwnerLinkUpdate on Opportunity (before insert, before update ) {

for (Opportunity a :Trigger.NEW) {

a.Owner_Link__c = a.OwnerId;

a.TM_Descr__c = a.Description.substring(1, 100);

}

}

 

And below is my class I currently have but know it is wrong.

 

@isTest

private class TestOwner_link {

 

    public static testMethod void OwnerLinkTest() {

     Opportunity o = [Select o.Id, o.Description FROM Opportunity o WHERE o.ID = '006Q0000005MIEK'];

o.Description = 'P123456789 - Appt Secured 9/21/2010 16:00 - Test Description to see if this will update correctly.';

        update o;

    

       Opportunity opp = new Opportunity(name = 'IS TEST 2' , StageName = 'Prospect',

        AccountId = '001Q000000EmsJs', OwnerId = '00580000001khcs',

        CloseDate = date.newinstance(2010, 10, 21), Product_Service_Type__c = 'Alarm',

        Product_Service_Detail__c = 'Burglar Alarm System', Type = 'NEW',

        Level__c = 'Recommended (We seek out the account to give them a proposal.)',

        LeadSource = 'Sales Rep Generated', Description = 'Text', External_ID__c = 'P123456789');

       insert opp;

 

    }

}

 

 

Any help would be appreciated, I have been reading posts and can't seem to find anything that seems to make the class work and the trigger cover 100%.

 

 

 

bob_buzzardbob_buzzard

Can you tell us what error you are seeing?

hisrinuhisrinu

You are hardcoding the Ids that might be the problem, instead of hardcoding the AccountId and OwnerId you can insert these records in test method.

 

If you can't really add a user record then you can query where profile name is system administrator and assign this user id as owner

 

If you do the above changes it should work fine

TrueCloudTrueCloud

As Bob and Srini pointed out, can you let us know the error,and hardcoded ids might be another issue. I am assuming that you may be hitting the system nullpoint execption error, attempt to dereference a null object. If that is the case then we have to figure out what is not matching vs. what has been hard coded. 

 

Hope this helps.

mbrewmbrew

Here is the errors I get in the FORCE.com IDE

 

- System.DmlException: Update failed. First exception on row 0 with id 006Q0000005MIEKIA4; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OwnerLinkUpdate: execution of BeforeUpdate caused by: System.StringException: Ending position out of bounds: 100 Trig

 

- Class.TestOwner_link.OwnerLinkTest: line 28, column 9  External entry point

TrueCloudTrueCloud

External Entry point usually means that your ID is not correct. I would recommend checking the IDs that you are using to resolve this issue.

 

 

Please mark this as a solution if this resolves the issue.

Dev_NeilDev_Neil

I created this class as below off of the example and I get the same External entry point error.  I'm not hardcoding ID numbers at all.

 

 

@isTest
private class OwnerLinkTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
    List<Opportunity> opps = new List<Opportunity>{};
        
    for(Integer i = 0; i < 200; i++){
        Opportunity a = new Opportunity(Name = 'Test Opportunity ' + i);
        opps.add(a);
    }

    // Start the test, this changes governor limit context to 
    // that of trigger rather than test. 
    test.startTest();
		
    // Insert the Account records that cause the trigger to execute.
    insert opps; 
		
    // Stop the test, this changes limit context back to test from trigger.
    test.stopTest();
		
    // Query the database for the newly inserted records.
    List<Opportunity> insertedOpp = [SELECT Name, Description 
                                      FROM Opportunity
                                      WHERE Id IN :opps];
		
    // Assert that the Description fields contains the proper value now.
    for(Opportunity a : insertedOpp){
      System.assertEquals(
        'This Account is probably left over from testing. It should probably be deleted.', 
        a.Description);
    }
}
}

 

sravanthi_84sravanthi_84

The following test class works......

 

 @isTest
private class TestOwner_Link { 

    public static testMethod void OwnerLinkTest() {

     Account a = new Account();
     a.Name='Test Account'; /* Insert all the require fields in your account object */
      
     insert a;
          
     Opportunity opp = new Opportunity();
     opp.Name='IS TEST 2';
     opp.StageName='Prospect';
     opp.AccountId=a.Id;
     opp.CloseDate=system.Today();
     opp.Product_Service_Type__c='Alarm';
     opp.Product_Service_Detail__c='Burglar Alarm System';
     opp.Type='New';
     opp.Level__c='Recommended (We seek out the account to give them a proposal.)';
     opp.LeadSource='Sales Rep Generated';
     opp.Description='I have got a trigger I wrote and put in our sandbox, it works great and production has all the same fields.  So now I have to build the class to test it. I thought I had it and it errors out instead';
     opp.External_ID__c='P123456789';
     
     insert opp;
     

     
     }

}

 

 

Please try this and let me know if this works for you.