I am trying to write a test class for a tigger and ansure why I am getting the following error –
Field is not writeable: OpportunityPartner.OpportunityId at line 40
column 68// test 2: 1 opppart, not End-User == null
OpportunityPartner opppart1 = new OpportunityPartner(OPPORTUNITYID=o.id, ACCOUNTTOID=a1.id, role='Consultant');
insert opppart1;
Any help would be much appreciated. Thank you!
Full Class
@isTest
private class Populate_Partner_onOpp_fromopppart {
static testMethod void EndUserPartnerTest() {
// create test account
Account a1 = new Account(Name = 'My Account A', Industry = 'School Bus', Phone = '9996661234', Territory__c = 'School Bus - Canada', Status__c = 'customer', Price_Level__c = 'standard',
BillingStreet = 'to come', BillingCity = 'to come', BillingCountry = 'canada', BillingPostalCode = '90210', BillingState = 'Alberta',
ShippingStreet = 'to come', ShippingCity = 'to come', ShippingCountry = 'canada', ShippingPostalCode = '90210', ShippingState = 'Alberta' );
insert a1;
Account a2 = new Account(Name = 'My Account B', Industry = 'School Bus', Phone = '9996661234', Territory__c = 'School Bus - Canada', Status__c = 'customer', Price_Level__c = 'standard',
BillingStreet = 'to come', BillingCity = 'to come', BillingCountry = 'canada', BillingPostalCode = '90210', BillingState = 'Alberta',
ShippingStreet = 'to come', ShippingCity = 'to come', ShippingCountry = 'canada', ShippingPostalCode = '90210', ShippingState = 'Alberta' );
insert a2;
Account a3 = new Account(Name = 'My Account C', Industry = 'School Bus', Phone = '9996661234', Territory__c = 'School Bus - Canada', Status__c = 'customer', Price_Level__c = 'standard',
BillingStreet = 'to come', BillingCity = 'to come', BillingCountry = 'canada', BillingPostalCode = '90210', BillingState = 'Alberta',
ShippingStreet = 'to come', ShippingCity = 'to come', ShippingCountry = 'canada', ShippingPostalCode = '90210', ShippingState = 'Alberta' );
insert a3;
Account a4 = new Account(Name = 'My Account D', Industry = 'School Bus', Phone = '9996661234', Territory__c = 'School Bus - Canada', Status__c = 'customer', Price_Level__c = 'standard',
BillingStreet = 'to come', BillingCity = 'to come', BillingCountry = 'canada', BillingPostalCode = '90210', BillingState = 'Alberta',
ShippingStreet = 'to come', ShippingCity = 'to come', ShippingCountry = 'canada', ShippingPostalCode = '90210', ShippingState = 'Alberta' );
insert a4;
// create test opp
Opportunity o = new Opportunity(name='test Opp', closedate=system.today(), stagename='Initial Communication', Market__c = 'Transit', ACCOUNTID=a4.id);
insert o;
// test 1: no oppparts == null
o = [select EndUser_Partner__c from opportunity where id=:o.id];
o.description='initial test';
update o;
system.assert(o.EndUser_Partner__c == null);
// test 2: 1 opppart, not End-User == null
OpportunityPartner opppart1 = new OpportunityPartner(OPPORTUNITYID=o.id, ACCOUNTTOID=a1.id, role='Consultant');
insert opppart1;
opppart1 = [select createddate, opportunityid, ACCOUNTTOID, role, isprimary from OpportunityPartner where id = :opppart1.id];
system.debug('************ opppart is:'+opppart1);
o.description='test';
update o;
o = [select EndUser_Partner__c from opportunity where id=:o.id];
system.assert(o.EndUser_Partner__c == null);
// test 3: 1 opppart, End-User == c1
opppart1.role == 'End-User';
update opppart1;
o.description='test2';
update o;
o = [select EndUser_Partner__c from opportunity where id=:o.id];
system.assert(o.EndUser_Partner__c == a1.id);
// test 4: 2 opppart, 1 not End-User, 1 End-User == c2
OpportunityPartner opppart2 = new OpportunityPartner(opportunityid=o.id, ACCOUNTTOID=a2.id, role='End-User', isprimary=true);
insert opppart2;
opppart2 = [select createddate, opportunityid, role, ACCOUNTTOID, isprimary from OpportunityPartner where id = :opppart2.id];
opppart1.role == 'Consultant';
update opppart1;
o.description='test3';
update o;
o = [select EndUser_Partner__c from opportunity where id=:o.id];
system.assert(o.EndUser_Partner__c == a2.id);
}
}
Best Answer
OpportunityPartner records are read-only and are created automatically by Salesforce
Also, they share the 00I keyprefix with Partner. As such, I tend to think of them as a specialised view over Partner.
So you shouldn't need to explicitly insert the record during a test case. The Partner object that you insert will have
accountToId
to an Account andopportunityId
to an Opportunity on a different Account.