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 

trouble finding master records where the detail has a certain value

ive written some code to try to find custom opp (master) where custom product (detail) is closed won,and ad the set to an if condition to create a new opp with new products:
public without sharing class autoopps {
    
    
      public static void instructionopp (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();
Id recId2 = Schema.SObjectType.Easy_Opportunity__c.getRecordTypeInfosByName().get('Residential Sales Market Appraisal').getRecordTypeId();          

for(Easy_Opportunity__c oppty : opps){



if( (oldOppmap.get(oppty.id).Stage__c != 'Closed won') && oppty.stage__c == 'closed won'  &&  oppty.name == 'Market Appraisal' && oppty.recordtypeid == recId2 && oppty.active_contact__c == 'Yes'){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();

            newopp.ownerid = oppty.Allocated_Negotiator__c;
            newopp.name = 'Instruction Opportunity'; 
            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 = Date.today().addDays(7);
            

     opplist.add(newopp);
}
}

insert opplist; 



/* List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Mortgage","Sale_Price__c":"0.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd; */
 
 
list<Product__c> pdlist = new list <Product__c>();
  
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

newpd.name = 'Easy One';
newpd.Sale_Price__c = 475.00;  
newpd.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd);

Product__c newpd1 = new Product__c();

newpd1.name = 'Easy Two';
newpd1.Sale_Price__c = 825.00;  
newpd1.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd1.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd1);

Product__c newpd2 = new Product__c();

newpd2.name = 'Easy Three';
newpd2.Sale_Price__c = 1500.00;  
newpd2.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd2.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd2);    
    
}

insert pdlist;

          
          
 
    

  } 
 //newcode
 
      
      
      
  
 //newcode
      public static void easyoneupsells (list<Easy_Opportunity__c> opps, map<id, Easy_Opportunity__c> oldoppmap) {

          
          


    list<product__c> easyprod = [select easy_opportunity__r.name, status__c, name from product__c where easy_opportunity__r.name = 'Instruction Opportunity' and status__c = 'Closed Won' and name ='Easy One' ];       
          set<id> easyprodset = new set<id>();
          for(Product__c prods : easyprod) 
             
                 easyprodset.add(prods.Id);
          
         list<easy_opportunity__c> easyopps = [select id from easy_opportunity__c where id in :easyprodset]; 
                        
         set<id> easyoppset = new set<id>();
          for(easy_opportunity__c eopps : easyopps)
              
           easyoppset.add(eopps.id);        
          
          
          
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

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

          

 
          
          
    
   for(Easy_Opportunity__c oppty : opps){

	if(   (easyoppset.contains(oppty.id) == true)    && (oldOppmap.get(oppty.id).Stage__c != 'Closed won') &&  oppty.stage__c == 'closed won'  &&  oppty.name == 'Instruction Opportunity' && oppty.recordtypeid == recId && oppty.active_contact__c == 'Yes') { 

 
    
    


 Easy_Opportunity__c newopp = new Easy_Opportunity__c (); 

            newopp.ownerid = oppty.Allocated_Negotiator__c;
            newopp.name = '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 = Date.today().addDays(7);
            

     opplist.add(newopp);

}
           } 
insert opplist; 




 
 
list<Product__c> pdlist = new list <Product__c>();
  
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

newpd.name = 'Conveyancing';
newpd.Sale_Price__c = 599.00;  
newpd.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd);

Product__c newpd1 = new Product__c();

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

pdlist.add(newpd1);

Product__c newpd2 = new Product__c();

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

pdlist.add(newpd2);    

Product__c newpd3 = new Product__c();

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

pdlist.add(newpd3); 

Product__c newpd4 = new Product__c();

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

pdlist.add(newpd4); 
    
Product__c newpd5 = new Product__c();

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

pdlist.add(newpd5); 
    
}

insert pdlist;

          
          
 
    

  }
}

the code that isnt working is:
list<product__c> easyprod = [select easy_opportunity__r.name, status__c, name from product__c where easy_opportunity__r.name = 'Instruction Opportunity' and status__c = 'Closed Won' and name ='Easy One' ];       
          set<id> easyprodset = new set<id>();
          for(Product__c prods : easyprod) 
             
                 easyprodset.add(prods.Id);
          
         list<easy_opportunity__c> easyopps = [select id from easy_opportunity__c where id in :easyprodset]; 
                        
         set<id> easyoppset = new set<id>();
          for(easy_opportunity__c eopps : easyopps)
              
           easyoppset.add(eopps.id);
notice the if statement in method easyoneupsells isnt working of the filtered products

please help
James LoghryJames Loghry
If you're talking about line 124 in your example, you're originally generating a list of ids of easy_opportunity__c records, but your contains check in line 124 is checking if the set contains opportunity ids.  You'll need to refactor your code to check either the correct opportunity or easy_opportunity__c id depending in your requirement.
Salesforce Admin 110Salesforce Admin 110
the code to find master opp with  detail product.status = closed won and product.name=instruction opp looks good i dont see how to refactor it?