• Jim Norden
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi,

So we have two types of opportunities pages (Commerical and Government). We use many different types of Products (we call then Services) on the commerical side of things but we only use one type of Product on the Government side. What I am trying to do is have a "Government Programs" product automatically added to an opportunity if the Opportunity Record Type = "Government". Below is the code that I have come up with (primarly through Googling), it saves without any erros but it is not acutally adding the Product to the opportunity. Any help would be greatly apprecaited!!

trigger AutoCreateProduct on Opportunity (after insert) { 
    List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); 
    for (Opportunity newOpportunity: Trigger.New) { 
        if(newOpportunity.RecordTypeID== '0121a000000NDDv'){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='00k63000003fW9d',Quantity = 1)); 
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='00k63000003fW9d',Quantity = 1)); 
        }
    } 
    if(!OpportunityLineItems.isEmpty()){
        Database.SaveResult[] srList = Database.insert(OpportunityLineItems, false);

        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted Line Item. Oppty Prodcut ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Opportunity Product fields that affected this error: ' + err.getFields());
                }
            }
        }
    }
}

I have very limitied knowledge of Apex code so I am assuming I am just not headed in the right direction. Again any help would be greatly appreaicted! I have been struggling with this issue for a while. Thanks!
At our company we have 2 record types for Opportunty pages (Page A and Page B). What I am trying to do is that when a sales person is creating a new opportunity if they select the "Page B" Opportunity record type I want a specific Product (at our company we call them Services) to be automatically selected for that new opportunity.

I have tried to use a workflow but that does not work because it can't reference two different object (the Opportunity object and the Product object). I am assuming I will need to create a trigger for this but I pretty new to using triggers, I have tried for quite a while and haven't gotten anywhere close. Any help is greatly appreciated!
At our company we have 2 record types for Opportunty pages (Page A and Page B). What I am trying to do is that when a sales person is creating a new opportunity if they select the "Page B" Opportunity record type I want a specific Product (at our company we call them Services) to be automatically selected for that new opportunity.

I have tried to use a workflow but that does not work because it can't reference two different object (the Opportunity object and the Product object). I am assuming I will need to create a trigger for this but I pretty new to using triggers, I have tried for quite a while and haven't gotten anywhere close. Any help is greatly appreciated!