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;
}
}
}
{
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
Post a Comment