You need to sign in to do that
Don't have an account?
ruchika Nayyar
Create an asset when create an OpportunityLineItem with associated Account
is this Code is right??
trigger createAssetonClosedWon on Opportunity (after insert, after update) {
for(opportunity o:Trigger.New) {
if(o.iswon==true && o.hasOpportunitylineitem==true){
string opptyId= o.Id;
Opportunitylineitem[] OLI=[Select Quantity,UnitPrice,PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description from opportunitylineitem where opportunityId=:opptyId];
asset[] ast= New Asset[]{};
asset a= new asset();
for(opportunitylineitem ol:OLI){
a= new asset();
a.AccountId=a.AccountID;
a.Quantity=ol.quantity;
a.Product2Id=ol.PricebookEntry.Product2Id;
a.price=ol.UnitPrice;
a.Description=ol.Description;
a.name=ol.PricebookEntry.Product2.Name;
ast.add(a);
}
insert ast;
}
}
}
error
Apex trigger myruchika.createAssetonClosedWon caused an unexpected exception, contact your administrator: myruchika.createAssetonClosedWon: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: AccountId, ContactId (asset must have account and/or contact parent(s)): [AccountId, ContactId]: Trigger.myruchika.createAssetonClosedWon: line 22, column 1
trigger createAssetonClosedWon on Opportunity (after insert, after update) {
for(opportunity o:Trigger.New) {
if(o.iswon==true && o.hasOpportunitylineitem==true){
string opptyId= o.Id;
Opportunitylineitem[] OLI=[Select Quantity,UnitPrice,PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description from opportunitylineitem where opportunityId=:opptyId];
asset[] ast= New Asset[]{};
asset a= new asset();
for(opportunitylineitem ol:OLI){
a= new asset();
a.AccountId=a.AccountID;
a.Quantity=ol.quantity;
a.Product2Id=ol.PricebookEntry.Product2Id;
a.price=ol.UnitPrice;
a.Description=ol.Description;
a.name=ol.PricebookEntry.Product2.Name;
ast.add(a);
}
insert ast;
}
}
}
error
Apex trigger myruchika.createAssetonClosedWon caused an unexpected exception, contact your administrator: myruchika.createAssetonClosedWon: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: AccountId, ContactId (asset must have account and/or contact parent(s)): [AccountId, ContactId]: Trigger.myruchika.createAssetonClosedWon: line 22, column 1
i tried your code and
this is error vineet sir can you help with it ?