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
ruchika Nayyarruchika 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 
  
VineetKumarVineetKumar
Please refer the below code
trigger createAssetonClosedWon on Opportunity (after insert, after update) {
    Opportunitylineitem[] OLI = [Select Quantity,UnitPrice,PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description from opportunitylineitem, opportunity.AccountID where opportunityId IN: trigger.new AND iswon = true AND hasOpportunitylineitem = true];
	List<Asset> assetList = new List<Asset>();        
	for(opportunitylineitem ol:OLI){
		a = new asset();
		a.AccountId=ol.opportunity.AccountID;
		a.Quantity=ol.quantity;		
		a.Product2Id=ol.PricebookEntry.Product2Id;
		a.price=ol.UnitPrice;
		a.Description=ol.Description;
		a.name=ol.PricebookEntry.Product2.Name;
		assetList.add(a);	
	}		
	insert assetList;           
}
Rahul Gupta 137Rahul Gupta 137
Error: Compile Error: Didn't understand relationship 'Opportunity.AccountId' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 2 column 33

i tried your code and

this is error  vineet sir can you help with it ?
VineetKumarVineetKumar
Seems like a type on my end. Try the below code:
trigger createAssetonClosedWon on Opportunity (after insert, after update) {
	List<Asset> assetList = new List<Asset>();        
	for(opportunitylineitem ol : [SELECT Quantity,UnitPrice,PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description,
										Opportunity.AccountID
								 FROM opportunitylineitem 
								 WHERE opportunityId IN: trigger.new AND iswon = true AND hasOpportunitylineitem = true]){
		a = new asset();
		a.AccountId=ol.opportunity.AccountID;
		a.Quantity=ol.quantity;		
		a.Product2Id=ol.PricebookEntry.Product2Id;
		a.price=ol.UnitPrice;
		a.Description=ol.Description;
		a.name=ol.PricebookEntry.Product2.Name;
		assetList.add(a);	
	}		
	insert assetList;           
}
Rahul Gupta 137Rahul Gupta 137
i have fixed it completely
trigger createAssetonClosedWon on Opportunity (after insert, after update) {
    Opportunitylineitem[] OLI = [Select Quantity,UnitPrice,PricebookEntry.Product2Id, 
                                 PricebookEntry.Product2.Name, Description from opportunitylineitem 
                                 where opportunityId IN: trigger.new AND Opportunity.iswon = true AND Opportunity.hasOpportunitylineitem = true];
    List<Asset> assetList = new List<Asset>();        
    for(opportunitylineitem ol:OLI){
       asset a = new asset();
        a.AccountId=ol.opportunity.AccountID;
        a.Quantity=ol.quantity;     
        a.Product2Id=ol.PricebookEntry.Product2Id;
        a.price=ol.UnitPrice;
        a.Description=ol.Description;
        a.name=ol.PricebookEntry.Product2.Name;
        assetList.add(a);   
    }       
    insert assetList;           
}