You need to sign in to do that
Don't have an account?
Jeff G
Can't insert custom object in test method
Hi All,
I'm having trouble testing a trigger I have for my Opportunity objects.
I have a custom object 'QQ' which is in a master-detail relationship with my Opportunity, and whenever the 'Total' field in the QQ is changed, this rolled up to my Opportunity, which then updates the Opportunity Probability via the trigger.
The problem I'm having is inserting a QQ in my test class. Each time I run the test, I get the following error:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity__c]: [Opportunity__c]
My test class code is the following:
@isTest
public class TestUpdateProbTrigger {
static testMethod void insertNewOpp() {
account acct = new account(Name = 'testing');
insert acct;
Opportunity o = new Opportunity(
Name = 'testing',
AccountId = acct.id,
CloseDate = System.today(),
StageName = 'Warm Prospect'
);
QQ__c q = new QQ__c (
Name = 'test'
S1_x__c = true, //these 4 fields can be ignored
L1_x__c = true,
C1_x__c = true,
Q1_x__c = true,
Opportunity__r = o,
Opportunity__c = o.Id //here I provided the Opportunity__c field for the QQ object, and yet I still get that error
);
o.QQ__c = q.Id;
insert o;
insert q;
q.D1_x__c = true;
update q;
System.assertEquals('Qualify', o.StageName); //this is just to verify that the stage name changed correctly
}
}
There is also a lookup relationship between the QQ and the Opportunity, though I doubt that is relevant to this. Does anyone have any suggestions? Thanks in advance!
Best,
Jeff
I'm having trouble testing a trigger I have for my Opportunity objects.
I have a custom object 'QQ' which is in a master-detail relationship with my Opportunity, and whenever the 'Total' field in the QQ is changed, this rolled up to my Opportunity, which then updates the Opportunity Probability via the trigger.
The problem I'm having is inserting a QQ in my test class. Each time I run the test, I get the following error:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity__c]: [Opportunity__c]
My test class code is the following:
@isTest
public class TestUpdateProbTrigger {
static testMethod void insertNewOpp() {
account acct = new account(Name = 'testing');
insert acct;
Opportunity o = new Opportunity(
Name = 'testing',
AccountId = acct.id,
CloseDate = System.today(),
StageName = 'Warm Prospect'
);
QQ__c q = new QQ__c (
Name = 'test'
S1_x__c = true, //these 4 fields can be ignored
L1_x__c = true,
C1_x__c = true,
Q1_x__c = true,
Opportunity__r = o,
Opportunity__c = o.Id //here I provided the Opportunity__c field for the QQ object, and yet I still get that error
);
o.QQ__c = q.Id;
insert o;
insert q;
q.D1_x__c = true;
update q;
System.assertEquals('Qualify', o.StageName); //this is just to verify that the stage name changed correctly
}
}
There is also a lookup relationship between the QQ and the Opportunity, though I doubt that is relevant to this. Does anyone have any suggestions? Thanks in advance!
Best,
Jeff
Look at the bold font those are the changes required.
@isTest
public class TestUpdateProbTrigger {
static testMethod void insertNewOpp() {
account acct = new account(Name = 'testing');
insert acct;
Opportunity o = new Opportunity(
Name = 'testing',
AccountId = acct.id,
CloseDate = System.today(),
StageName = 'Warm Prospect'
);
insert o;
Opportunity opty = [Select Id, Name from Oportunity where Id=: o.Id limit 1];
system.assert(o.Name, 'testing');
QQ__c q = new QQ__c (
Name = 'test'
S1_x__c = true, //these 4 fields can be ignored
L1_x__c = true,
C1_x__c = true,
Q1_x__c = true,
Opportunity__r = o,
Opportunity__c = o.Id //here I provided the Opportunity__c field for the QQ object, and yet I still get that error
//* Opportunity Field API Name on the QQ = o.Id is enough.
);
o.QQ__c = q.Id;
insert q;
q.D1_x__c = true;
update q;
System.assertEquals('Qualify', o.StageName); //this is just to verify that the stage name changed correctly
}
}
If this solves your problem make this as a solution.
All Answers
1. You can't assign an opportunity ID till you have inserted the opportunity. So you should be inserting "o" before you are creating "q"
2. I don't think you can assign a whole object to a field. IE Opportunity__r = o.id is valid BUT Opportunity__r = o is NOT valid.
Not sure if that totally fixed your problem, but hope it helps.
Thanks,
Look at the bold font those are the changes required.
@isTest
public class TestUpdateProbTrigger {
static testMethod void insertNewOpp() {
account acct = new account(Name = 'testing');
insert acct;
Opportunity o = new Opportunity(
Name = 'testing',
AccountId = acct.id,
CloseDate = System.today(),
StageName = 'Warm Prospect'
);
insert o;
Opportunity opty = [Select Id, Name from Oportunity where Id=: o.Id limit 1];
system.assert(o.Name, 'testing');
QQ__c q = new QQ__c (
Name = 'test'
S1_x__c = true, //these 4 fields can be ignored
L1_x__c = true,
C1_x__c = true,
Q1_x__c = true,
Opportunity__r = o,
Opportunity__c = o.Id //here I provided the Opportunity__c field for the QQ object, and yet I still get that error
//* Opportunity Field API Name on the QQ = o.Id is enough.
);
o.QQ__c = q.Id;
insert q;
q.D1_x__c = true;
update q;
System.assertEquals('Qualify', o.StageName); //this is just to verify that the stage name changed correctly
}
}
If this solves your problem make this as a solution.