+ Start a Discussion
azar khasimazar khasim 

Need to add some criteria where opportunity is Closedwon update project record.

Hello Everyone,

Here I need some criteria to add to my existing code. i.e..,
We have 
Opportunity object
Project object.

In Project, we have two record types.
1. Project Template 
2.Interior Works Project.

In Project Template we have three records(We call them as templates)
A Checkbox field called Default Project Template is true in any one of the records.


Now as per current business 

Whenever Opportunity is Closedwon 
A need project is getting created.

(A New project is creating like in below
When oppty is closedwon
in Project Template record type on of the record has default project template checkbox is true, then we are cloning that record and creating as a new project in Other record type-- Interior Works Project )


Now my new criteria is 
I have created three checkboxes in Opportunity when I select 1st checkbox and make oppty closedwon then first update the default project template checkbox of the first record in the project has to get an update and then clone operation has to be performed.


Here below is my Code in Apex Class:

public class ProjectManagementUtility {
    public static List<Milestone1_Project__c> cloneProject(List<Opportunity> oppts) {
        Decimal [] lProjectValues = new List<Decimal>();
        DateTime [] lSignUpDatesOfProjects = new List<DateTime>();
        
        List<Opportunity> oppList  = [SELECT id, name, Account.Name, AccountId,Region__c, Amount, Designer__c, Offer_and_Discounts__c, CloseDate, Meeting_Venue__c, 
                                      Customer_ID__c, Enquiry_ID__c, OwnerId, Signup_Amount__c,Insert_60_days_Project_Milestones__c,Insert_90_days_Project_Milestones__c,
                                      Insert_120_days_Project_Milestones__c,(SELECT ContentDocumentId, Visibility, ShareType FROM ContentDocumentLinks),
                                      (SELECT id FROM Projects__r) FROM Opportunity WHERE Id IN :oppts];
        
        for(Opportunity lOpp : oppList) {
            lProjectValues.add(lOpp.Amount);
            lSignUpDatesOfProjects.add(DateTime.newInstance(lOpp.CloseDate, Time.newInstance(10, 0, 0, 0)));
        }
        
        
        return cloneProject(oppts, lSignUpDatesOfProjects, lProjectValues);
    }
    
    public static List<Milestone1_Project__c> cloneProject(List<Opportunity> oppts, DateTime [] aSignUpDatesOfProjects, Decimal [] aProjectValues) {
        List<Milestone1_Project__c> newProjs = new List<Milestone1_Project__c>();
        List<Milestone1_Milestone__c> newmiles = new List<Milestone1_Milestone__c>();
        List<Milestone1_Milestone__c> updatedMiles = new List<Milestone1_Milestone__c>();
        Map<Id,List<Milestone1_Milestone__c>> mapNewmiles = new Map<Id,List<Milestone1_Milestone__c>>();
        
        List<Opportunity> oppList  = [SELECT id, name, Account.Name, AccountId,Region__c, Amount, Designer__c, Offer_and_Discounts__c, CloseDate, Meeting_Venue__c, 
                                      Customer_ID__c, Enquiry_ID__c, OwnerId, Signup_Amount__c,Insert_60_days_Project_Milestones__c,Insert_90_days_Project_Milestones__c,
                                      Insert_120_days_Project_Milestones__c,(SELECT ContentDocumentId, Visibility, ShareType FROM ContentDocumentLinks),
                                      (SELECT id FROM Projects__r) FROM Opportunity WHERE Id IN :oppts];
        
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>(oppList);
        
        Milestone1_Project__c proj = [SELECT Id, Name, Project_Duration_in_days__c, Offers_and_Discounts__c,Total_Hours_Budget__c,
                                      Total_Expense_Budget__c, First_Cut_Presentation_Date__c, X3D_Presentation_Date__c, Design_Finalization_Date__c FROM Milestone1_Project__c 
                                      WHERE Default_Template_Project__c = true LIMIT 1];
        
        if(proj != NULL) {
            Integer lIndex = 0;
            Id projRecordTypeId = Schema.SObjectType.Milestone1_Project__c.getRecordTypeInfosByDeveloperName().get('Interior_Works_Project').getRecordTypeId();
        
            for(Opportunity opp : oppList) {
                if(opp.Projects__r.size() > 0) {
                    continue;
                }
                
                DateTime  lProjectStartDate = aSignUpDatesOfProjects[lIndex];
                Milestone1_Project__c projCopy = proj.clone(false,true);
                projCopy.RecordTypeId = projRecordTypeId;
                projCopy.Name = 'DC - ' + opp.Account.Name + ' - ' + opp.Customer_ID__c;
                
                if(String.isNotBlank(opp.Meeting_Venue__c)) {
                    projCopy.Name += ' - ' + opp.Meeting_Venue__c;
                }
                
                projCopy.Customer__c = opp.AccountId;
                projCopy.Amount__c = aProjectValues[lIndex];
                projCopy.Opportunity__c = opp.id;
                projCopy.Enquiry_ID__c = opp.Enquiry_ID__c;
                projCopy.Signup_Amount__c = opp.Signup_Amount__c;
                projCopy.REGION__c = opp.Region__c;
                projCopy.Offers_and_Discounts__c=opp.Offer_and_Discounts__c;
                projCopy.Kickoff__c = lProjectStartDate.Date();
                System.debug('^^^ ' + projCopy.Project_Duration_in_days__c);
                
                projCopy.Deadline__c = lProjectStartDate.Date() + Integer.valueOf(projCopy.Project_Duration_in_days__c);
                projCopy.Survey_Date__c = lProjectStartDate + 2;
                projCopy.KYC_Date__c = lProjectStartDate + 7;
                projCopy.First_Cut_Presentation_Date__c = lProjectStartDate + 12;
                projCopy.X3D_Presentation_Date__c = lProjectStartDate + 31;
                projCopy.Kickoff_Meeting_Date__c = lProjectStartDate + 47;
                projCopy.Design_Finalization_Date__c = lProjectStartDate + 21;
                newProjs.add(projCopy);
                lIndex++;
            }
            
            if(newProjs.size() > 0) {
                insert newProjs;
            }
            
            Id milesRecordTypeId = Schema.SObjectType.Milestone1_Milestone__c.getRecordTypeInfosByDeveloperName().get('Project_Milestone').getRecordTypeId();
            List<Milestone1_Milestone__c> miles = [SELECT Id, Name, Type__c, EligibleAmountPercentage__c, Project__c, Assign_To_Role__c, Hours_Budget__c, 
                                                   MilestoneTemplateSFID__c,Predecessor_Milestone__c,Successor_Milestone__c,Expense_Budget__c,Description__c,
                                                   Collectable_Amount_Percent_Before_This__c, Order__c,
                                                   Previous_Payment_Milestone__c,Relative_Deadline_in_days__c,Relative_Kickoff_Date_in_days__c FROM Milestone1_Milestone__c 
                                                   WHERE Project__c = : proj.Id];
            
            if(miles != null && miles.size() > 0) {
                for(Milestone1_Project__c p : newProjs) {
                    List<Milestone1_Milestone__c> projMile = mapNewmiles.get(p.id);
                    
                    if(projMile == null) {
                        projMile = new List<Milestone1_Milestone__c>();
                        mapNewmiles.put(p.id, projMile);
                    }
                    
                    for(Milestone1_Milestone__c mile : miles) {
                        Milestone1_Milestone__c mileCopy = mile.clone(false,true);
                        mileCopy.RecordTypeId = milesRecordTypeId;
                        mileCopy.Name = mileCopy.Name.replaceAll('TEMPLATE - ', ''); 
                        mileCopy.MilestoneTemplateSFID__c = mile.Id;
                        mileCopy.Milestone_Template_PredecessorSFID__c = mile.Predecessor_Milestone__c;
                        mileCopy.Milestone_Template_SuccessorSFID__c = mile.Successor_Milestone__c;
                        mileCopy.Milestone_Template_Prev_Payment_SFID__c = mile.Previous_Payment_Milestone__c;
                        mileCopy.Project__c = p.Id;
                        mileCopy.Assign_To_Role__c =  mile.Assign_To_Role__c;
                        mileCopy.Type__c = mile.Type__c;
                        mileCopy.Kickoff__c = p.Kickoff__c + Integer.valueOf(mileCopy.Relative_Kickoff_Date_in_days__c);
                        mileCopy.Deadline__c = p.Kickoff__c + Integer.valueOf(mileCopy.Relative_Deadline_in_days__c);
                        mileCopy.Customer__c = p.Customer__c;
                        mileCopy.EligibleAmountPercentage__c = mile.EligibleAmountPercentage__c; 
                        newmiles.add(mileCopy);
                        projMile.add(mileCopy);
                    }
                }
                
                if(newmiles.size() > 0) {
                    insert newmiles;
                }
                
                Map<String, ID> MilesPredRef = new Map<String, ID>();
                
                for(Milestone1_Milestone__c mile : newmiles) {
                    MilesPredRef.put(mile.MilestoneTemplateSFID__c + '' + mile.Project__c, mile.Id);
                }
                
                Set<ID> lDupeCheckerSet = new Set<ID>();
                
                for(Milestone1_Project__c p : newProjs) {
                    for(Milestone1_Milestone__c lmile : mapNewmiles.get(p.id)) {
                        if(lmile.Milestone_Template_PredecessorSFID__c != null) {
                            lmile.Predecessor_Milestone__c = MilesPredRef.get(lmile.Milestone_Template_PredecessorSFID__c + '' + lmile.Project__c);
                            
                            if(!lDupeCheckerSet.contains(lmile.id)) {
                                lDupeCheckerSet.add(lmile.id);
                                updatedMiles.add(lmile);
                            }
                        }
                        
                        if(lmile.Milestone_Template_SuccessorSFID__c != null) {
                            lmile.Successor_Milestone__c = MilesPredRef.get(lmile.Milestone_Template_SuccessorSFID__c + '' + lmile.Project__c);
                            
                            if(!lDupeCheckerSet.contains(lmile.id)) {
                                lDupeCheckerSet.add(lmile.id);
                                updatedMiles.add(lmile);
                            }
                        }
                        
                        if(lmile.Milestone_Template_Prev_Payment_SFID__c != null) {
                            lmile.Previous_Payment_Milestone__c = MilesPredRef.get(lmile.Milestone_Template_Prev_Payment_SFID__c + '' + lmile.Project__c);

                            if(!lDupeCheckerSet.contains(lmile.id)) {
                                lDupeCheckerSet.add(lmile.id);
                                updatedMiles.add(lmile);
                            }
                        }
                    }
                }
                
                update updatedMiles;
                Map<String, ID> MilesRef = new Map<String, ID>();
                
                for(Milestone1_Milestone__c mile : newmiles) {
                    MilesRef.put(mile.MilestoneTemplateSFID__c+''+mile.Project__c, mile.Id);
                }
            }  
            
            if(newProjs.size() > 0) {
                ContentDocumentLink contDocLink;
                List<Project_Team_Member__c> ptms = new List<Project_Team_Member__c>();
                List<ContentDocumentLink> cdls = new List<ContentDocumentLink>();
                
                for(Milestone1_Project__c p : newProjs){
                    ptms.add(new Project_Team_Member__c(Role__c = 'Primary Designer', User__c =  oppMap.get(p.Opportunity__c).Designer__c, Project__c = p.Id));
                    ptms.add(new Project_Team_Member__c(Role__c = 'Sales Executive', User__c =  oppMap.get(p.Opportunity__c).OwnerId, Project__c = p.Id));
                    
                    for(ContentDocumentLink cdl : oppMap.get(p.Opportunity__c).ContentDocumentLinks) {
                        contDocLink = new ContentDocumentLink();
                        contDocLink =  cdl.clone();
                        contDocLink.LinkedEntityId = p.Id;
                        cdls.add(contDocLink);
                    }
                }
                
                if(ptms!= null && ptms.size() > 0) {
                    insert ptms;
                }
                
                if(cdls != null && cdls.size() > 0) {
                    insert cdls;
                }
            }
        }
        
        return newProjs;
    }
}

Please give me some solution for this.

Thanks and Regards,
Azar Khasim.