function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Avinash AngasayAvinash 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
mukesh guptamukesh gupta
Hi Avinash

Please follow below code:-
 
Account a1 = new Account(Name ='acc1'); insert a1;

Contact cont1 = new Contact(); 
cont1.FirstName='Test Con1'; 
cont1.LastName='Test Con1'; 
cont1.Accountid= a1.id; 
insert cont1;

Contact cont2 = new Contact();
cont2.FirstName='Test Con2';
cont2.LastName='Test Con2';
cont2.Accountid= a1.id;
insert cont2;

Id pricebookId = Test.getStandardPricebookId();

//Create your product
Product2 prod = new Product2(
     Name = 'Product X',
     ProductCode = 'Pro-X',
     isActive = true
);
insert prod;

//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;

//create your opportunity line item.  This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = opp.Id,
     AccountId = a1.Id,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
     TotalPrice = quantity * pbEntry.UnitPrice
);
insert oli;

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Avinash AngasayAvinash Angasay
I have written code like this......Just Once go through This.... And i got thid error while i am runing it...

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);
      }   
}