public class RenewaloppClass{ public static void RenewalOppMethod(ListOpplist){ try{ List RenewalOpportunities = new List(); Id newrectypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New Business').getRecordTypeId(); Id RenewablerectypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Renewable Business').getRecordTypeId(); System.debug('@@@@@ +new Business Id:' + newrectypeId); System.debug('@@@@@ + Renewable Business Id:' + RenewablerectypeId); Listopplist2=new List(Opplist); system.debug('@@@@@+ Getting Data'+opplist2); Listopt=new List(); for(Opportunity op : Opplist){ System.debug('@@@@@ Opplist in loop'+Opplist); if (op.RecordTypeId == newrectypeId && op.StageName == 'Closed Won') { Opportunity renewalOpp = new Opportunity(); System.debug('@@@@@@ inside the 1st loop' ); // Set the fields for the Renewal Opportunity renewalOpp.RecordTypeId = RenewablerectypeId; renewalOpp.Name = op.Name + ' - Renewal'; renewalOpp.CloseDate = System.today() + 30; renewalOpp.StageName = 'Renewal Ready'; renewalOpp.Parent_Opportunity__c = op.Id; renewalOpportunities.add(renewalOpp); } else if (op.RecordTypeId == RenewablerectypeId && op.StageName == 'Closed Won'){ Opportunity renewalOpp = new Opportunity(); System.debug('@@@@@@ inside the 2nd loop' ); // Set the fields for the Renewal Opportunity renewalOpp.RecordTypeId = RenewablerectypeId; renewalOpp.Name = op.Name + ' - Renewal'; renewalOpp.CloseDate = System.today() + 30; renewalOpp.StageName = 'Renewal Ready'; renewalOpp.Parent_Opportunity__c = op.Id; renewalOpportunities.add(renewalOpp); } else{ System.debug('@@@@ nothiggg'); } } insert RenewalOpportunities; } catch(Exception e){ System.debug('@@@@@ Error:' + e); } } //For Update.... public static void RenewaloppMethod2(Map nMap,Map oMap){ try { List RenewalOpportunities = new List(); Boolean exist=False; Id newrectypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New Business').getRecordTypeId(); Id RenewablerectypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Renewable Business').getRecordTypeId(); System.debug('@@@@@ +new Business Id:' + newrectypeId); System.debug('@@@@@ + Renewable Business Id:' + RenewablerectypeId); Set idd = new Set(); // Use set to store Id values //getting id from nMap for (Opportunity od1 : nMap.values()) { idd.add(od1.Id); // Add the Id value to the list using the add() method } List oppt = [SELECT Parent_Opportunity__c FROM Opportunity WHERE RecordTypeId=:RenewablerectypeId AND Parent_Opportunity__c IN :idd]; system.debug('@@@@@ Opppp id value'+oppt); //getting those opportunities which has already child in related And stroring them into set of id Set set1 = new Set(); for(Opportunity ol: [SELECT Parent_Opportunity__c FROM Opportunity WHERE RecordTypeId=:RenewablerectypeId AND Parent_Opportunity__c IN :idd]){ set1.add(ol.Parent_Opportunity__c); } //updating value of exist for(Opportunity opl :nMap.values()){ system.debug('@@@@@ in loop'); if(set1.contains(opl.Id)){ exist=True; system.debug('coantans???'+exist); } else{ exist=False; system.debug('coantans???'+exist); } //checking the conditions if(oMap.get(opl.Id).StageName!=nMap.get(opl.Id).StageName && nMap.get(opl.Id).StageName=='Closed Won' && exist==False){ if (opl.RecordTypeId == newrectypeId || opl.RecordTypeId == RenewablerectypeId ) { Opportunity renewalOpp = new Opportunity(); system.debug('Making record'); // Set the fields for the Renewal Opportunity renewalOpp.RecordTypeId = RenewablerectypeId; renewalOpp.Name = opl.Name + ' - Renewal'; renewalOpp.CloseDate = System.today() + 30; renewalOpp.StageName = 'Renewal Ready'; renewalOpp.Parent_Opportunity__c = opl.Id; RenewalOpportunities.add(renewalOpp); } else{ System.debug('@@@@@@ no Criteria matched'); } } else{ System.debug('Record already attached'); } insert RenewalOpportunities; } } catch(Exception e){ System.debug('@@@@ Error:' + e); } } }