+ Start a Discussion

Automatically create quote and sync it after opportunity and product lines creation


I'm trying to automatically create quote and sync it after opportunity and product lines creation.

Here is my code :
Pricebook2 pb = [select Id from Pricebook2 where Name = 'Magasins EuroCave - prix publics'];
        Opportunity opp = new Opportunity(
            Name = 'Test',
            CloseDate = System.Today(),
            StageName = 'Prospecting',
            AccountId = accountId
        insert opp;
        Map<Id, PriceBookEntry> pbeMap = new Map<Id, PriceBookEntry>();
        for(PriceBookEntry pbe : [Select Id, Product2Id, UnitPrice From PriceBookEntry Where Product2Id IN: produitsCommandeIds And Pricebook2Id =: pb.Id])
            pbeMap.put(pbe.Product2Id, pbe);
        List<OpportunityLineItem> oppLis = new List<OpportunityLineItem>();
        for(ProduitStockMutualiseWrapper pc : produitsCommande)
            OpportunityLineItem oppLineItem = new OpportunityLineItem(
                OpportunityId = opp.Id,
                PricebookEntryId = pbeMap.get(pc.produit.Id).Id,
                Quantity = pc.quantiteCommande,
                Discount = pc.remise,
                UnitPrice = pbeMap.get(pc.produit.Id).UnitPrice,
                Date_prochain_camion__c = pc.dateProchainCamion
        insert oppLis;
        Quote qte = new Quote(
            Name = 'Devis-' + opp.Name,
            OpportunityId = opp.Id
        insert qte;
        opp.SyncedQuoteId = qte.Id;
        update opp;
Everything is fine until I try to snyc the quote, the quote is created and sync but the product lines aren't created :(
If I remove the sync the product lines are correctly created.

How can I keep both the sync and the product line creation ?

Thanks for your help.
Is it Read-Only on your specific profile? Please check the ​Field Accessibility of Opportunity object