+ Start a Discussion
Jim NordenJim Norden 

Update a product/service if an certain Opportunity record type is selected

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!
swati_sehrawatswati_sehrawat
When you say automatically selected it should be visible under related list once you save that opportunity?
Jim NordenJim Norden
That is correct. So when I am creating an opportunity and I determine that it is going to fall under Page B type work - once I select Page B the Product/Serivce will automatically be assigned to that opportunity and is visible under related list once I save that opportunity.
swati_sehrawatswati_sehrawat
Hello Jim,

You cannot achieve this requirement using a workflow and need to extend it to a trigger on opportunity to save product, once you save the opportunity. Can you share if you have written any trigger and we can modify it to help you.
Jim NordenJim Norden
I really appreciate your help, I am very new to trigger and apex so I am having some trouble on even where to start... I have taken a few stabs at it but I do not think I am even formatting the trigger properly. Any help with even starting the trigger woudl be greatly appreciated. Thank you!
swati_sehrawatswati_sehrawat
Your Trigger should start as: 
trigger oppTrigger on Opportunity (before insert) {
	string recordTypeName = 'Name of your recordType'
	Id recordTypeId = Schema.SObjectType.Service_request__c.getRecordTypeInfosByName().get(recordTypeName).getRecordTypeId();
	for(Opportunity obj : Trigger.New){
		if(obj.RecordTypeId == recordTypeName){
			
		}
	}
}

Also standard products cannot be inserted directly under Opportunities they are used as OpportunityLineItems, is your Product object a custom object.