You need to sign in to do that
Don't have an account?
Avinash Angasay
Dml Related Question
Please write code for the below problem....
Insert 5 records in Account object and display the list of records which are successfully inserted.Create related contact and opportunity to those successful accounts and add an opportunity product to those opportunity.. Using dml...
how to add opportunity product to opportunity
Thank you in advance
Insert 5 records in Account object and display the list of records which are successfully inserted.Create related contact and opportunity to those successful accounts and add an opportunity product to those opportunity.. Using dml...
how to add opportunity product to opportunity
Thank you in advance
Please follow below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Error->Line: 68, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, Before creating a custom price, create a standard price.: []
Please resolve or write a proper code as per the requirement------
Q.- Insert 5 records in Account object and display the list of records which are successfully inserted.Create related contact and opportunity to those successful accounts and add an opportunity product to those opportunity
public class checkDmil
{
public static void myMethod()
{
List<Account> a5 = new List<Account>();
for(integer i=0;i<5;i++)
{
Account aa = new Account(Name = 'Small Company '+(i+1)); //Creating 5 account records
a5.add(aa);
}
Database.SaveResult[] saveResult = Database.insert(a5,false);
for(Database.SaveResult s: saveResult)
{
if(s.isSuccess())
{
system.debug('Successfully Inserted and Record Id is: ' +s.getId());
}
}
List<Account> a6 = [Select id,Name from Account where name Like 'small %' ]; //query those records
system.debug(a6);
List<Contact> conn = new List<Contact>();
List<Opportunity> oppn = new List<Opportunity>();
for(Account a : a6) //Iterating account records
{
contact c1 = new contact(); //related contact creation by passing account id
c1.firstName = 'Rohan' + a.Name ;
c1.lastName = 'visu';
c1.AccountId = a.id;
contact c2 = new contact();//another related contact creation by passing account id
c2.firstName = 'Sohan' + a.Name;
c2.lastName = 'bisu';
c2.AccountId = a.id;
conn.add(c1);
conn.add(c2);
opportunity o1 = new Opportunity(Name = 'accopp '+ a.Name ,StageName = 'Prospecting', CloseDate = date.today()); //related opportunity creation using account record id
o1.AccountId = a.id;
oppn.add(o1);
}
insert conn; //Inserting contacts into apex database
insert oppn; //Inserting Opportunity into apex Database
List<Opportunity> Oppnn = new List<Opportunity>();
for(Account a : a6)
{
List<opportunity> Olist = [Select Name from Opportunity where Accountid = :a.id]; //Querying opportunity records related to Account
oppnn.addall(oList);
}
system.debug(Oppnn);
system.debug(Oppnn.Size());
Id pricebookId = '01s5i00000BtFBmAAN'; //I have created one in my org and used its Id
system.debug(PriceBookId);
product2 pro = new Product2( Name = 'Product X',ProductCode = 'Pro-X',isActive = true); //create product
insert pro;
system.debug(pro);
//Create product PriceBook Entry
PricebookEntry pbEntry = new PricebookEntry(Pricebook2Id = pricebookId ,Product2Id = pro.Id, UnitPrice = 100.00,IsActive = true);
insert pbEntry;
system.debug(pbEntry);
List<OpportunityLineItem> oppline = new List<OpportunityLineItem>();
for(Opportunity o:oppnn)
{
OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = o.Id,
PricebookEntryId = pbEntry.Id);
oppline.add(oli);
}
insert oppLine;
system.debug(oppline);
}
}