You need to sign in to do that
Don't have an account?
Trigger to get Quote Line Items into custom object
Hi,
Iam new to Apex.I have custom objects Order and Order Line Items (master detail).The order line items appears as a related list under the Order object.
Whenever an order is created from the quote,the quoteline items should be inserted as order line items ....This functionality is just like how the opportunity products are inserted as quote line items whenever a new quote is created from the opportunity.
Is it possible to do this using a trigger?
If yes ,can i please have some pointers to any sample trigger code that does this kind of insert from a related list of the master object to the related list of the detail object
Thanks!
Yes the functionality you are trying to implement can be done via triggers.
Here a sample code:-
trigger test1 on Order__c (after insert)
{
Order__c to=trigger.new[0];
Quote q=[select id from Quote where id=:to.Quote__c];
QuoteLineItem ql=[select id,...,....,.. from QuoteLineItem where Quote=:q.id];
for(QuoteLineItem qli:ql)
{
OrderLineItem__c lm=new OrderLineItem__c();
lm.Email__c=qli.Email;
.....
.....
.....
lm.TestObject__c=qli.id;
insert lm;
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Yes the functionality you are trying to implement can be done via triggers.
Here a sample code:-
trigger test1 on Order__c (after insert)
{
Order__c to=trigger.new[0];
Quote q=[select id from Quote where id=:to.Quote__c];
QuoteLineItem ql=[select id,...,....,.. from QuoteLineItem where Quote=:q.id];
for(QuoteLineItem qli:ql)
{
OrderLineItem__c lm=new OrderLineItem__c();
lm.Email__c=qli.Email;
.....
.....
.....
lm.TestObject__c=qli.id;
insert lm;
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Sorry for the late reply...
Thanks Ispita!!!!!
i used the following code,but i still get errors:
trigger test1 on Sales_Order__c (after insert)
{
Sales_Order__c to=trigger.new[0];
Quote q=[select id from Quote where id=:to.Quote__c];
QuoteLineItem ql=[select id,List Price,Product,Quantity,Quote Name,Sales Price,Subtotal,Total Price from QuoteLineItem where Quote=:q.id];
for(QuoteLineItem qli:ql)
{
Order_Line__c lm=new Order_Line__c();
lm.Email__c=qli.Email;
lm.List_Price__c=qli.List Price;
lm.Product__C=qli.Product;
lm.Order_Line__c=qli.id;
insert lm;
}
I get the following error:
Error: Compile Error: No such column 'SalesPrice' on entity 'QuoteLineItem'.
Is the API names for the quote line items fields correct?
i tried to refer the salesforce help for the names.
Please help.
Thanks !!!!!
I was completely stuck, until I read this post by Ispita_Navatar
Here is how I handled it, I'm going from Opportunity & Opportunity Lines to custom Order and Order Lines BTW
First trigger on Opportunity:
The second triiger is on the custom Order object
Obviously this doesn't have any exception handling and every save of opportunity creates an order. But it should move you in the right direction.
My questions for Ispita are
1.
Why did you specify [0] element at the end?
2. I have my head round the trigger syntax but struggling with moving the code out to a class for better reusability. I guess it's just a question of playing and reading as much as possible. Any advice?
3. What's the best way to call my first trigger from a button e.g. Create Order
Really enjoying figuring things out. The platform is very impressive.
Mike
Thanks a ton Ispita!!...
Thanks Mike for the inputs that helped me get this work...
My final code:
Thanks Again!!!!!
Anytime ...u r all most welcome
Hi Mike,
Answer to your question no. 3.
In order to execute a trigger on the click of a trigger you need to update an instance of the object as then the trigger will automatically get invoked- so in summary fire a DML on the object and as a result the trigger will get invoked.
Answer to your question no. 2
Yes it is a question of practice, for a class generally you manipulate the parameters passed to the respective function in class rather than refering to trigger.new or Trigger.old. as is done in a trigegr, plus all checks on if it is insert or update are not included.
Answer to question 1,
trigger.new[0] is used as in trigger mostly parameters are passed as an array and as I am just manipulating the first instance of the records submitted I use the 0th element.
Thanks
Ispita
Thanks for advice, building my knowledgebase of best-practices.
Cheers,
Mike
i have a problem to pass the quote total price to amount of opportunity when adding bulky quoteline items .How to solve this problem with out using queries in loop.Here is my code.
Thanks
Sreelekha
i tried this but i got compile error
that is : Variable does not exist: q.opportunityid at line 59 column 41
Hi,
Tried with the same trigger getting the Following Error
Error: Compile Error: sObject type 'QuoteLineItem' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 5 column 24
any help would be appreciated
Hello I am having a similar error. It keeps stating that I have the Incorrect SObject type: SPA__c should be Quote and Line 1 column 1. Does anyone have any ideas as to why this is happening? Here is my code as it stands. Use case is to bring over quote line items after SPA__c is created by a trigger from the Quote(generating the header information) Then the line items should pull over as well from the same quote.