You need to sign in to do that
Don't have an account?
sfdeveloper12
Create Opportunity Line Item Record when new opportunity is created
Hello,
I want to create automatic opportuinty line item when opportunity is created. How is this possible. I want to create of particular Product name suppose "Accomodation". I have written a code.
trigger insertOppLineItem on Opportunity (after insert, after update) {
system.debug('****************InTrigger**********************');
if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate)){
String OpportunityID ;
for(Opportunity o : Trigger.new){
system.debug('OpportunityID 8 -->'+o.Id);
OpportunityID = o.Id;
if(OpportunityID != null)
{
List<Opportunity> opp = [select Id , No_of_Nights__c from Opportunity where ID =: OpportunityID ];
system.debug('Opportunity 14.......... -->'+opp );
// List<OpportunityLineItem> vList = new List<OpportunityLineItem >();
for(OpportunityLineItem v : trigger.new){
o.No_of_Nights__c = v.Quantity;
//vList.add(v);
}
}
upsert v;
}
}
}
How is this achivable?
Thanks &Regards,
Utkarsha
I want to create automatic opportuinty line item when opportunity is created. How is this possible. I want to create of particular Product name suppose "Accomodation". I have written a code.
trigger insertOppLineItem on Opportunity (after insert, after update) {
system.debug('****************InTrigger**********************');
if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate)){
String OpportunityID ;
for(Opportunity o : Trigger.new){
system.debug('OpportunityID 8 -->'+o.Id);
OpportunityID = o.Id;
if(OpportunityID != null)
{
List<Opportunity> opp = [select Id , No_of_Nights__c from Opportunity where ID =: OpportunityID ];
system.debug('Opportunity 14.......... -->'+opp );
// List<OpportunityLineItem> vList = new List<OpportunityLineItem >();
for(OpportunityLineItem v : trigger.new){
o.No_of_Nights__c = v.Quantity;
//vList.add(v);
}
}
upsert v;
}
}
}
How is this achivable?
Thanks &Regards,
Utkarsha
To create opportunity line item first we need to crate pricbook,product and PriceBookEntry.
More Info please check oncebelow link ;
https://www.youtube.com/watch?v=kuYatMj-_4U
Hope this helps.
Thanks
Varaprasad
All Answers
To create opportunity line item first we need to crate pricbook,product and PriceBookEntry.
More Info please check oncebelow link ;
https://www.youtube.com/watch?v=kuYatMj-_4U
Hope this helps.
Thanks
Varaprasad
Utkarsha,
Try the Code below.
Trigger Code:-
Regards,
Ajay
If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.
trigger InsertOppLineItemOnOpp on Opportunity (after insert) {
Set<id> oId = new set<id>();
for(Opportunity opp : trigger.new){
oId.add(opp.Id);
}
Opportunity opp = [select Id , No_of_Nights__c,CurrencyIsoCode,RecordTypeId,recordtype.name,Arrival_Date__c,Guest_Name__c,
Departure_Date__c from Opportunity where ID =: oId];
system.debug('opp************'+opp);
PriceBookEntry p = [SELECT Id, Product2Id, Product2.Id, Product2.Name, CurrencyIsoCode FROM PriceBookEntry WHERE
Product2Id='01t5E000000btVk' and CurrencyIsoCode =: opp.CurrencyIsoCode and Pricebook2Id = '01s5E0000004rHG'];
system.debug('p***************'+p);
string recordtypename = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(opp.recordtypeid).getname();
system.debug(' recordtypename*************'+recordtypename);
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
for(Opportunity op : trigger.new){
if(opp.No_of_Nights__c!= null && opp.RecordType.Name == recordtypename){
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = opp.Id;
oli.Quantity = opp.No_of_Nights__c;
oli.PricebookEntryId = p.Id;
oli.Guest__c = opp.Guest_Name__c;
oli.Departure_Date__c = opp.Departure_Date__c;
oli.UnitPrice = 0;
oliList.add(oli);
system.debug('oliList------------'+oliList);
}
}
insert oliList;
}
}
This is working now. Both answers posted by you are correct.
i think this line only oli.PricebookEntryId=priceBookList[0].Id ;
please help me
Utkarsha,
Try the Code below.
//helper
trigger OpportunityToOpplineitem on Opportunity (after insert) {
if(trigger.Isafter){
if(trigger.Isinsert){
OpportunityToOpplineitemClass.myMethod(trigger.new);
}
}
}
//Handler
public class OpportunityToOpplineitemClass {
public static void myMethod(List<Opportunity>opplist){
List<OpportunityLineItem> opline = new List<OpportunityLineItem>();
List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry
WHERE Product2Id='01t5j0000046rvQAAQ' AND PriceBook2.isStandard=true LIMIT 1];
for(Opportunity opp : opplist){
if(opp.StageName== 'Closed Won'){
OpportunityLineItem op = new OpportunityLineItem();
op.OpportunityId= opp.Id;
op.PricebookEntryId=priceBookList[0].Id;
op.Quantity=1;
op.TotalPrice=100.0;
opline.add(op);
}
}
insert opline;
}
}