You need to sign in to do that
Don't have an account?
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
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
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 ;)
Thx bob_buzzard, for your reply.
Following is my trigger.
You need to tell us what problem you are experiencing - are you seeing an error? if so, post that too.
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.
In your original post you said you couldn't create an opportunity in test mode. What did you base this statement on?
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.
In your trigger you have this line:
however, the opportunity that you insert is created as follows:
Note that the opportunity has no Transaction_Id__c field value - this means that your query will find no matches.
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.
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.
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.
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.
I tried every trial and error combination.
I'm under frustration.
Thanks,
Maddy.
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.
Thank you very much Swarnasankha.
Your solution works perfect. Now I am getting 100% test coverage.
Thanks once again. :-)
(^_^)