+ Start a Discussion
selvakumar Anbazhaganselvakumar Anbazhagan 

Need test class coding

Hi ,

I need test class for this trigger . Please help me on this.

trigger BulkPricebookOpp on Opportunity (before insert,before update)
{   
    Id pbID = null;
    Id recordtypId = null;   
    pbID = [Select Name, Description From Pricebook2 where Name =: 'Bulk Price Book'].Id;
    recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'k) Bulk Services'].Id;   
   for(Opportunity obj : trigger.new)
   {
        if(obj.RecordTypeId  == recordtypId)
        {
            obj.Pricebook2Id = pbID;
        }
        else
        {
        if(Trigger.isUpdate){
                List<Pricebook2> prcbooklist = [select id,name from pricebook2];
                 Map<String,Id> prcbookMap = new Map<String,Id>();
                 Map<Id,String> cccAccmap = new Map<Id,String>();
                 Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
                 Set<Id> accountIds = new Set<Id>();                             
                if(!prcbooklist.isEmpty()){
                    for(Pricebook2 prcIter : prcbooklist){
                        prcbookMap.put(prcIter.name,prcIter.id);
                    }
                }
                System.debug('==prcmap'+prcbookMap);               
                for(Opportunity OppIdsItr : Trigger.new){
                    accountIds.add(OppIdsItr.accountid);
                    
                }
                for(Account AccItr : [SELECT id,Region__c FROM Account WHERE Id IN:accountIds]){
                    cccAccmap.put(AccItr.id,AccItr.Region__c);
                }           
                for(Opportunity OppItr : Trigger.new){
                    if(prcbookMap.size() > 0){    
                            
                        if (cccAccmap.get(OppItr.accountId) == 'P1'){
                            OppItr.Pricebook2Id = prcbookMap.get('Price Book - 1');
                        }
                        else if(cccAccmap.get(OppItr.accountId) == 'P2'){
                            OppItr.Pricebook2Id = prcbookMap.get('Price Book - 2');
                       }
                                            
                      }
                      System.debug('====assign'+OppItr.Pricebook2Id);
                }
            }
        }
   }
}
 
sandeep sankhlasandeep sankhla
Hi,

You can create a test class and in that class you can insert the opportunity and then you can updaet the opportunity record..It will cover the entire trigger code..

ex:
    Id pbID = null;
    Id recordtypId = null;   
    pbID = [Select Name, Description From Pricebook2 where Name =: 'Bulk Price Book'].Id;
    recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'k) 


Account obj = new Account(Name = 'Test');
insert obj;

Opportunity objOpp = new Opportunity(name = 'newTest', AcountId = obj.Id, recordtypeId = recordtypId .Id);
insert objOpp;

Opportunity objUOppt = new opportunity(Id =objOpp.Id );
update objUOppt ;

Pleae implement above code and let me know if it heps you...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer