
Please try this I have made the changes:))let me know what happens?ThanksTrigger CreateAssetonClosedWon on Opportunity (after insert, after update){
Set<string> OppsIds = new set<string>();
Set<string> accIds = new set<string>();
List<Asset> assets = new List<Asset>();
For(opportunity o : trigger.new)
{
OppsIds.add(o.id);
accIds.add(o.accountid);
}
Map<id, opportunityLineItem> mapOpp = new Map<id, opportunityLineItem>([select Id, unitprice, quantity,pricebookentry.product2id from opportunityLineItem where opportunityId IN : OppsIds and converted_to_Asset__c = false]);
Contact con = [Select id from Contact where accountid =: accIds order by CreatedDate limit 1 ];
For(opportunity opp : trigger.new)
{
For (opportunityLineItem Oli : mapOpp.values())
{
If(opp.iswon == true && opp.hasopportunitylineitem ==true)
{
Asset a = new asset();
a.name = a.Opportunity_Name__c;
a.accountid = opp.Accountid;
a.contactid = con.id;
a.product2id = Oli.pricebookentry.product2id;
a.quantity = Oli.quantity;
a.price = Oli.unitprice;
a.purchasedate = opp.closedate;
assets.add(a);
}
}
}
If (assets.size()>0)
Insert assets;
}