It can be done with help of External Id.
1 | Date dt = Date.today().addDays( 7 ); |
2 | Opportunity newOpportunity = new Opportunity(Name = 'shivasoft' , StageName = 'Prospecting' , CloseDate = dt); |
3 |
4 | /* |
5 | Create the parent reference. Used only for foreign key reference and doesn't contain any other fields. If we provide any other value it will give following error |
6 |
7 | System.DmlException: Insert failed. First exception on row 1; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity: Account: [] |
8 | */ |
9 |
10 | Account accountReference = new Account(MyExtID__c = 'SHIVA1234567' ); |
11 | newOpportunity.Account = accountReference; |
12 |
13 | // Create the Account object to insert. Same as above but has Name field. Used for the insert. |
14 | Account parentAccount = new Account(Name = 'Shiva' , MyExtID__c = 'SHIVA1234567' ); |
15 |
16 | Database.SaveResult[] |
17 | results = Database.insert( new SObject[] { parentAccount, newOpportunity }); |
No comments:
Post a Comment