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
Jerry ClifftJerry Clifft 

Help with creating test method please

I have hte following code, which works, when a product is added to a opportunityl ine item, it copies the data + some other data as free text to a hiddel field. I want to deploy this from my sandbox to our production instance, but can not since it does not have a test method and I am lost on building one for this, any help is apprciated.


trigger OpportunityProuctCopytoHiddenField on Opportunity (before insert, before update) {
        List<String> NewList= new List<String>();

        if (t.HasOpportunityLineItem == true) 
             for(OpportunityLineItem OpLine: [SELECT CreatedBy.Name, Quantity, TotalPrice, LastModifiedDate, PriceBookEntry.Name, UnitPrice, ID FROM OpportunityLineItem WHERE OpportunityId =:t.Id] ) 
                NewList.add(' serving ');
                String str = '' + opLine.Quantity;
                NewList.add(' units passed has been authorized by ');
                NewList.add('and the transation has been logged into the record id: ');
                NewList.add(' on');
                String str1 = '' + opLine.lastModifiedDate;
                NewList.add(' GMT 0');
            for(String c : NewList){
                //t.Hidden_Products__c = t.Hidden_Products__c + c + '<br>';
                t.Hidden_Products__c = t.Hidden_Products__c + c ;
                system.debug('********' +  t.Hidden_Products__c);

Hi Jerry,


Have a look here. It's definitely worth reading and will help you write your test method in no time.




It should not be very difficult.........


what you need to do is write a test the method


create an opportunity opp with some values in the field (Opportunity opp = new Opportunity(); opp.somefield__c = somevalue)

insert that opportunity (insert opp)

write the query to get the id of inserted opportunity opp (String id = [select id from Opportunity where somefield__c = somevalue])

put that id for Opportunity ( = id)

Create one or more OpportunityLineItem oli and set the opportunity id field of that oli to the id you got from query.

insert the OpportunityLineItem oli.

change some value of the Opportunity opp (opp.somefield__c = someothervalue)

update the Opportunity opp again (update opp)


then major part of your code should be covered. hope this help.