You need to sign in to do that
Don't have an account?
Jim Norden
Auto-Add Product Too Specific Opportunity Record Type
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!
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!
SalesFORCE_enFORCEr
There is no need to write code for this. You can just create a process builder which will create an Opportunty Product record on create of opportunity