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 

trying to find id of master opp where detail product name and status are certain values so i can trigger new opp with products

i have custom opp as master (easy_opportunity__c) and custom product detail product__c
i want to list the products, when product name = easy one and product status = closed won and then i want to find the id of the master opp

i can then use this in boolean if statement to create a new opp

the complete class is below - where i am listing the product and adding to easyprodset then listing opp where id in easyprodset and a map of opplist i am trying to use containskey in the if statement

but its not working as i would expect a new opp

please help
 
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) {

          //try maps

/* list<product__c> easyprod = [select id, Easy_Opportunity__r.name, Easy_Opportunity__r.id, status__c, name from product__c  where Easy_Opportunity__r.name = 'Instruction Opportunity' and status__c = 'Closed Won' and name ='Easy One' ];       
          map<id, product__c> easyprodmap = new map<id, product__c>(easyprod);  */
          


    list<product__c> easyprod = [select id, Easy_Opportunity__r.name, Easy_Opportunity__r.id, status__c, name from product__c  where  status__c = 'Closed Won' and name ='Easy One' ];       
    /*  map<id, product__c> easyprodmap = new map<id, product__c>(easyprod);  */
          
          
            set<id> easyprodset = new set<id>();
          for(Product__c prods : easyprod) {
             
                 easyprodset.add(prods.Id);  
          }


         list<easy_opportunity__c> easyopps = [select id, name from easy_opportunity__c where id in :easyprodset]; 
              map<id, easy_opportunity__c> easyoppmap = new map<id, easy_opportunity__c>(easyopps);
          
       /*  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(   easyoppmap.containskey(oppty.id)    && (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;

          
          
 
    

  }
//newcode
      public static void easytwoupsells (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();
 
          
          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 Two' ];       
          set<id> easyprodset = new set<id>();
          for(Product__c prods : easyprod) 
           if(prods.easy_opportunity__r.name == 'Instruction Opportunity' && (prods.Status__c == 'Closed Won' && prods.name == 'Easy Two') ) 
                easyprodset.add(prods.Id);
          
         list<easy_opportunity__c> easyopps = [select id from easy_opportunity__c where id in :easyprodset]; 
        map<id, easy_opportunity__c> easyoppmap = new map<id, easy_opportunity__c>(easyopps);
          
          
          /*   set<id> easyoppset = new set<id>();
          for(easy_opportunity__c eopps : easyopps)
           easyoppset.add(eopps.id);   */

for(Easy_Opportunity__c oppty : opps){



 if(  easyoppmap.containskey(oppty.id)  &&  (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 Two 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 = 'Hosted Open House Viewings';
newpd.Sale_Price__c = 420.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 = 'Mortgage';
newpd2.Sale_Price__c = 0.00;  
newpd2.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd2.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd2); 
    

    
}

insert pdlist;

          
          
 
    

  }
//newcode
      public static void easythreeupsells (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();
         
 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 Three' ];       
          set<id> easyprodset = new set<id>();
          for(Product__c prods : easyprod) 
              if(prods.easy_opportunity__r.name == 'Instruction Opportunity' && (prods.Status__c == 'Closed Won' && prods.name == 'Easy Three') )  
              easyprodset.add(prods.Id);
          
     list<easy_opportunity__c> easyopps =     [select id from easy_opportunity__c where id in :easyprodset]; 
       map<id, easy_opportunity__c> easyoppmap = new map<id, easy_opportunity__c>(easyopps);
          
       /*   set<id> easyoppset = new set<id>();
          for(easy_opportunity__c eopps : easyopps)
           easyoppset.add(eopps.id);  */
          
          
         
for(Easy_Opportunity__c oppty : opps){



 if(  easyoppmap.containskey(oppty.id)  && (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 Three 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 = 'EPC';
newpd1.Sale_Price__c = 70.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 = 'Mortgage';
newpd2.Sale_Price__c = 0.00;  
newpd2.recordtypeid = product__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
newpd2.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd2); 
    

    
}

insert pdlist;

          
          
 
    

  }

    
 
//end
}