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
MaddyConnectMaddyConnect 

Unable to create opportunity by test class

Hello friends,

 

                      I'm trying to create opportunity using test class but I unable to do the same. I'm writing test class for trigger. This trigger process opportunity, so I need to create it through test class but I fail to do so. But my trigger works fine.

 

Any solution?

Please help.

 

Thanks,

Maddy

Best Answer chosen by Admin (Salesforce Developers) 
SwarnasankhaSwarnasankha

I just checked your Test Class and found that while creating the Cheques_Incoming__c record, you are setting the value for Cheque_sent_by__c as 'opp.Name' without querying the Opportunity record. When you insert a record, you instantly get access to the ID of the record but for accessing any other field value for the inserted record you will need to query it.

 

Try adding this statement after you have inserted opp:

System.Debug('Value of Insert Opp ID ------------------------>' + opp.Id);

System.Debug('Value of Insert Opp Name ------------------------>' + opp.Name);

 

You will find that the first debug will have the ID value but the second will be empty.

 

Add this query after you have inserted opp:

Opportunity oppCreated = [Select Name From Opportunity Where Id =: opp.Id LIMIT 1];

 

While creating the Cheques_Incoming__c chq record, set the value for Cheque_sent_by__c to 'oppCreated.Name'.

 

Hope this helps.

All Answers

bob_buzzardbob_buzzard

There's no reason why you can't create an opportunity in a test class.  If the creation is failing its likely to be required fields, validation rules etc.

 

Can you post your code and the errors that you are getting?  Otherwise its going to be difficult to nail down ;)

MaddyConnectMaddyConnect

Thx bob_buzzard, for your reply.

 

Following is my trigger.

 

 

trigger UpsertChequeDetails on Cheques_Incoming__c (after insert, after update) 
{
  Set<String> TrnNumber = new Set<String>();
  Set<ID> OppID = new Set<ID>();
  Map<String, Cheques_Incoming__c> mapChqDetail = new Map<String, Cheques_Incoming__c>();
  Map<ID, Cheques_Incoming__c> mapFinalChqDetail = new Map<ID, Cheques_Incoming__c>();
  if(Trigger.isAfter)
  {
  for(Cheques_Incoming__c chq : Trigger.new)
  {
  if((chq.Cheque_sent_by__c!=null)||(chq.Cheque_sent_by__c!=' '))
  {
  String TrnNum=chq.Cheque_sent_by__c;
 String Trn_New = TrnNum.substring(0,3);
  if(Trn_New=='TRN')
  {
  TrnNumber.add(chq.Cheque_sent_by__c);
  mapChqDetail.put(chq.Cheque_sent_by__c, chq);
  }
  }
  }
  if(TrnNumber.size()>0)
  {
  List<sObject> Opprt = [Select Id, Name from Opportunity where Transaction_ID__c in : TrnNumber];
  if(Opprt.size()>0)
  {  
  for(sObject idOpprt : Opprt)
  {
  OppID.add(idOpprt.Id);
  String strTrnNum = String.valueOf(idOpprt.get('Name'));
  Cheques_Incoming__c chqGetData = mapChqDetail.get(strTrnNum);
  mapFinalChqDetail.put(idOpprt.Id, chqGetData);
  }
  List<sObject> TrnMOPDet = [Select Name, Transaction_Id__c, Cheque_Number__c, BankName__c,  BranchName__c, MICR__c, DateOfCheque__c from Transaction_MOP_Detail__c where  Transaction_Id__c in : OppID];

  for(sObject nameMOP : TrnMOPDet)
  {
  Transaction_MOP_Detail__c UpdateTrnMopDet = (Transaction_MOP_Detail__c)nameMOP;
  String TrnIDInMOP = String.valueOf(nameMOP.get('Transaction_Id__c'));
  Cheques_Incoming__c chqGetData = (Cheques_Incoming__c)mapFinalChqDetail.get(TrnIDInMOP);

  String chqNumber = chqGetData.Name;
  UpdateTrnMopDet.Cheque_Number__c = chqNumber;
 UpdateTrnMopDet.Tracking_Id__c = chqNumber;
 UpdateTrnMopDet.BankName__c = chqGetData.Bank_drawn_on__c;
 UpdateTrnMopDet.BranchName__c = chqGetData.Bank_Branch__c;
 UpdateTrnMopDet.MICR__c = chqGetData.MICR__c;
 UpdateTrnMopDet.DateOfCheque__c = chqGetData.Date_of_Cheque__c;
 update UpdateTrnMopDet;
 }
 }
 }
 }
}
Following is test class:
@isTest
private class TestUpsertChequeDetails
{
 static testMethod void myUnitTest() 
 {
 datetime t2 = System.now();
    date clsDate = Date.newInstance(t2.year(),t2.month(),t2.day());
          Currency__c C= new Currency__c(Conversion_Rate_To_INR__c=0.0126, CurrencyIsoCode='INR');
 insert C;
 Tax_Paying_Status__c Tps = new Tax_Paying_Status__c(Long_Name__c='testindia',Name__c='RI',CurrencyIsoCode='INR');
    insert Tps;
  
   Mode_of_Payment__c Mop = new Mode_of_Payment__c(Name='Mop-000003', MOP_Name__c='Cheque', CurrencyIsoCode=c.CurrencyIsoCode);
   insert Mop;
Contact con = new Contact(FirstName = 'TestName', Bank_Account_Number__c = '1234', LastName = 'TestName', Salutation ='Mr.', Prg_primary_email__c = 'test@test.com', GI_personal_email__c = 'test1@test.com', PRG_Employee_ID__c = '1234');
   con.Prg_primary_email__c ='test22@test.com'; 
  con.GI_personal_email__c ='test1@gh.com';
     con.Bank_Account_Number__c=null;
insert con;
        Opportunity opp = new Opportunity(CloseDate = clsDate, Name = 'TRN-0100438691', StageName = 'Pending', Donor__c = con.Id, Tax_Paying_Status__c = Tps.Id, currency__C = c.Id, Mop_id__c = Mop.Id);
        insert opp;
        Transaction_MOP_Detail__c tmd = new Transaction_MOP_Detail__c(MOP__c=Mop.Id, GI_MOP_Currency_Amt__c=1600, GI_MOP_Currency__c=c.Id, Transaction_Id__c=Opp.Id);
        insert tmd;
    
     datetime t1 = System.now();
     date d1 = Date.newInstance(t1.year(),t1.month(),t1.day());
    
       Cheques_Incoming__c chq = new Cheques_Incoming__c(Name='47577657', CurrencyIsoCode = c.CurrencyIsoCode, Cheque_sent_by__c=opp.Name, Bank_drawn_on__c='HDFC', Bank_Branch__c='Mumbai', MICR__c='49', Date_of_Cheque__c=d1);
       insert chq;
    }
}
Revert back if u unable to understand trigger or test class. I will explain in more detail.
Thanks,
Maddy

 

bob_buzzardbob_buzzard

You need to tell us what problem you are experiencing - are you seeing an error?  if so, post that too.

MaddyConnectMaddyConnect

I unable to get test coverage of 75%. Otherwise trigger working fine on sandbox. But when I run test class, it gives only 47% code coverage.

bob_buzzardbob_buzzard

In your original post you said you couldn't create an opportunity in test mode.  What did you base this statement on?

MaddyConnectMaddyConnect

To test trigger, in test class im trying to create opportunity but my code failed where it query to opportunity object, where it get zero records instead of test class inserted opportunity already.

bob_buzzardbob_buzzard

In your trigger you have this line:

 

 

  List<sObject> Opprt = [Select Id, Name from Opportunity where Transaction_ID__c in : TrnNumber];

 

 

however, the opportunity that you insert is created as follows:

 

 

Opportunity opp = new Opportunity(CloseDate = clsDate, Name = 'TRN-0100438691', StageName = 'Pending', Donor__c = con.Id, Tax_Paying_Status__c = Tps.Id, currency__C = c.Id, Mop_id__c = Mop.Id);

 

Note that the opportunity has no Transaction_Id__c field value - this means that your query will find no matches.

 

 

 

 

MaddyConnectMaddyConnect

True,

But field Transaction_Id__c is generated automatically. 

And it will not allow to insert in the field 'Transaction_Id__c' of opportunity through test class.

Still I modified query in trigger as per your suggestion  as follows:

 

List<sObject> Opprt = [Select Id, Name from Opportunity where Name in : TrnNumber];

 

but problem not solved, it is unable fetch records from opportunity.

 

bob_buzzardbob_buzzard

I didn't suggest you modified your trigger, I simply pointed out a reason why it might not be working.

 

I'd suggest that you add some debug lines to your trigger and check at what point your data is not matching as expected.

MaddyConnectMaddyConnect

bob-buzzard,

 

I tried putting debug lines. It works fine when I create through browser but same thing if I try to do from Test class for coverage purpose, it's not working.

 

Thank you for your time and posts.

 

Regards,

Maddy.

bob_buzzardbob_buzzard

Surely the debug lines will show you what the difference is?  I've got at least a dozen projects that create opportunities via test code, so I can't imagine its to do with the context.

MaddyConnectMaddyConnect

I tried every trial and error combination. 

I'm under frustration.

 

Thanks,

Maddy.

SwarnasankhaSwarnasankha

I just checked your Test Class and found that while creating the Cheques_Incoming__c record, you are setting the value for Cheque_sent_by__c as 'opp.Name' without querying the Opportunity record. When you insert a record, you instantly get access to the ID of the record but for accessing any other field value for the inserted record you will need to query it.

 

Try adding this statement after you have inserted opp:

System.Debug('Value of Insert Opp ID ------------------------>' + opp.Id);

System.Debug('Value of Insert Opp Name ------------------------>' + opp.Name);

 

You will find that the first debug will have the ID value but the second will be empty.

 

Add this query after you have inserted opp:

Opportunity oppCreated = [Select Name From Opportunity Where Id =: opp.Id LIMIT 1];

 

While creating the Cheques_Incoming__c chq record, set the value for Cheque_sent_by__c to 'oppCreated.Name'.

 

Hope this helps.

This was selected as the best answer
MaddyConnectMaddyConnect


Thank you very much Swarnasankha.

Your solution works perfect. Now I am getting 100% test coverage.

 

Thanks once again. :-)


SwarnasankhaSwarnasankha

(^_^)