• Rajat Burman
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
Hi Experts, 
I am getting Apex CPU time limit exceeded.
Error is in expression '{!saveopportunityAfterEdit}' in component <apex:commandButton> in page unclonedopportunities_vf: (hed)
Kindly help me out.
Here is my code.
 
public class UnclonedOpportunities_CS {
    
    public List<Opportunity> opCheckList{get;set;}  
    
    public Id currentCycleId;     
    public UnclonedOpportunities_CS(ApexPages.StandardController controller){ 
        Id idsss;
        if(test.isRunningTest()) {
            
            Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Internship Opportunity').getRecordTypeId();
            Date dt = System.Today();
            Account testAccount = new Account();
            testAccount.Name = 'Test Account' ;
            insert testAccount;
            
            Contact con = new Contact(Lastname='test Contact', AccountId=testAccount.id); 
            insert con;
            
            alu_Internship_Cycle__c cycle1= new alu_Internship_Cycle__c(Name='Cycle1', Start_Date__c = system.today(), End_Date__c=system.today());
            insert cycle1;
            
            Opportunity opp = new Opportunity();
            opp.RecordTypeId = OppRecordTypeId;
            opp.Name = 'Test Opportunity';
            opp.StageName = 'Under Discussion';
            opp.CloseDate = dt;
            opp.AccountId = testAccount.Id;
            opp.Internship_Cycle__c = cycle1.Id;
            opp.Number_of_Internships_Committed__c = 1;
            opp.Opportunity_Countries__c = 'Algeria';
            opp.Key_Contact__c = con.id;
            opp.Final_Commitment__c = 2000.00;
            insert opp;
            
            alu_Internship_Cycle__c cycle2= new alu_Internship_Cycle__c(Name='Cycle2', Start_Date__c = dt.addYears(1), End_Date__c=system.today(),Clone_Internship_Opportunity__c=true);
            insert cycle2;
            
            currentCycleId = cycle2.id;
        }
        else if(!test.isRunningTest()) {
            currentCycleId =  ApexPages.currentPage().getParameters().get('id'); 
        }
        alu_Internship_Cycle__c cycle = [SELECT Id,start_Date__c FROM alu_Internship_Cycle__c WHERE Id =: currentCycleId];  
        Set<Id> cloneOppPrevIds = new Set<Id>();
        List<Opportunity> clonedList = [SELECT id, Name, RecordTypeId, AccountId, Account.Name, Key_Contact__c,Internship_Cycle__c,
                                        CloseDate,  StageName ,Opportunity_Countries__c,Final_Commitment__c,Previous_Opportunity_Id__c
                                        FROM Opportunity WHERE Internship_Cycle__c =:currentCycleId]; 
        system.debug('cloned List '+clonedList);
        
        for(Opportunity ops : clonedList) {
            cloneOppPrevIds.add(ops.Previous_Opportunity_Id__c);
        }
        
        Map<Id,alu_Internship_Cycle__c> mapIdbyOldInternCyc = new Map<Id,alu_Internship_Cycle__c> ([SELECT Id, Name, Start_Date__c 
                                                                                                    FROM alu_Internship_Cycle__c 
                                                                                                    WHERE Id !=: currentCycleId 
                                                                                                    AND Start_Date__c =:  cycle.Start_Date__c.addYears(-1)]);
        system.debug('mapIdbyOldInternCyc '+mapIdbyOldInternCyc);   
        
        List<Opportunity> oppListByOldCyc = [SELECT id, Name, RecordTypeId, AccountId, Account.Name,Key_Contact__c,
                                             CloseDate,  StageName , Internship_Cycle__c, Opportunity_Countries__c,Final_Commitment__c,Previous_Opportunity_Id__c
                                             FROM Opportunity 
                                             WHERE Internship_Cycle__c IN: mapIdbyOldInternCyc.KeySet()];
        system.debug('oppListByOldCyc'+oppListByOldCyc);
        
        opCheckList = new List<Opportunity>(); 
        for(opportunity opOld : oppListByOldCyc){
            if(!cloneOppPrevIds.contains(opOld.Id)){
                opCheckList.add(opOld);  
            }
        }
        system.debug('opCheckList'+opCheckList);
    }
    
    public PageReference saveopportunityAfterEdit() {
        Set<Opportunity> insertSetList = new Set<Opportunity>();
        integer i = 0;
        try{
            if(opCheckList.size() > 0) {
                update this.opCheckList; 
                System.debug('opCheckList '+opCheckList);
            }
        } catch(Exception e){
            e.getMessage();
        }
        system.debug('this.opCheckList '+opCheckList);
        try 
        {
            Opportunity OppToInsertInNewIntCycle = new Opportunity();
            List<Opportunity> oppUnClonedList = new List<Opportunity>();
            for (Opportunity opppp : opCheckList) {
                if((opppp.CloseDate == null ) || String.isBlank(opppp.StageName) || String.isBlank(opppp.Opportunity_Countries__c) || String.isBlank(opppp.name) || String.isBlank(opppp.AccountId) || String.isBlank(opppp.Key_Contact__c) || String.isBlank(opppp.Internship_Cycle__c)) {
                    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.info, 'Please fill all the details.');
                    apexPages.addMessage(msg);
                    system.debug('Error');
                    return null;
                } 
                else if((opppp.CloseDate != null ) && String.isNotBlank(opppp.StageName) && String.isNotBlank(opppp.Opportunity_Countries__c) && String.isNotBlank(opppp.name) && String.isNotBlank(opppp.AccountId) && String.isNotBlank(opppp.Key_Contact__c) && String.isNotBlank(opppp.Internship_Cycle__c)) {
                    OppToInsertInNewIntCycle.name =  opppp.name; 
                    OppToInsertInNewIntCycle.RecordTypeId = opppp.RecordTypeId;
                    OppToInsertInNewIntCycle.AccountId = opppp.AccountId;
                    OppToInsertInNewIntCycle.StageName = opppp.StageName;
                    OppToInsertInNewIntCycle.Key_Contact__c = opppp.Key_Contact__c;
                    OppToInsertInNewIntCycle.Internship_Cycle__c = currentCycleId;
                    OppToInsertInNewIntCycle.Opportunity_Countries__c = opppp.Opportunity_Countries__c;
                    OppToInsertInNewIntCycle.CloseDate = opppp.CloseDate;
                    OppToInsertInNewIntCycle.Previous_Opportunity_Id__c = opppp.id;
                }
                insertSetList.add(OppToInsertInNewIntCycle);
                if(insertSetList.size() > 0 ) {
                    oppUnClonedList.addAll(insertSetList);
                }
                system.debug('add to list ' + oppUnClonedList);
                system.debug('add to Set ' + insertSetList);
                System.debug('insertSetList.size() ' +insertSetList.size()); 
                System.debug('oppUnClonedList.size() ' +oppUnClonedList.size()); 
            }
            if(oppUnClonedList.size() > 0){
                system.debug('rec oppUnClonedList ' + oppUnClonedList);
                insert oppUnClonedList;
                System.debug('oppUnClonedListoppUnClonedListoppUnClonedList '+oppUnClonedList );
            }
        } catch(Exception e) {
            e.getMessage(); 
        }
        system.debug('currentCycleId pagereference '+ currentCycleId);
        PageReference page = new Pagereference('/'+currentCycleId);
        page.setRedirect(false);
        return page;
    }
}

 
Hi,
I am not able to give permission to a profile to merge duplicate contacts.
I am using 'Duplicate Check for Salesforce1' package.

Please help
Thanks
Hi Experts,

I have added a button "opt-out" in the email template.
This email will be sent to people and when they click the opt-out button, then the stage in opportunity should change to opt out.
How can do this

Please help.
Thanks.
Hi Experts,  
I am pulling and assigning the date value like the below code. 
for(Application__c AppRound : AppRoundData) {
Date uploadFinanceSponsor =  AppRound.Application_Round__r.Deadline_for_Uploading_Finances_Sponsors__c;
}
I need to change the date to January 8, 2018 format.
Hi Experts,

I have added a button "opt-out" in the email template.
This email will be sent to people and when they click the opt-out button, then the stage in opportunity should change to opt out.
How can do this

Please help.
Thanks.
Hi Experts,  
I am pulling and assigning the date value like the below code. 
for(Application__c AppRound : AppRoundData) {
Date uploadFinanceSponsor =  AppRound.Application_Round__r.Deadline_for_Uploading_Finances_Sponsors__c;
}
I need to change the date to January 8, 2018 format.