onAssestWhenAssestAddedOppAndOPLCreated on Asset

trigger onAssestWhenAssestAddedOppAndOPLCreated on Asset (after insert)
{

  List<Opportunity> lstopp = new List<Opportunity>();
  List<OpportunityLineItem> lstopl = new List<OpportunityLineItem>();
  List<PricebookEntry> lstpbe = new List<PricebookEntry>();
  for(Asset a:trigger.new)
  {
   Opportunity opp = new Opportunity();
   opp.Name= a.Name+'_'+'Opportunity';
   opp.CloseDate=Date.Today();
   opp.stageName='won';
   opp.AccountId=a.AccountId;
    lstopp.add(opp);  
  }
            lstpbe =[select id from PricebookEntry limit 2];
  if(lstopp.size()>0)
  {
  insert lstopp;
      if(lstpbe.size()>0)
 {

  for(Opportunity oppObj:lstopp)
   {
    for(PricebookEntry ids:lstpbe)
             {
  OpportunityLineItem opl = new OpportunityLineItem();
  opl.OpportunityId=oppObj.id;
  opl.Description='just for chek';
  opl.Quantity=5;
  opl.UnitPrice=2;
  opl.PriceBookEntryId=ids.id;
  lstopl.add(opl);
       }
 
        }
    insert lstopl;
   }

  }
 
 
   
 

 
 
  }

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page