• Jia Kang Won
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
User imput a product at Account.
I want a trigger code that able auto add product to opportunity level. Any help.

mycode:
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 = :standardBook.Id];   //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;
    }
}
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 ?
 
I want a Apex code to add product from lookup list to my account.
Any idea??
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 ?
 
I want a Apex code to add product from lookup list to my account.
Any idea??