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
Salesforce Admin 110Salesforce Admin 110 

trigger new custom opportunity record from the value of a product record (detail)

hi all

i have set up custom opp and custom product objects in master-detail relationship.  opp master, product detail.  i have multiple products per opp.  i have a status field with values closedwon/closedlost for each product.  i want to trigger a new opp record when i close win a product.

so far i hae written the following code below

i think i have built a list of products related to opps but now i need to loop through that list where the product is closed won and then create a new opportunity
public static void easyoneupsells (list<Easy_Opportunity__c> opps, map<id, Easy_Opportunity__c> oldoppmap) {

list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

Id recId = Schema.SObjectType.Easy_Opportunity__c.getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();

easy_opportunity__c[] easyopps = [select id, name from easy_opportunity__c where id in :opps];
                set<id> easyoppsset = new set<id>();
        for(easy_opportunity__c eopps : easyopps) 
            if( == 'Instruction Opportunity') 

        list<product__c> prods = [select id, status__c, recordtypeid, name from product__c where easy_opportunity__c in: 
        easyoppsset and  recordtypeid = :recId ];

//need help at this point

for(Easy_Opportunity__c oppty : opps){

if( (oldOppmap.get( != 'Closed won') && oppty.stage__c == 'closed won'  && == 'Instruction Opportunity' && oppty.recordtypeid == recId && oppty.active_contact__c == 'Yes'){


Easy_Opportunity__c newopp = new Easy_Opportunity__c ();

            newopp.ownerid = oppty.Allocated_Negotiator__c;
   = 'Easy One Upsells'; 
            newopp.Account_Name__c = oppty.Account_Name__c;
            newopp.CurrencyIsoCode = oppty.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = recId;
            newopp.Contact_Name__c = oppty.Contact_Name__c;
            newopp.Close_Date__c =;


insert opplist; 

list<Product__c> pdlist = new list <Product__c>();
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c(); = 'Conveyancing';
newpd.Sale_Price__c = 599.00;  
newpd.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd.Easy_Opportunity__c =;


Product__c newpd1 = new Product__c(); = 'Premium Listing';
newpd1.Sale_Price__c = 140.00;  
newpd1.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd1.Easy_Opportunity__c =;


Product__c newpd2 = new Product__c(); = 'EPC';
newpd2.Sale_Price__c = 70.00;  
newpd2.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd2.Easy_Opportunity__c =;


Product__c newpd3 = new Product__c(); = 'Block Viewings';
newpd3.Sale_Price__c = 79.00;  
newpd3.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd3.Easy_Opportunity__c =;


Product__c newpd4 = new Product__c(); = 'Mortgage';
newpd4.Sale_Price__c = 0.00;  
newpd4.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd4.Easy_Opportunity__c =;

Product__c newpd5 = new Product__c(); = 'Sales Progression';
newpd5.Sale_Price__c = 470.00;  
newpd5.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd5.Easy_Opportunity__c =;


insert pdlist;



Mahesh DMahesh D
Please explain more about your requirement, I didn't understand your code as you are creating the Easy_Opportunity and Product records.

Also paste the Trigger and Class here.