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
LALALALALALA 

Problem of how to assign value to field on Quote Line Item?

Hi, Everyone:)
I am new for salesforce, now I am doing about quote line Item.

my question is I am already get all value from another object which is I need assign those value to quote line Item, but I don't know how to assign value? I don't know what is pricebookEntryId and product2Id on quote line Item........can anybody give me some idea or sample?

Thanks you very much:)
pconpcon
Well pricebookEntryId is an instance of a PricebookEntry and Product2Id is an instance of Product2.  So you'll need to query those two object and use thos to create your object.
 
Map<Id, Product2> productMap = new Map<Id, Product2>([
    select Id
    from Product2
    where ...
]);

List<PricebookEntry> peList = [
    select Id
    from PricebookEntry
    where Product2Id in :productMap.keySet() and
        ...
];

You'll want to modify the query to to get the right Product2 and PricebookEntry.  Then you'll pick the right one for each Quote to generate your QuoteLineItem.  The logic to pick those is up to you and your organizational needs.
LALALALALALA

@pcon

Hi, this is my code.

trigger CreateQuoteLineItemFromQuote on Quote (after insert) {
    List<QuoteLineItem> qList = new List<QuoteLineItem>();  
    
    Map<String, OrderLineItem__c> oliMap = new Map<String, OrderLineItem__c>();
    
    //Get all OfwId from Quote
    List<Id> ofwIdList = New List<Id>();
   
    for(Quote q : trigger.new)
    {
        ofwIdList.add(q.QuoteOfw__c);
    }
    
    //Get all OrderLineItem from ofw
    List<OrderFromWeb__c> ofwList = [select Id,ofwOrderLineItem__c,
                                     ofwOrderLineItem__r.Price__c,
                                     ofwOrderLineItem__r.ProductionName__c,
                                     ofwOrderLineItem__r.Quantity__c,
                                     ofwOrderLineItem__r.Total_Inc_tax__c,
                                     ofwOrderLineItem__r.SKU__c
                                     from OrderFromWeb__c 
                                     where id in:ofwIdList];
    map<Id,OrderLineItem__c> ofwIdoliMap = new Map<Id,OrderLineItem__c>();
    
    for(OrderFromWeb__c ofw: ofwList)
    {
        ofwIdoliMap.put(ofw.Id, ofw.ofwOrderLineItem__r);
    }
    //end
    
    for (OrderLineItem__c oli: [select SKU__c, olitoquote__c from OrderLineItem__c where olitoquote__c in :trigger.newMap.keySet()]) 
    {
        oliMap.put(oli.olitoquote__c ,oli);
    }

//Problem:
        List<OrderLineItem__c> SKU = new List<OrderLineItem__c>([select id, sku__c from OrderLineItem__c where id in: ofwIdList]);    
          Map<Id, Product2> ProductMap = new Map<Id, Product2>(
        [select Id from Product2 where ProductCode =:SKU]);
        
        List<PricebookEntry> pkList = [
            select Id from PricebookEntry
            where Product2Id in: ProductMap.keySet()
        ];
    
    
    for (Quote q : trigger.new) 
    {
        QuoteLineItem qli = new QuoteLineItem(); 
        qli.Product2Id = ProductMap.get(q.QuoteOfw__c).id;
        qli.PricebookEntryId = 
        //qli.PricebookEntryId = ofwIdoliMap.get(q.quoteOfw__c).Product_Name__c;
        qList.add(qli);
    }
    insert qList;
}

I already can get all data from orderLineItem, but how can match sku code with product code from product? and productName from product ..... some thing like that  and then according these such as product2ID, pricebookEntryID to create Quote Line Item?   thank you so much:)
pconpcon
This should do what you want.  I built up a map of SKU -> product2 and then built up a map of Product2Id -> PricebookEntry so you can use them when builing your QuoteLineItem
 
trigger CreateQuoteLineItemFromQuote on Quote (after insert) {
    List<QuoteLineItem> qList = new List<QuoteLineItem>();

    Set<Id> ofwIdList = new Set<Id>();

    for (Quote q : Trigger.new) {
        ofwIdList.add(q.QuoteOfw__c);
    }

    Map<Id, OrderLineItem__c> ofwIdoliMap = new Map<Id, OrderLineItem__c>();

    for (OrderFromWeb__c ofw : [
        select ofwOrderLineItem__c,
            ofwOrderLineItem__r.Price__c,
            ofwOrderLineItem__r.ProductionName__c,
            ofwOrderLineItem__r.Quantity__c,
            ofwOrderLineItem__r.Total_Inc_tax__c,
            ofwOrderLineItem__r.SKU__c
        from OrderFromWeb__c
        where id in :ofwIdList
    ]) {
        ofwIdoliMap.put(ofw.Id, ofw.ofwOrderLineItem__r);
    }  

    Map<Id, OrderLineItem__c> oliMap = new Map<Id, OrderLineItem__c>();
    for (OrderLineItem__c oli: [
        select SKU__c,
            olitoquote__c
        from OrderLineItem__c
        where olitoquote__c in :Trigger.newMap.keySet()
    ]) {
        oliMap.put(oli.olitoquote__c ,oli);
    }  

    Set<String> skuSet = new Set<String>();

    for (OrderLineItem__c oli : [
        select sku__c
        from OrderLineItem__c
        where id in :ofwIdList
    ]) {
        skuSet.add(oli.sku__c);
    }  

    Map<String, Product2> productMap = new Map<Id, Product2>();
    Set<Id> productIds = new Set<Id>();

    for (Product2 product : [
        select ProductCode
        from Product2
        where ProductCode in :skuSet
    ]) {
        productMap.put(product.ProductCode, product);
        productIds.add(product.Id);
    }  

    Map<Id, PricebookEntry> pricebookMap = new Map<Id, PricebookEntry>();

    for (PricebookEntry pbe : [
        select Product2Id
        from PricebookEntry
        where Product2Id in :productIds
    ]) {
        pricebookMap.put(pbe.Product2Id, pbe);
    }  

    for (Quote q : Trigger.new) {
        OrderLineItem__c oli = ofwIdoliMap.get(q.QuoteOfw__c);
        Product2 product = productMap.get(oli.SKU__c);
        PricebookEntry pbe = pricebookMap.get(product.Id);

        qList.add(new QuoteLineItem(
            Product2Id = product.Id,
            PricebookEntryId = pbe.Id
        ));
    }

    insert qList;
}

NOTE: This code has not been tested and may contain typographical or logical errors.
LALALALALALA



@pcon

Hi, thank you for help.

I think I just need 'quntity' from OrderLineItem, So I add one code: qli.Quantity = Decimal.valueOf(ofwIdoliMap.get(q.QuoteOfw__c).Quantity__c);

but when I create order object it will show a problem:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CreateOppFromOrder caused an unexpected exception, contact your administrator: CreateOppFromOrder: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateQuoteFromOpp: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateQuoteLineItemFromQuote: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.CreateQuoteLineItemFromQuote: line 70, column 1: [] Trigger.CreateQuoteFromOpp: line 88, column 1: []: Trigger.CreateOppFromOrder: line 79, column 1 


So, what should I do next?

thank you, and have a nice weekend:)
LALALALALALA
@pcon

please help me to solved above last problems,,,thank you very much
pconpcon
Can you please include your updated code including CreateQuoteLineItemFromQuote, CreateOppFromOrder
 and CreateQuoteFromOpp.  Please use the "Add a code sample" button (icon <>) and make sure that the lines mentioned in the backtrace line up correctly.  If they do not line up correctly, please make sure you tell me what lines they are now in your newly pasted code.
 
LALALALALALA
@pcon

thank you so much:)
pconpcon
The problem is that on lie 70 your oli is null.  In otherwords you cannot find it by SKU
 
trigger CreateQuoteLineItemFromQuote on Quote (after insert) {
    List<QuoteLineItem> qList = new List<QuoteLineItem>();

    Set<Id> ofwIdList = new Set<Id>();

    for (Quote q : Trigger.new) {
        ofwIdList.add(q.QuoteOfw__c);
    }

    Map<Id, OrderLineItem__c> ofwIdoliMap = new Map<Id, OrderLineItem__c>();

    for (OrderFromWeb__c ofw : [
        select ofwOrderLineItem__c,
            ofwOrderLineItem__r.Price__c,
            ofwOrderLineItem__r.ProductionName__c,
            ofwOrderLineItem__r.Quantity__c,
            ofwOrderLineItem__r.Total_Inc_tax__c,
            ofwOrderLineItem__r.SKU__c
        from OrderFromWeb__c
        where id in :ofwIdList
    ]) {
        ofwIdoliMap.put(ofw.Id, ofw.ofwOrderLineItem__r);
    }

    Map<Id, OrderLineItem__c> oliMap = new Map<Id, OrderLineItem__c>();
    for (OrderLineItem__c oli: [
        select SKU__c,
            olitoquote__c
        from OrderLineItem__c
        where olitoquote__c in :Trigger.newMap.keySet()
    ]) {
        oliMap.put(oli.olitoquote__c ,oli);
    }

    Set<String> skuSet = new Set<String>();

    for (OrderLineItem__c oli : [
        select sku__c
        from OrderLineItem__c
        where id in :ofwIdList
    ]) {
        skuSet.add(oli.sku__c);
    }

    Map<String, Product2> productMap = new Map<string, Product2>();
    Set<Id> productIds = new Set<Id>();

    for (Product2 product : [
        select ProductCode
        from Product2
        where ProductCode in :skuSet
    ]) {
        productMap.put(product.ProductCode, product);
        productIds.add(product.Id);
    }

    Map<Id, PricebookEntry> pricebookMap = new Map<Id, PricebookEntry>();

    for (PricebookEntry pbe : [
        select Product2Id
        from PricebookEntry
        where Product2Id in :productIds
    ]) {
        pricebookMap.put(pbe.Product2Id, pbe);
    }


    for (Quote q : Trigger.new) {
        OrderLineItem__c oli = ofwIdoliMap.get(q.QuoteOfw__c);
        if (oli == null) {
            continue;
        }

        Product2 product = productMap.get(oli.SKU__c);
        if (product == null) {
            continue;
        }

        PricebookEntry pbe = pricebookMap.get(product.Id);
        if (pbe == null) {
            continue;
        }

        qList.add(new QuoteLineItem(
            Product2Id = product.Id,
            PricebookEntryId = pbe.Id,
            Quantity = Decimal.valueOf(oli.Quantity__c)
        ));
    }

    if (!qList.isEmpty()) {
        insert qList;
    }
}

By adding some null checks you'll make sure that you don't get this exception.
LALALALALALA
@pcon

Now I didn't get this exception,, but in QuoteLineItem still empty, didn't according my data of OrderLineItem to create Quotelineitem,,
olitoquote__c also is empty, do I need create a trigger for it?,,I dont know why. Thank you help alot,,,,,   only this problem, please please help me,,,I think you are the best one in developer community:)
LALALALALALA
@pcon
I find OrderLineItem__c oli = ofwIdoliMap.get(q.QuoteOfw__c);   in oli is null, so quoteLineItem cannot according to oli to create quotelineitem,
because only here if I delete :   If(oli==null){continue}      it will showing exception,  another two I delete this code didn't occur any problem
pconpcon
If the oli variable is null, that means that it is unable to find a OrderFromWeb__c that is set on your Quote object in your QuoteOfw__c field.  This could mean that your Quote does not have that field set.  Is the QuoteOfw__c field set manually, or is it populated in another trigger automatically

 
LALALALALALA
@pcon
Now I set a trigger to get QuoteOfw__c, and already get value,,,but OrderLineItem still empty,,,,,don't know why
LALALALALALA
@pcon

I find out, olitoquote__c is null too, this is lookup field in OrderLineItem, but only I create orders__c object, then quote created, then olitoquote__c can have value, because all value from quote,,,,so what can i do? can i set trigger for it? or I need change this lookup for them?
pconpcon
I'm afraid I don't know how to help you without spending more time than I am willing to learning your orgs object configuration and code configuration.