• Salesforce Admin 110
  • NEWBIE
  • 60 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 46
    Questions
  • 41
    Replies
problem with code
i need to refactor this code:
error = Initial term of field expression must be a concrete SObject: List<pba__Listing__c&gt line 31
public without sharing class copylistingpricetproperty {

    public static void resisales(List<pba__Property__c> propertyIds){
        Id recId = Schema.SObjectType.pba__Property__c.getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
        
        

   /*    list<pba__Property__c> properties = [select id, name, published__C from pba__Property__c where id in :propertyIds and published__c = true];
                set<id> propertiesset = new set<id>();
        
       
        for(pba__Property__c prop : properties) 
            
                propertiesset.add(prop.Id);    */  

     
        list<pba__Listing__c> listings = [select id, pba__ListingPrice_pb__c,pba__Status__c,pba__Property__c,  recordtypeid, name from pba__Listing__c where /* id in :propertiesset  and */	recordtypeid = :recId and pba__Status__c = 'Approved']; 
        
        set<id>listset = new set<id>();
        for(pba__Listing__c lists : listings)
            listset.add(lists.pba__Property__c);
        
          
      
        List<pba__Property__c> propstoUpdate = new List<pba__Property__c>(); 
        
     for(pba__Property__c propp : propertyIds)
         if( listset.contains(propp.id)) 
      
     
         listings.pba__ListingPrice_pb__c = propp.Asking_price__c;
         
     
        update propsToUpdate;
        
    
        
        
        
  
        
        
    }
  
}

 
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
}

 
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
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(eopps.name == 'Instruction Opportunity') 
                easyoppsset.add(eopps.Id);

     
        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(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;

          
          
 
    

  }

 
i want to build a search page in visualforce and am able search for country and city and populate a list of matches but i am not sure how serch for currency values that fall within a range as set in 2 input fields, can you please help?
3 opps in 1 account if close win 1 opp how do i close lose other 2 opps?
i am trying to tidy my code so that i reduce the number of soqls here is a sample
public  class updatedeletopps {

    public static void updatedelopps(List<Contact> contactIds){
        Id recId = Schema.SObjectType.Easy_Opportunity__c.getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
        
        
//n1        
//
        contact[] activecontacts = [select id, active_contact__c from contact where id in :contactIds];
        set<id> activecontactsset = new set<id>();
        for(contact con : activecontacts) 
            if(con.active_contact__c == 'No') 
            	activecontactsset.add(con.Id);

     List<easy_opportunity__c> oppsToUpdate1 = new List<easy_opportunity__c>();
        list<easy_opportunity__c> opptys = [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 
		activecontactsset and name = 'Market Appraisal' and stage__c = 'Closed Lost' and recordtypeid = :recId];
     for(easy_opportunity__c opp1 : opptys)
     { 
         opp1.Stage__c='Closed Lost';
         opp1.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp1.Opportunity_Rating__c = 'Cold';
         oppsToUpdate1.add(opp1);
     }

     update oppsToUpdate1;
        
    list<easy_opportunity__c> oppstodelete2 = new list<easy_opportunity__c>();
        list<easy_opportunity__c> opptys1 = [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:activecontactsset and (name =  'Lost MA Upsell - Gas Safety Check' or name = 'Lost MA Upsell - Home Buyer Report' or name = 'Lost MA Upsell - Conveyancing' or name = 'Lost MA Upsell - Mortgage') ];
        for(easy_opportunity__c opp2 : opptys1)
        {
        oppstodelete2.add(opp2);
        }
        delete oppstodelete2;
here i have trigger how do i convert to class:
 
trigger updatedeleteopps3 on Contact (after update) {
Set<Id> contactIds = new Set<Id>();

 
    
//n1

    for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }




     List<easy_opportunity__c> oppsToUpdate1 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp1 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Market Appraisal' and stage__c = 'Closed Lost' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp1.Stage__c='Closed Lost';
      opp1.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp1.Opportunity_Rating__c = 'Cold';
          oppsToUpdate1.add(opp1);
     }

     update oppsToUpdate1;

 list<easy_opportunity__c> oppstodelete2 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp2 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and (name =  'Lost MA Upsell - Gas Safety Check' or name = 'Lost MA Upsell - Home Buyer Report' or name = 'Lost MA Upsell - Conveyancing' or name = 'Lost MA Upsell - Mortgage') ])
        {
        oppstodelete2.add(opp2);
        }
        delete oppstodelete2;


//n2

 for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate3 = new List<easy_opportunity__c>();

    for(easy_opportunity__c opp3 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Market Appraisal' and stage__c = 'Closed Won' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp3.Stage__c='Closed Won';
      
         opp3.Opportunity_Rating__c = 'Warm';
          oppsToUpdate3.add(opp3);
     }

     update oppsToUpdate3; 

 list<easy_opportunity__c> oppstoupdate4 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp4 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and name = 'Instruction Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
        {
            opp4.Stage__c='Closed Lost';
      opp4.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp4.Opportunity_Rating__c = 'Cold';
        oppstoupdate4.add(opp4);
        }
        update oppstoupdate4;
    
    
    //n3
 for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate5 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp5 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Instruction Opportunity' and stage__c = 'Closed Lost' and recordtypeid in (select id from 

recordtype where name = 'Residential Sales')])
     { 
          opp5.Stage__c='Closed Lost';
      opp5.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp5.Opportunity_Rating__c = 'Cold';
          oppsToUpdate5.add(opp5);
     }

     update oppsToUpdate5;

list<easy_opportunity__c> oppstodelete6 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp6 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and (name = 'Lost Instruction Upsell - Gas Safety Check' or name = 'Lost Instruction Upsell - Home Buyer Report' or name = 'Lost Instruction Upsell - Conveyancing' or name = 'Lost Instruction Upsell - Mortgage')])
        {
        oppstodelete6.add(opp6);
        }
        delete oppstodelete6;
        
//n4

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate7 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp7 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Instruction Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select id from 

recordtype where name = 'Residential Sales')])
     { 
          opp7.Stage__c='Closed Lost';
      opp7.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp7.Opportunity_Rating__c = 'Cold';
          oppsToUpdate7.add(opp7);
     }

     update oppsToUpdate7;

list<easy_opportunity__c> oppstodelete8 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp8 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and (name = 'Bundle 1 Opportunity' or name = 'Bundle 2 Opportunity' or name = 'Bundle 3 Opportunity') ])
        {
        oppstodelete8.add(opp8);
        }
        delete oppstodelete8;


//n5






for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate9 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp9 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Bundle 1 Opportunity' and stage__c = 'Closed Lost' and recordtypeid in (select id from 

recordtype where name = 'Residential Sales')])
     { 
          opp9.Stage__c='Closed Lost';
      opp9.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp9.Opportunity_Rating__c = 'Cold';
          oppsToUpdate9.add(opp9);
     }

     update oppsToUpdate9;

//n6

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate10 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp10 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Bundle 2 Opportunity' and stage__c = 'Closed Lost' and recordtypeid in (select id from 

recordtype where name = 'Residential Sales')])
     { 
          opp10.Stage__c='Closed Lost';
      opp10.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp10.Opportunity_Rating__c = 'Cold';
          oppsToUpdate10.add(opp10);
     }

     update oppsToUpdate10;

//n7

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate11 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp11 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Bundle 3 Opportunity' and stage__c = 'Closed Lost' and recordtypeid in (select id from 

recordtype where name = 'Residential Sales')])
     { 
          opp11.Stage__c='Closed Lost';
      opp11.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp11.Opportunity_Rating__c = 'Cold';
          oppsToUpdate11.add(opp11);
     }

     update oppsToUpdate11;


//n8

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate12 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp12 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Bundle 1 Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp12.Stage__c='Closed Lost';
      opp12.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp12.Opportunity_Rating__c = 'Cold';
          oppsToUpdate12.add(opp12);
     }

     update oppsToUpdate12;

list<easy_opportunity__c> oppstodelete13 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp13 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and (name = 'Bundle 1 Upsell Opportunity - Conveyancing' or name = 'Bundle 1 Upsell Opportunity - Premium Listing' or name = 'Bundle 1 Upsell Opportunity - EPC' or name = 'Bundle 1 Upsell Opportunity - Individual Viewings' or name = 'Bundle 1 Upsell Opportunity - Mortgage' or name = 'Bundle 1 Upsell Opportunity - Sales Progression') ])
        {
        oppstodelete13.add(opp13);
        }
        delete oppstodelete13;

//n9

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate14 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp14 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Bundle 2 Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp14.Stage__c='Closed Lost';
      opp14.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp14.Opportunity_Rating__c = 'Cold';
          oppsToUpdate14.add(opp14);
     }

     update oppsToUpdate14;

list<easy_opportunity__c> oppstodelete15 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp15 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and (name = 'Bundle 2 Upsell Opportunity - Hosted Open House Viewings' or name = 'Bundle 2 Upsell Opportunity - Premium Listing' or name = 'Bundle 2 Upsell Opportunity - Mortgage' )])
        {
        oppstodelete15.add(opp15);
        }
        delete oppstodelete15;

//n10

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate16 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp16 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Bundle 3 Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp16.Stage__c='Closed Lost';
      opp16.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp16.Opportunity_Rating__c = 'Cold';
          oppsToUpdate16.add(opp16);
     }

     update oppsToUpdate16;

list<easy_opportunity__c> oppstodelete17 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp17 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and (name = 'Bundle 3 Upsell Opportunity - Conveyancing' or name = 'Bundle 3 Upsell Opportunity - EPC' or name = 'Bundle 3 Upsell Opportunity - Mortgage') ])
        {
        oppstodelete17.add(opp17);
        }
        delete oppstodelete17;

//n11

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate18 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp18 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Conveyancy Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select id 

from recordtype where name = 'Residential Sales')])
     { 
          opp18.Stage__c='Closed Lost';
      opp18.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp18.Opportunity_Rating__c = 'Cold';
          oppsToUpdate18.add(opp18);
     }

     update oppsToUpdate18;

//n12
for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate19 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp19 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Mortgage Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select id from 

recordtype where name = 'Residential Sales')])
     { 
          opp19.Stage__c='Closed Lost';
      opp19.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp19.Opportunity_Rating__c = 'Cold';
          oppsToUpdate19.add(opp19);
     }

     update oppsToUpdate19;



//n13
for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate20 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp20 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Home Buyer Report Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select 

id from recordtype where name = 'Residential Sales')])
     { 
          opp20.Stage__c='Closed Lost';
      opp20.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp20.Opportunity_Rating__c = 'Cold';
          oppsToUpdate20.add(opp20);
     }

     update oppsToUpdate20;

//n14
for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }

     List<easy_opportunity__c> oppsToUpdate21 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp21 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Gas Safety Check Opportunity' and stage__c = 'Closed Won' and recordtypeid in (select 

id from recordtype where name = 'Residential Sales')])
     { 
          opp21.Stage__c='Closed Lost';
      opp21.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp21.Opportunity_Rating__c = 'Cold';
          oppsToUpdate21.add(opp21);
     }

     update oppsToUpdate21;
//n15   
  for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }




     List<easy_opportunity__c> oppsToUpdate22 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp22 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Market Appraisal' and stage__c = 'New' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp22.Stage__c='Closed Lost';
      opp22.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp22.Opportunity_Rating__c = 'Cold';
          oppsToUpdate22.add(opp22);
     }

     update oppsToUpdate22;
    
    list<easy_opportunity__c> oppstodelete27 = new list<easy_opportunity__c>();
        
        for(easy_opportunity__c opp27 : [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:contactids and name like '%Lost MA Upsell%' ])
        {
        oppstodelete27.add(opp27);
        }
        delete oppstodelete27;
    

   //n16
  
for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }




     List<easy_opportunity__c> oppsToUpdate23 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp23 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Conveyancy Opportunity' and stage__c = 'New' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp23.Stage__c='Closed Lost';
      opp23.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp23.Opportunity_Rating__c = 'Cold';
          oppsToUpdate23.add(opp23);
     }

     update oppsToUpdate23;

//17

for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }




     List<easy_opportunity__c> oppsToUpdate24 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp24 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Mortgage Opportunity' and stage__c = 'New' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp24.Stage__c='Closed Lost';
      opp24.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp24.Opportunity_Rating__c = 'Cold';
          oppsToUpdate24.add(opp24);
     }

     update oppsToUpdate24;
    
  //n18  
    
 for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }




     List<easy_opportunity__c> oppsToUpdate25 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp25 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Home Buyer Report Opportunity' and stage__c = 'New' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp25.Stage__c='Closed Lost';
      opp25.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp25.Opportunity_Rating__c = 'Cold';
          oppsToUpdate25.add(opp25);
     }

     update oppsToUpdate25;   
    
    //n19
    
    for(contact con : Trigger.new)
    {
         if(con.active_contact__c == 'No')
              contactIds.add(con.Id);
    }




     List<easy_opportunity__c> oppsToUpdate26 = new List<easy_opportunity__c>();

     for(easy_opportunity__c opp26 : [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 

contactIds and name = 'Applicant Gas Safety Check Opportunity' and stage__c = 'New' and recordtypeid in (select id from recordtype 

where name = 'Residential Sales')])
     { 
          opp26.Stage__c='Closed Lost';
      opp26.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp26.Opportunity_Rating__c = 'Cold';
          oppsToUpdate26.add(opp26);
     }

     update oppsToUpdate26;  
    
    
    
    
    
    
    
    
       
    }

 
i need to convert this trigger into class methos so i can call from trigger, i have only done this a couple of times so need some help
trigger oppclosedate on Easy_Opportunity__c (before update) {

 


    
    
for (easy_opportunity__c opp : trigger.new)
    
    
    if (opp.Stage__c == 'Closed Won' || opp.Stage__c == 'Closed Lost')
        {
          opp.Close_Date__c= system.today();
         }
    
    
    
    
    
}

 
hi i have some code that creates opp on contact when field value is added  but checks the opps exist on every subsequent afterupdate on the contact and doesn't create duplicate opps

so my question is if these opps are eventually closed and the same contact returns for more of our services i would want to create the same opps

how wouqld i do this??

heres my code:
public static void sellercon (list<Contact> cons) {
    //seller resi
    
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

 /* recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales']; */
        
     /*   Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
Schema.RecordTypeInfo rtByName =  rtMapByName.get('Residential Sales');  */

  

 Contact[] allBuyers = [Select id, contacttype__c From Contact where Id In : cons ];
 set<id> allBuyersSet = new set<id>();
 for (Contact buyer : allBuyers)
     if ((buyer.contacttype__c != null) && buyer.contacttype__c.contains('Seller') && buyer.contacttype__c.contains ('Residential') )
        allBuyersSet.add(buyer.id);
    
    
set<id> fltrCons = new set<id>(); 
for (Easy_Opportunity__c easyopp : [Select Contact_Name__c From Easy_Opportunity__c where Contact_Name__c  In :allBuyersSet and name = 'Market Appraisal'])
 fltrCons.add(easyopp.Contact_Name__c);
    
for(Contact con : cons){



    if( con.Active_Contact__c == 'Yes' &&  (con.contacttype__c != null) && fltrCons.contains(con.id) == false   && con.contacttype__c.contains('Seller') && con.contacttype__c.contains('Residential') ) {
/*con.Seller__c == true && con.Residential__c == true && con.Buyer__c == false && con.Commercial__c == false && con.Tenant__c == false && con.Landlord__c == false*/

            
        
        Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  easy_opportunity__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(1);
        
            
            

           opplist.add(newopp);


 }
    

 }
//outsideforloop
 //
  if(oppList.size() > 0){
    insert opplist; 
  
list<Product__c> pdlist = new list <Product__c>();
/* recordtype[] aa = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Product__c' and Name = 'Residential Sales']; */
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

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

pdlist.add(newpd);


}

insert pdlist;

          
          
 
    

  } 
    
 

}

 
hi ive written a visualforce page to showmap from address fields but i want to do this from latitude and longitude fields
 
<apex:page standardController="Account">

<head>

<script type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyBmZQov1SBI9a3f9nWPwCS_cy37nPZIm9I&sensor=false"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 

$(document).ready(function() {

  var myOptions = {
    zoom: 18,
    mapTypeId: google.maps.MapTypeId.SATELLITE,
    mapTypeControl: false
  }

  var map;
  var marker;

  var geocoder = new google.maps.Geocoder();   
  /* var address = "{!Account.BillingStreet}, " + "{!Account.BillingCity}, " + "{!Account.BillingPostalCode}, " + "{!Account.BillingCountry}"; */
 /*  var latlng = ("{!Account.Latitude__c}",  "{!Account.Longitude__c}"); */
   /* var address = "{!Account.Longitude__c}, " + "{!Account.Latitude__c}; */
    var address = "{!Account.Address__c}, " + "{!Account.city__c}, " + "{!Account.postal_code__c}, " + "{!Account.country__c}";  
/* var lat = '{!Account.Latitude__c}';
var lng = '{!Account.Longitude__c}';
var latlng = new google.maps.LatLng(lat, lng); */


  var infowindow = new google.maps.InfoWindow({
    content: "<b>{!Account.Name}</b><br>{!Account.BillingStreet}<br>{!Account.BillingCity}, {!Account.BillingPostalCode}<br>{!Account.BillingCountry}"
  });  

   geocoder.geocode( { address: address}, function(results, status) { 
 /*  geocoder.geocode( { latlng: latlng}, function(results, status) {  */
    if (status == google.maps.GeocoderStatus.OK && results.length) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {

        //create map
        map = new google.maps.Map(document.getElementById("map"), myOptions);

        //center map
        map.setCenter(results[0].geometry.location);

        //create marker
        marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map,
            title: "{!Account.Name}"
        });

        //add listeners
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        });
        google.maps.event.addListener(infowindow, 'closeclick', function() {
          map.setCenter(marker.getPosition()); 
        });

      }

    } else {
      $('#map').css({'height' : '25px'});
      $('#map').html("Oops! {!Account.Name}'s billing address could not be found, please make sure the address is correct.");
      resizeIframe();
    }
  });

  function resizeIframe() {
    var me = window.name;
    if (me) {
      var iframes = parent.document.getElementsByName(me);
      if (iframes && iframes.length == 1) {
        height = document.body.offsetHeight;
        iframes[0].style.height = height + "px";
      }
    }
  }

});
</script>

<style>
#map {
  font-family: Arial;
  font-size:12px;
  line-height:normal !important;
  height:250px;
  background:transparent;
}
</style>

</head>

<body>
<div id="map"></div> 
</body> 
</apex:page>

 
need some help with unit test which gives mee 100% on trigger but only 34% on class
 
trigger iainb on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete) {

if(trigger.isupdate && trigger.isafter){


iain.sellercon (Trigger.new);
iain.sellercon2 (Trigger.new);

}   
}

public with sharing class iain {

    public static void sellercon (list<Contact> cons) {
    //seller resi
    
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

  

 Contact[] allBuyers = [Select id, contacttype__c From Contact where Id In : cons ];
 set<id> allBuyersSet = new set<id>();
 for (Contact buyer : allBuyers)
  if (buyer.contacttype__c.contains('Residential') && buyer.contacttype__c.contains('Seller'))
   allBuyersSet.add(buyer.id);   
    
    
set<id> fltrCons = new set<id>(); 
for (Easy_Opportunity__c easyopp : [Select Contact_Name__c From Easy_Opportunity__c where Contact_Name__c  In :allBuyersSet and name = 'Market Appraisal'])
 fltrCons.add(easyopp.Contact_Name__c);
    
for(Contact con : cons){



    if( fltrCons.contains(con.id) == false && con.Stage__c == 'Lead'     && con.contacttype__c.contains('Residential') && con.contacttype__c.contains('Seller')) {
/*con.Seller__c == true && con.Residential__c == true && con.Buyer__c == false && con.Commercial__c == false && con.Tenant__c == false && con.Landlord__c == false*/


        
        Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
        
            
            

            opplist.add(newopp);

 
    
   
 }
    
 }
 //outsideforloop  
  //
  if(oppList.size() > 0) {
      system.debug('###opplist' +opplist);
      
    insert opplist; 
  
list<Product__c> pdlist = new list <Product__c>();
recordtype[] aa = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype 

='Product__c' and Name = 'Residential Sales'];
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

newpd.name = 'Market Appraisal';
newpd.Sale_Price__c = 0.00;
newpd.recordtypeid = aa[0].id;
newpd.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd);


}

insert pdlist;

          
          
 
    

  } 
    

}
 //seller resi buyer
    public static void sellercon2 (list<Contact> cons) {
    //seller resi
    
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

  

 Contact[] allBuyers = [Select id, contacttype__c From Contact where Id In : cons ];
 set<id> allBuyersSet = new set<id>();
 for (Contact buyer : allBuyers)
  if (buyer.contacttype__c.contains('Residential') && buyer.contacttype__c.contains('Seller') && buyer.contacttype__c.contains('Buyer'))
   allBuyersSet.add(buyer.id);   
    
    
set<id> fltrCons = new set<id>(); 
for (Easy_Opportunity__c easyopp : [Select Contact_Name__c From Easy_Opportunity__c where Contact_Name__c  In :allBuyersSet and name = 'Market Appraisal'])
 fltrCons.add(easyopp.Contact_Name__c);
    
for(Contact con : cons){



    if( fltrCons.contains(con.id) == false && con.Stage__c == 'Lead'     && con.contacttype__c.contains('Residential') && con.contacttype__c.contains('Seller') && con.contacttype__c.contains('Buyer')) {
/*con.Seller__c == true && con.Residential__c == true && con.Buyer__c == false && con.Commercial__c == false && con.Tenant__c == false && con.Landlord__c == false*/


        
        Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
        
            
            

            opplist.add(newopp);

 
    
   
 }
    
 }
 //outsideforloop  
  //
  if(oppList.size() > 0) {
      system.debug('###opplist' +opplist);
      
    insert opplist; 
  
list<Product__c> pdlist = new list <Product__c>();
recordtype[] aa = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype 

='Product__c' and Name = 'Residential Sales'];
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

newpd.name = 'Market Appraisal';
newpd.Sale_Price__c = 0.00;
newpd.recordtypeid = aa[0].id;
newpd.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd);


}

insert pdlist;

          
          
 
    

  } 
    

}

}

@istest
public class iaintest {

    static testmethod void test_trigger(){
        
       account acct = new account(Name='test'); 
        
        insert acct;
        
        contact ct = new contact(firstname='iain', lastname='banks');
        
        insert ct;
        Test.StartTest(); 
        
        ct.Stage__c = 'Lead';
        ct.ContactType__c ='Seller';
        ct.ContactType__c ='Buyer';   
        ct.ContactType__c ='Residential';   
            
                update ct;
        
        
        Test.StopTest();
        
        
    }
  
}


 
i have tried a couple of things but no success........need to stop the duplicate opps after updates.....pulling my hair out

 
trigger easycontacttrigger on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete){
  
if(Trigger.isUpdate  && Trigger.isAfter ){
    
    
     
    
easycontacttriggerhelper.sellercon (Trigger.new);
    
    
easycontacttriggerhelper.buyerconConveyancy(Trigger.new);
    
easycontacttriggerhelper.buyerconMortgage(Trigger.new);
easycontacttriggerhelper.buyerconhomebuyerreport(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchgas(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchwater(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchelec(Trigger.new);
easycontacttriggerhelper.buyerconremovals(Trigger.new);
easycontacttriggerhelper.buyercongassafetycheck(Trigger.new);
easycontacttriggerhelper.buyerconpattest(Trigger.new);
easycontacttriggerhelper.buyerconaffiliates(Trigger.new);

    
    


       
}  

if(Trigger.isinsert && Trigger.isafter){
    
easycontacttriggerhelper.sellercon2 (Trigger.new);
easycontacttriggerhelper.buyerconConveyancy2(Trigger.new);
easycontacttriggerhelper.buyerconMortgage2(Trigger.new);
easycontacttriggerhelper.buyerconhomebuyerreport2(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchgas2(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchwater2(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchelec2(Trigger.new);
easycontacttriggerhelper.buyerconremovals2(Trigger.new);
easycontacttriggerhelper.buyercongassafetycheck2(Trigger.new);
easycontacttriggerhelper.buyerconpattest2(Trigger.new);
easycontacttriggerhelper.buyerconaffiliates2(Trigger.new);








}

    if(Trigger.isinsert && Trigger.isbefore){
        
easycontacttriggerhelper.contactuserfields(Trigger.new);        
        
    }
if(Trigger.isupdate && Trigger.isbefore){
        
easycontacttriggerhelper.contactuserfields(Trigger.new);        
        
    }

    
}


 
public with sharing class easycontacttriggerhelper {

public static void sellercon (list<Contact> cons) {
    
    
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

  
    
for(Contact con : cons){



    if(  con.Stage__c == 'Lead' && con.contacttype__c != null    && con.contacttype__C.containsany('Seller Residential') ) {


        
        Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
    		
            
            

            opplist.add(newopp);

 
    
   
 }
 }
 //outsideforloop  
  //
  if(oppList.size() > 0){
    insert opplist; 
  
list<Product__c> pdlist = new list <Product__c>();
recordtype[] aa = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype 

='Product__c' and Name = 'Residential Sales'];
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

newpd.name = 'Market Appraisal';
newpd.Sale_Price__c = 0.00;
newpd.recordtypeid = aa[0].id;
newpd.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd);


}

insert pdlist;

          
          
 
    

  } 
    
}
}

 
i have a trigger and trigger handler class as below and need to see how i can use a checkrecursive class to stop afterupdate code working twice
trigger........how do i reference the constants class into trigger

i thought  
if(Trigger.isUpdate && Trigger.isAfter && constants.isfirsttime)  but i need to add constants.isfirsttime = false

i dont know how to write this please help
 
trigger easycontacttrigger on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete){
  
if(Trigger.isUpdate  && Trigger.isAfter){
 
   
    

easycontacttriggerhelper.sellercon(Trigger.new);
    
    /*
easycontacttriggerhelper.buyerconConveyancy(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconMortgage(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconhomebuyerreport(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconutilityswitchgas(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconutilityswitchwater(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconutilityswitchelec(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconremovals(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyercongassafetycheck(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconpattest(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconaffiliates(Trigger.new, trigger.oldmap);


*/

}
trigger handler class
public static void sellercon(list<Contact> cons) {
    
   
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

  
    
for(Contact con : cons){



if(  con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Seller') ){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
    
            
            

            opplist.add(newopp);

 
    
   
 }
 }
 //outsideforloop  
  //
  if(oppList.size() > 0){
    insert opplist; 
  
list<Product__c> pdlist = new list <Product__c>();
recordtype[] aa = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype 

='Product__c' and Name = 'Residential Sales'];
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

newpd.name = 'Market Appraisal';
newpd.Sale_Price__c = 0.00;
newpd.recordtypeid = aa[0].id;
newpd.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd);


}

insert pdlist;

          
          
 
    

  } 
    
}

checkrecursive class:
public class Constants {
public static final boolean isfirsttime = true;
}

 
i have been modifing some code to allow multiple inserts but only the first inserted opportunity gets a product attached, please help

i need every inserted opp to have same product attached - opportunity/product is master/detail

heres a snippet of my class
 
public static void buyerconConveyancy2(list<Contact> cons) {
system.debug('###list<Contact> cons ' +  cons);



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

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){

system.debug('### con ' +  con);

if(  con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Buyer')){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            newopp.ownerid = con.Allocated_Negotiator__c;
            newopp.name = 'Applicant Conveyancy Opportunity'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   
 


}
}
  //outsideforloop  
  //
  if(oppList.size() > 0){
    insert opplist; 
system.debug('### opplist ' +  opplist);
List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Conveyancing","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
 system.debug('### pd ' +  pd);
    
  } 
    
}

 
this code works fine per single transaction but when i einsert multiple contacts at same time the insert fails

 
public with sharing class easycontacttriggerhelper {
public static void buyerconConveyancy2(list<Contact> cons) {
system.debug('###list<Contact> cons ' +  cons);



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

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){

system.debug('### con ' +  con);

if( con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Buyer')){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            newopp.ownerid = con.Allocated_Negotiator__c;
            newopp.name = 'Applicant Conveyancy Opportunity'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   insert opplist; 
system.debug('### opplist ' +  opplist);
List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Conveyancing","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
 system.debug('### pd ' +  pd);
 


}
}
}
}

 
i have a problem , when testing this code by inserting more than 2 records via dataloader i get below error: i had only ever been testing by manually adding 1 contact at a time.......i understand i may have to move the DML outside of the loop but i am new to apex and not sure how to fix this

easycontacttrigger: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0 with id a13250000005OnzAAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Class.easycontacttriggerhelper.buyerconConveyancy2: line 139, column 1
Trigger.easycontacttrigger: line 28, column 1

here's a snippet of the class
public with sharing class easycontacttriggerhelper {
public static void buyerconConveyancy2(list<Contact> cons) {
system.debug('###list<Contact> cons ' +  cons);



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

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){

system.debug('### con ' +  con);

if( con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Buyer')){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            newopp.ownerid = con.Allocated_Negotiator__c;
            newopp.name = 'Applicant Conveyancy Opportunity'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   insert opplist; 
system.debug('### opplist ' +  opplist);
List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Conveyancing","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
 system.debug('### pd ' +  pd);
 


}
}
}
}

 
i have a trigger and trigger helper class to create opportunity on contact after insert and afetr update. it works great but if i edit contact record it creates opportunity again

i have created recursive class below

how do i write into trigger helper class or trigger or both to run the recursive class? i'm trying to use  if(checkRecursive.runOnce()) but i dont know where to add it?
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


 
trigger easycontacttrigger on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete){
    
if(Trigger.isUpdate  && Trigger.isAfter){
    
    

easycontacttriggerhelper.sellercon(Trigger.new);
easycontacttriggerhelper.buyerconConveyancy(Trigger.new);
easycontacttriggerhelper.buyerconMortgage(Trigger.new);
easycontacttriggerhelper.buyerconhomebuyerreport(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchgas(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchwater(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchelec(Trigger.new);
easycontacttriggerhelper.buyerconremovals(Trigger.new);
easycontacttriggerhelper.buyercongassafetycheck(Trigger.new);
easycontacttriggerhelper.buyerconpattest(Trigger.new);
easycontacttriggerhelper.buyerconaffiliates(Trigger.new);



}
    

else if(Trigger.isinsert && Trigger.isafter){
easycontacttriggerhelper.sellercon(Trigger.new);
easycontacttriggerhelper.buyerconConveyancy(Trigger.new);
easycontacttriggerhelper.buyerconMortgage(Trigger.new);
easycontacttriggerhelper.buyerconhomebuyerreport(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchgas(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchwater(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchelec(Trigger.new);
easycontacttriggerhelper.buyerconremovals(Trigger.new);
easycontacttriggerhelper.buyercongassafetycheck(Trigger.new);
easycontacttriggerhelper.buyerconpattest(Trigger.new);
easycontacttriggerhelper.buyerconaffiliates(Trigger.new);








}

    if(Trigger.isinsert && Trigger.isbefore){
        
easycontacttriggerhelper.contactuserfields(Trigger.new);        
        
    }
if(Trigger.isupdate && Trigger.isbefore){
        
easycontacttriggerhelper.contactuserfields(Trigger.new);        
        
    }

    
}

part of thehelper class below:
public static void sellercon(list<Contact> cons) {
    
   
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){



if(  con.Stage__c == 'Lead' && con.contacttype__c != null  && con.contacttype__C.contains('Seller' ) ){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   insert opplist; 

List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Recordtype.name":"Residential Sales","Name":"Market Appraisal","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
    
   
 }
 }

}

 
ive written a trigger and helper class but struggling to write unit test to pass.  

error message
System.DmlException: Update failed. First exception on row 0 with id a0A25000001lav1EAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, easyoffertrigger: execution of AfterUpdate
caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Contact_Name__c]: [Contact_Name__c]
Class.easyoffertriggerhelper.offeroppaffiliates: line 34, column 1
Trigger.easyoffertrigger: line 5, column 1: []

stack trace
Class.easyoffertriggertest.testrigger: line 29, column 1

here is triiger:
 
trigger easyoffertrigger on pba__offer__c (before insert, after insert, before update, after update, before delete, after delete, after undelete){

    if(Trigger.isUpdate  && Trigger.isAfter){

easyoffertriggerhelper.offeroppaffiliates(Trigger.new);



here is class:
 
public with sharing class easyoffertriggerhelper {

//newcode1

public static void offeroppaffiliates(List<pba__offer__c> offers) {



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

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];
    
    for (pba__offer__c off : offers) {
    
    

        if( off.pba__Status__c == 'Accepted' ) {
            Easy_Opportunity__c newopp = new Easy_Opportunity__c ();
            
            newopp.ownerid = off.ownerid;
            newopp.name = 'Offer Opportunity - Affiliates'; 
            
            newopp.CurrencyIsoCode = off.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = tt[0].Id;
            newopp.Contact_Name__c = off.Contact_Name__c;
            newopp.Close_Date__c = Date.today().addDays(2);
           
            
            

            opplist.add(newopp);

insert opplist;

List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Recordtype.name":"Residential Sales","Name":"Affiliates","List_Price__c":"111.00","Sold_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
}
}
}



here is unit test:
 
@isTest
public class easyoffertriggertest 
{

        static testmethod void testrigger(){
        
contact ct = new contact(firstname = 'iain', lastname = 'banks');


pba__Listing__c lt = new pba__Listing__c(name = 'iainbanks title');
pba__Property__c py = new pba__Property__c(name = 'iainbanks title', PropertyOwnerContactId__c = ct.id);


User u1 = [SELECT Id FROM User WHERE Alias='ibank'];
 

 


pba__offer__c off = new pba__offer__c( currencyisocode = 'GBP', ownerid = u1.id, Package_pruchased__c = '1', pba__Status__c = 'Active', pba__Listing__c = lt.id);

insert off;



             Test.StartTest(); 
             
                off.pba__Status__c = 'Accepted';
                 update off;
             
                
                
             Test.StopTest();
            
        }

}

 
trigger:
 
trigger easyopportunitytrigger on Easy_Opportunity__c (before insert, after insert, before update, after update, before delete, after delete, after undelete){
if(Trigger.isUpdate && Trigger.isAfter){
        easyopportunitytriggerhelper.oppbundle1(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.oppbundle2(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.oppbundle3(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupsellaffiliates(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupsellgassafetycheck(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupsellhomebuy(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupsellpattest(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupsellremovals(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupselluswitchelec(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupselluswitchgas(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.lostmaupselluswitchwater(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle1upsellConveyancing(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle1upsellEPC(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle1upsellHostedViewings(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle1upsellMortgage(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle1upsellSalesProgression(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle2upsellBlockViewing(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle2upsellMortgage(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle2upselluswitchelec(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle2upselluswitchgas(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle2upselluswitchwater(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle3upsellConveyancing(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle3upsellEPC(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle3upsellMortgage(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle3upselluswitchelec(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle3upselluswitchgas(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.bundle3upselluswitchwater(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicanthomebuyerreport(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicantutilityswitchgas(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicantutilityswitchwater(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicantutilityswitchelec(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicantremovals(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicantgassafetycheck(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicantpattest(Trigger.new, trigger.oldmap);
        easyopportunitytriggerhelper.applicantaffiliates(Trigger.new, trigger.oldmap);
    
}
}



part of the class displayed here:
public with sharing class easyopportunitytriggerhelper {

public static void oppbundle1 (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();

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 == recId){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();

            newopp.ownerid = oppty.ownerid;
            newopp.name = 'Bundle 1 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(2);
            

     opplist.add(newopp);

   insert opplist; 


    List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Recordtype.name":"Residential Sales","Name":"Easy One","List_Price__c":"111.00","Sold_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;

}
}
}

unit test:
 
@isTest
public class easyopportunitytriggerTest 
{

        static testmethod void test_trigger(){
        
Account acct = new Account(Name='Test');


    insert acct;
        

           

contact ct = new contact(firstName='Iain', lastname =  'Banks');

    insert ct;
Id recId = Schema.SObjectType.Easy_Opportunity__c.getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
            Easy_Opportunity__c easyOpp = new Easy_Opportunity__c();
            
            
            easyopp.Account_Name__c = acct.id;
            easyOpp.Stage__c = 'New';
            easyOpp.Name = 'Market Appraisal';
            easyOpp.Close_Date__c = system.today();
            easyOpp.currencyisocode = 'GBP';
            easyOpp.Contact_Name__c = ct.id;
            easyOpp.recordtypeid = recid;
            
            // insert all required field;
            insert easyOpp;
            
            product__c pd = new product__c();
            pd.name='easy two';
            pd.Easy_Opportunity__c=easyopp.id;
           
            
            insert pd;

             Test.StartTest(); 
             
                easyOpp.Stage__c = 'Closed Won';
                easyOpp.Opportunity_Rating__c = 'Hot';
                easyOpp.recordtypeid = recid;
                update easyOpp;
                
                easyOpp.Stage__c = 'Closed lost';
                easyOpp.Opportunity_Lost_Reason__c = 'Other';
                easyOpp.Other_Lost_Reason__c = 'dfdf';
                easyOpp.recordtypeid = recid;
                update easyOpp;
                
                 easyOpp.name =  'Bundle 2 Opportunity';
                update easyOpp;
                
                easyOpp.name =  'market appraisal';
                update easyOpp;
                
                easyOpp.name =  'Bundle 3 Opportunity';
                update easyOpp;
                
                easyOpp.name =  'Bundle 1 Opportunity';
                update easyOpp;
                
                easyOpp.name =  'Mortgage & Conveyancy Opportunity';
                update easyOpp;
                
             Test.StopTest();
            
        }

}

 
i have Added an auto-number field to User selecting the "Generate Auto Number for existing records" checkbox to populate the field for the existing Users.

Then use it from Apex code - i need help with apex on the following::
  • Generate say 10 random numbers between the min and max values of that auto-number field obtained using an aggregate query.
  • Query for the Users that match those numbers and also have IsActive=true.
  • Take the one 
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
3 opps in 1 account if close win 1 opp how do i close lose other 2 opps?
i am trying to tidy my code so that i reduce the number of soqls here is a sample
public  class updatedeletopps {

    public static void updatedelopps(List<Contact> contactIds){
        Id recId = Schema.SObjectType.Easy_Opportunity__c.getRecordTypeInfosByName().get('Residential Sales').getRecordTypeId();
        
        
//n1        
//
        contact[] activecontacts = [select id, active_contact__c from contact where id in :contactIds];
        set<id> activecontactsset = new set<id>();
        for(contact con : activecontacts) 
            if(con.active_contact__c == 'No') 
            	activecontactsset.add(con.Id);

     List<easy_opportunity__c> oppsToUpdate1 = new List<easy_opportunity__c>();
        list<easy_opportunity__c> opptys = [select id, Stage__c, recordtypeid from easy_opportunity__c where Contact_Name__c in: 
		activecontactsset and name = 'Market Appraisal' and stage__c = 'Closed Lost' and recordtypeid = :recId];
     for(easy_opportunity__c opp1 : opptys)
     { 
         opp1.Stage__c='Closed Lost';
         opp1.Opportunity_Lost_Reason__c = 'Lost Contact';
         opp1.Opportunity_Rating__c = 'Cold';
         oppsToUpdate1.add(opp1);
     }

     update oppsToUpdate1;
        
    list<easy_opportunity__c> oppstodelete2 = new list<easy_opportunity__c>();
        list<easy_opportunity__c> opptys1 = [select id, stage__c, recordtypeid from easy_opportunity__c where contact_name__c 

in:activecontactsset and (name =  'Lost MA Upsell - Gas Safety Check' or name = 'Lost MA Upsell - Home Buyer Report' or name = 'Lost MA Upsell - Conveyancing' or name = 'Lost MA Upsell - Mortgage') ];
        for(easy_opportunity__c opp2 : opptys1)
        {
        oppstodelete2.add(opp2);
        }
        delete oppstodelete2;
i need to convert this trigger into class methos so i can call from trigger, i have only done this a couple of times so need some help
trigger oppclosedate on Easy_Opportunity__c (before update) {

 


    
    
for (easy_opportunity__c opp : trigger.new)
    
    
    if (opp.Stage__c == 'Closed Won' || opp.Stage__c == 'Closed Lost')
        {
          opp.Close_Date__c= system.today();
         }
    
    
    
    
    
}

 
i have a trigger and trigger handler class as below and need to see how i can use a checkrecursive class to stop afterupdate code working twice
trigger........how do i reference the constants class into trigger

i thought  
if(Trigger.isUpdate && Trigger.isAfter && constants.isfirsttime)  but i need to add constants.isfirsttime = false

i dont know how to write this please help
 
trigger easycontacttrigger on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete){
  
if(Trigger.isUpdate  && Trigger.isAfter){
 
   
    

easycontacttriggerhelper.sellercon(Trigger.new);
    
    /*
easycontacttriggerhelper.buyerconConveyancy(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconMortgage(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconhomebuyerreport(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconutilityswitchgas(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconutilityswitchwater(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconutilityswitchelec(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconremovals(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyercongassafetycheck(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconpattest(Trigger.new, trigger.oldmap);
easycontacttriggerhelper.buyerconaffiliates(Trigger.new, trigger.oldmap);


*/

}
trigger handler class
public static void sellercon(list<Contact> cons) {
    
   
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

  
    
for(Contact con : cons){



if(  con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Seller') ){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
    
            
            

            opplist.add(newopp);

 
    
   
 }
 }
 //outsideforloop  
  //
  if(oppList.size() > 0){
    insert opplist; 
  
list<Product__c> pdlist = new list <Product__c>();
recordtype[] aa = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype 

='Product__c' and Name = 'Residential Sales'];
for(Easy_Opportunity__c eachopp:opplist){
Product__c newpd = new Product__c();

newpd.name = 'Market Appraisal';
newpd.Sale_Price__c = 0.00;
newpd.recordtypeid = aa[0].id;
newpd.Easy_Opportunity__c = eachopp.id;

pdlist.add(newpd);


}

insert pdlist;

          
          
 
    

  } 
    
}

checkrecursive class:
public class Constants {
public static final boolean isfirsttime = true;
}

 
i have been modifing some code to allow multiple inserts but only the first inserted opportunity gets a product attached, please help

i need every inserted opp to have same product attached - opportunity/product is master/detail

heres a snippet of my class
 
public static void buyerconConveyancy2(list<Contact> cons) {
system.debug('###list<Contact> cons ' +  cons);



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

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){

system.debug('### con ' +  con);

if(  con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Buyer')){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            newopp.ownerid = con.Allocated_Negotiator__c;
            newopp.name = 'Applicant Conveyancy Opportunity'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   
 


}
}
  //outsideforloop  
  //
  if(oppList.size() > 0){
    insert opplist; 
system.debug('### opplist ' +  opplist);
List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Conveyancing","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
 system.debug('### pd ' +  pd);
    
  } 
    
}

 
this code works fine per single transaction but when i einsert multiple contacts at same time the insert fails

 
public with sharing class easycontacttriggerhelper {
public static void buyerconConveyancy2(list<Contact> cons) {
system.debug('###list<Contact> cons ' +  cons);



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

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){

system.debug('### con ' +  con);

if( con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Buyer')){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            newopp.ownerid = con.Allocated_Negotiator__c;
            newopp.name = 'Applicant Conveyancy Opportunity'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   insert opplist; 
system.debug('### opplist ' +  opplist);
List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Conveyancing","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
 system.debug('### pd ' +  pd);
 


}
}
}
}

 
i have a problem , when testing this code by inserting more than 2 records via dataloader i get below error: i had only ever been testing by manually adding 1 contact at a time.......i understand i may have to move the DML outside of the loop but i am new to apex and not sure how to fix this

easycontacttrigger: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0 with id a13250000005OnzAAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Class.easycontacttriggerhelper.buyerconConveyancy2: line 139, column 1
Trigger.easycontacttrigger: line 28, column 1

here's a snippet of the class
public with sharing class easycontacttriggerhelper {
public static void buyerconConveyancy2(list<Contact> cons) {
system.debug('###list<Contact> cons ' +  cons);



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

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){

system.debug('### con ' +  con);

if( con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Buyer')){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            newopp.ownerid = con.Allocated_Negotiator__c;
            newopp.name = 'Applicant Conveyancy Opportunity'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid = tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   insert opplist; 
system.debug('### opplist ' +  opplist);
List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Conveyancing","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
 system.debug('### pd ' +  pd);
 


}
}
}
}

 
i have a trigger and trigger helper class to create opportunity on contact after insert and afetr update. it works great but if i edit contact record it creates opportunity again

i have created recursive class below

how do i write into trigger helper class or trigger or both to run the recursive class? i'm trying to use  if(checkRecursive.runOnce()) but i dont know where to add it?
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


 
trigger easycontacttrigger on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete){
    
if(Trigger.isUpdate  && Trigger.isAfter){
    
    

easycontacttriggerhelper.sellercon(Trigger.new);
easycontacttriggerhelper.buyerconConveyancy(Trigger.new);
easycontacttriggerhelper.buyerconMortgage(Trigger.new);
easycontacttriggerhelper.buyerconhomebuyerreport(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchgas(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchwater(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchelec(Trigger.new);
easycontacttriggerhelper.buyerconremovals(Trigger.new);
easycontacttriggerhelper.buyercongassafetycheck(Trigger.new);
easycontacttriggerhelper.buyerconpattest(Trigger.new);
easycontacttriggerhelper.buyerconaffiliates(Trigger.new);



}
    

else if(Trigger.isinsert && Trigger.isafter){
easycontacttriggerhelper.sellercon(Trigger.new);
easycontacttriggerhelper.buyerconConveyancy(Trigger.new);
easycontacttriggerhelper.buyerconMortgage(Trigger.new);
easycontacttriggerhelper.buyerconhomebuyerreport(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchgas(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchwater(Trigger.new);
easycontacttriggerhelper.buyerconutilityswitchelec(Trigger.new);
easycontacttriggerhelper.buyerconremovals(Trigger.new);
easycontacttriggerhelper.buyercongassafetycheck(Trigger.new);
easycontacttriggerhelper.buyerconpattest(Trigger.new);
easycontacttriggerhelper.buyerconaffiliates(Trigger.new);








}

    if(Trigger.isinsert && Trigger.isbefore){
        
easycontacttriggerhelper.contactuserfields(Trigger.new);        
        
    }
if(Trigger.isupdate && Trigger.isbefore){
        
easycontacttriggerhelper.contactuserfields(Trigger.new);        
        
    }

    
}

part of thehelper class below:
public static void sellercon(list<Contact> cons) {
    
   
    
list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>();

recordtype[] tt = [Select  r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales'];

for(Contact con : cons){



if(  con.Stage__c == 'Lead' && con.contacttype__c != null  && con.contacttype__C.contains('Seller' ) ){

Easy_Opportunity__c newopp = new Easy_Opportunity__c ();



            
            
            newopp.ownerid = con.allocated_user__c;
            newopp.name = 'Market Appraisal'; 
            newopp.account_name__c = con.accountid;
            newopp.CurrencyIsoCode = con.currencyisocode;
            newopp.stage__c = 'New';
            newopp.recordtypeid =  tt[0].Id;
            newopp.Contact_Name__c = con.id;
            newopp.Close_Date__c = Date.today().addDays(2);
            
            
            

            opplist.add(newopp);

   insert opplist; 

List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Recordtype.name":"Residential Sales","Name":"Market Appraisal","Sale_Price__c":"111.00"}]', List<Product__c>.class);
    for (Product__c eachProd : Pd)
 eachProd.Easy_Opportunity__c = opplist[0].id;
    
insert Pd;
    
   
 }
 }

}