You need to sign in to do that
Don't have an account?
Adriana Smith 9
Need Help with Trigger Error
Hello, I'm building a trigger that I had at a previous company where an asset is populated on the account, from the listed products on an Opportunity, anytime it is Closed Won.
the trigger is:
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 UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Project_Association__c, Product_Family__c, Product_Category__c, Converted_to_Asset__c
From OpportunityLineItem
where OpportunityId = :opptyId and Converted_to_Asset__c = false];
Asset[] ast = new Asset[]{};
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity = ol.Quantity;
a.Price = ol.UnitPrice;
a.PurchaseDate = o.CloseDate;
The error I'm getting is: Error: Compile Error: expecting right curly bracket, found '<EOF>' at line 17 column 0
I added a curly bracket on line 17, so it pushed the error to line 18. What am I missing?
the trigger is:
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 UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Project_Association__c, Product_Family__c, Product_Category__c, Converted_to_Asset__c
From OpportunityLineItem
where OpportunityId = :opptyId and Converted_to_Asset__c = false];
Asset[] ast = new Asset[]{};
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity = ol.Quantity;
a.Price = ol.UnitPrice;
a.PurchaseDate = o.CloseDate;
The error I'm getting is: Error: Compile Error: expecting right curly bracket, found '<EOF>' at line 17 column 0
I added a curly bracket on line 17, so it pushed the error to line 18. What am I missing?
Please try this I have made the changes:))
let me know what happens?
Thanks
All Answers
I have removed the Compile error.
Try it once
let me know what happens
Thanks:)
why are you using soql inside the for loop.
please try the below code ... Please let me know, if this help...
thanks
shivdeep
.
Error:Apex trigger CreateAssetonClosedWon caused an unexpected exception, contact your administrator: CreateAssetonClosedWon: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.PricebookEntry: Trigger.CreateAssetonClosedWon: line 16, column 1
please query all the fields at line no. 9 which is used for updation like pricebookentry, product2, opportunityId and related fields of opportunitylineitem object.
thanks
shivdeep
use this query at line no.9
Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c
From OpportunityLineItem
thanks
Shivdeep
Trigger CreateAssetonClosedWon on Opportunity (after insert, after update){
Set<string> OppsIds = new set<string>();
For(opportunity o : trigger.new){
OppsIds.add(o.id);
}
List<Asset> assets = new List<Asset>();
Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c
From OpportunityLineItem
For(opportunity opp : trigger.new){
For (opportunityLineItem Oli : mapOpp.values()){
If(opp.iswon == true && opp.hasopportunitylineitem ==true){
Asset a = new asset();
a.Accountid = opp.Accountid;
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;
}
no...
lIke this....
Map<id, opportunityLineItem> mapOpp = new Map<id, opportunityLineItem>([Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c From OpportunityLineItem]);
thanks
are you running this trigger without adding any product related to the opportunity?
This trigger is working fine on my org. Please check your opportunity record.
You can do one more thing that between line no.14 and 15 add
a.name = Oli.pricebookentry.product2.Name;
If it ll not work then you are doing a wrong process.
Thanks
Error:Apex trigger CreateAssetonClosedWon caused an unexpected exception, contact your administrator: CreateAssetonClosedWon: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity]: [Opportunity]: Trigger.CreateAssetonClosedWon: line 27, column 1
I added your line above between 14 and 15 and its still not working.
you are getting this error because you have missed defining one of the required fields on assets.
Please check that by creating an asset through UI
So you can know what all fields are required to create assets and then check all the fields are present in the trigger asset instance or not
Hope it will help you
Thanks:)
a.Accountid = opp.Accountid;
a.product2id = Oli.pricebookentry.product2id;
a.quantity = Oli.quantity;
a.price = Oli.unitprice;
a.purchasedate = opp.closedate;
a.required_field = opp.field_of which_you_value_want_in_this_required_field
assets.add(a);
let me know what happens?
Thanks
Thanks:)
Please try this I have made the changes:))
let me know what happens?
Thanks
Skype ID- anurag.twopirconsulting
Deployment settings in Sandbox are done, I hit refresh in production, what am I missing?