You need to sign in to do that
Don't have an account?

Trigger help:Urgent Neede
In opportunity object having an standard related list "products".And another custom related list am using is "Accomodation ".
In Products related list ,if am adding products using add product button same products will be added into custom related list "Accomodation" also at the same time.
so my trigger in OpportunityLineItem is
trigger accc on OpportunityLineItem (before insert,after insert) {
List<Accomodation__c> lstAccomodation = new List<Accomodation__c>();
OpportunityLineItem to=trigger.new[0];
Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Product2.Family,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];
for(OpportunityLineItem qli:ql){
Accomodation__c lm=new Accomodation__c();
lm.Price__c=qli.ListPrice;
lm.Name=qli.PriceBookEntry.Name;
lm.OpportunityLineItem__c=to.id;
lstAccomodation.add(lm);
}
if(lstAccomodation != null && !lstAccomodation.isEmpty()){
insert lstAccomodation;
}
}
In this line lm.OpportunityLineItem__c=to.id; am having error
Error: Compile Error: Invalid field OpportunityLineItem__c for SObject Accomodation__c at line 13 column 9 |
because Accomodation object need an lookup relationship to OpportunityLineItem.</script>
but there is no lookup relationship to OpportunityLineItem
How to solve it
Hi anandanand,
The only thing i can understand from here is that check your schema.
The name of the field you have referred is not correct.
Hope this helps!!!
Hi Anand,
Fire the trigger on only 'After Insert' of Opportunity Line Item.
thanks i used after insert but it shows error
Apex trigger accc caused an unexpected exception, contact your administrator: accc: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.accc: line 5, column 18
trigger accc on OpportunityLineItem (after insert) {
List<Accomodation__c> lstAccomodation = new List<Accomodation__c>();
OpportunityLineItem to=trigger.new[0];
Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Product2.Family,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];
for(OpportunityLineItem qli:ql){
Accomodation__c lm=new Accomodation__c();
lm.Price__c=qli.ListPrice;
lm.Name=qli.PriceBookEntry.Name;
//lm.OpportunityLineItem__c=to.id;
lstAccomodation.add(lm);
}
if(lstAccomodation != null && !lstAccomodation.isEmpty()){
insert lstAccomodation;
}
}
Hi Anand,
Add a 'null' check to your list 'ql' before you iterate it in the for loop
if(ql != null && ql.size() > 0)
{
for(OpportunityLineItem qli: ql)
{
...............................
}
}