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) {
for(Opportunity t:trigger.new)
      {
        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(OpLine.PriceBookEntry.Name);
                NewList.add(' serving ');
                String str = '' + opLine.Quantity;
                NewList.add(str);
                NewList.add(' units passed has been authorized by ');
                NewList.add(OpLine.CreatedBy.Name);
                NewList.add('and the transation has been logged into the record id: ');
                NewList.add(OpLine.ID);
                NewList.add(' on');
                String str1 = '' + opLine.lastModifiedDate;
                NewList.add(str1);
                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);
            }
        }
    }
}
florianhoehnflorianhoehn

Hi Jerry,

 

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

 

Florian

Sam27Sam27

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

 

what you need to do is write a test method...........in 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 (opp.id = 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.