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
Jia Kang WonJia Kang Won 

Apex: Add Opportunity with product when an account and selected product is choose

User imput: at UI
Account Name and a prouct

Output:
An Opportunity with product selected
================================================
trigger AutoAddOpportunity on Account(after insert, after update) {
 
    List<Opportunity> oppList = new List<Opportunity>();
    
    // Get the related opportunities for the accounts in this trigger
    Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
        [SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
    
    // Add an opportunity for each account if it doesn't already have one.
    // Iterate through each account. 
    for(Account acc : Trigger.New) {
        
        System.debug('acctsWithOpps.get(acc.Id).Opportunities.size()=' + acctsWithOpps.get(acc.Id).Opportunities.size());
         
        // Check if the account already has a related opportunity.      
       if (acctsWithOpps.get(acc.Id).Opportunities.size() == 0) {
             
            // If it doesn't, add a default opportunity
            oppList.add(new Opportunity(Name=acc.Name + ' Opportunity',
                                       StageName='Prospecting',                 //set to Prospecting
                                       CloseDate=System.today().addMonths(1),   //set date to one month
                                       AccountId=acc.Id));
        }           
    }

   if (oppList.size() > 0) {
        insert oppList;
   }
}

==================================
trigger AutoAddProduct on Opportunity (before insert, after insert) {
    Pricebook2 standardBook = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
    if(Trigger.isBefore) {
        for(Opportunity record: Trigger.new) {
            record.Pricebook2Id = standardBook.Id;
        }
    }
    if(Trigger.isAfter) {
        OpportunityLineItem[] lines = new OpportunityLineItem[0];
        PricebookEntry entry = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardBook.Id AND Product2.ProductCode = 'DEFAULT'];   //set Product using code 
        for(Opportunity record: Trigger.new) {
            lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=record.Id, UnitPrice=entry.UnitPrice, Quantity=2)); //set Quantity
        }
        insert lines;
    }
}============================================
So far above is hard code the item, i want select item from user choose, any idea ?
 
Jia Kang WonJia Kang Won
Any idea ? urgent need help,appreciate the help.

trigger AutoAddProduct on Opportunity (before insert, after insert) {
    Pricebook2 standardBook = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
    if(Trigger.isBefore) {
        for(Opportunity record: Trigger.new) {
            record.Pricebook2Id = standardBook.Id;
        }
    }
    if(Trigger.isAfter) {
        OpportunityLineItem[] lines = new OpportunityLineItem[0];
        PricebookEntry entry = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardBook.Id AND Product2.ProductName = 'Name'];   ---> i want this auto trigger from user select product at Account. 
        for(Opportunity record: Trigger.new) {
            lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=record.Id, UnitPrice=entry.UnitPrice, Quantity=2)); //set Quantity
        }
        insert lines;
    }
}