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
anandanandanandanand 

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&nbsp;need an lookup relationship to OpportunityLineItem.</script>

but there is no lookup relationship to OpportunityLineItem

 How to solve it

rmehrmeh

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!!!

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Anand,

 

Fire the trigger on only 'After Insert' of Opportunity Line Item.

anandanandanandanand

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;
    }
}

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

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)

{

...............................

}

}