1

I always thought that Opportunity had a Master-Detail relationship with Account. Which means that you aren't able to create an Opportunity without an Account.

Today I ran apex code to create an Opportunity. It did not have AccountId set. So I expected it to not execute the code. Lo and behold, the Opportunity was created. If I then want to edit the opportunity, I can't save it because of the standard Salesforce settings which require an Account for an Opportunity.

I executed the following code:

        //Create an Opportunity
    Opportunity testOpportunity = new Opportunity(Name = 'Test Opp', CloseDate = Date.today(), StageName = 'Qualification');
    insert testOpportunity;
4
  • 1
    Please include the error message verbatim. Also, any org could implement Validation Rules which require an Account.
    – Adrian Larson
    Commented Jan 30, 2018 at 15:06
  • There is no error message because it's not failing. Like I mentioned: But I noticed today that I am able to insert an Opportunity without an Account through Execute Anonymous Window. Commented Jan 30, 2018 at 15:08
  • "Once you want to edit the Opportunity it will tell you that you can't save it because there's no Account related to the Opportunity." Can you please edit your post to be a bit more clear about what behavior you observe and where you are confused?
    – Adrian Larson
    Commented Jan 30, 2018 at 15:11
  • I thought it was clear that this wasn't the part of the question I was curious about. The main question was, can you insert an opportunity without an account, which I thought was impossible. I hope that makes it clear why I am confused. Commented Jan 30, 2018 at 15:15

1 Answer 1

4

Yes, the Opportunity.AccountId field is a Lookup. You can create an Opportunity record which has no Account. Here's what you get when you debug Opportunity.AccountId.getDescribe():

Schema.DescribeFieldResult[getByteLength=18;getCalculatedFormula=null;getCompoundFieldName=null;getController=null;getDefaultValue=null;getDefaultValueFormula=null;getDigits=0;getFilteredLookupInfo=null;getInlineHelpText=null;getLabel=Account ID;getLength=18;getLocalName=AccountId;getMask=null;getMaskType=null;getName=AccountId;getPrecision=0;getReferenceTargetField=null;getRelationshipName=Account;getRelationshipOrder=null;getScale=0;getSoapType=ID;getSobjectField=AccountId;getType=REFERENCE;isAccessible=true;isAggregatable=true;isAutoNumber=false;isCalculated=false;isCascadeDelete=false;isCaseSensitive=false;isCreateable=true;isCustom=false;isDefaultedOnCreate=false;isDependentPicklist=false;isDeprecatedAndHidden=false;isDisplayLocationInDecimal=false;isEncrypted=false;isExternalId=false;isFilterable=true;isGroupable=true;isHighScaleNumber=false;isHtmlFormatted=false;isIdLookup=false;isNameField=false;isNamePointing=false;isNillable=true;isPermissionable=true;isQueryByDistance=false;isRestrictedDelete=false;isSearchPrefilterable=true;isSortable=true;isUnique=false;isUpdateable=true;isWriteRequiresMasterRead=false;]

Specifically, getRelationshipOrder returns null, whereas if the relationship were Master-Detail, it would be either 0 or 1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .