function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Salesforce Admin 110Salesforce Admin 110 

have prevented duplicate creation of opps on afterupdate but if customer returns how create same opps for next transaction

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;

          
          
 
    

  } 
    
 

}

 
@Karanraj@Karanraj
Just exclude the closed opportunity in your condition while checking the duplicate values. so that for a client it will have only one open opportunity in the system.