• Ashish Dixit 2
  • NEWBIE
  • -2 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
Hi Everybody,

I am new in test class and i want test class for my below class, could you please help me?Thanks a lot.
public without sharing class PMOTriggerHandlers {

    /** BeforeInsertHandler
     * @author: Martin Gardner
     * @date: 23/03/2015
     * @description: Before Insert handler on PMO Object
     * @variables: Definition of variables (optional)
     * @version: 1.0 - created from IC_PMOTrigger
     */
    public class BeforeInsertHandler implements Triggers.HandlerInterface{

        public void handle() {
        
        if(checkPMOcount()){
            System.debug(LoggingLevel.INFO, 'Pre insert handling ' + Trigger.new );
            //MG cDecisions 04/11/2014 removing the update of PMO stage to opportunity
            Set<Id> setOppIds = new Set<Id>();
            //MG cDecisions //public List<Opportunity> ListOfOppToUpdate = new List<Opportunity>();
            Map<Id,Opportunity> mapOfOpportunity ;
                
            //retrieve the ListOfOpportunityIds
            for(PMO__c rec : (List<PMO__c>)trigger.new){
                System.debug('*****PMO ID****'+rec.JJ_Opportunity__c);
                if(rec.JJ_Opportunity__c!=NULL){
                    setOppIds.add(rec.JJ_Opportunity__c);
                }
            }

            //map to store the parent Opportunities for all PMOs being inserted or updated
            //mapOfOpportunity = new Map<Id,Opportunity>([Select Id,Name,JJ_Share_Access__c,JJ_PMO_R_D_Stage__c FROM Opportunity WHERE Id IN :setOppIds]);
            mapOfOpportunity = new Map<Id,Opportunity>( [SELECT Id,Name,JJ_Share_Access__c FROM Opportunity WHERE Id IN :setOppIds] );
            System.debug('************'+mapOfOpportunity);
            if(mapOfOpportunity!=NULL){
                for(PMO__c rec : (List<PMO__c>)trigger.new){                
                    /**********************Sets the access level on the PMO based on the parent Opportunity***************************/
                    if( rec.JJ_Opportunity__c != NULL && 
                        mapOfOpportunity.containsKey(rec.JJ_Opportunity__c) && 
                        mapOfOpportunity.get(rec.JJ_Opportunity__c).JJ_Share_Access__c!=NULL){
                        rec.JJ_Private__c = mapOfOpportunity.get(rec.JJ_Opportunity__c).JJ_Share_Access__c.equalsIgnoreCase(IC_Constants__c.getInstance('Private Share').value__c)?True:False;
                    }
                }
            }
            IC_PMOSetStageDate.setRDStageDate(trigger.new);
           }             
        }  
        
        public boolean checkPMOcount(){
         Set<Id> existingOppIds=new Set<Id>();
         Boolean Status = false;
         PMOModuleSecurity.updateAccess(trigger.new);
         existingOppIds =  PMOModuleSecurity.checkNoOfPMOModule(trigger.new);
         if(existingOppIds.size()>0){                  
            for(PMO__c rec : (List<PMO__c>)trigger.new){                
                if(existingOppIds.contains(rec.JJ_Opportunity__c)){
                    rec.addError('Another PMO module already exists for this opportunity. Only 1 PMO module is allowed per opportunity.');
                    Status=false;
                }
                else{
                   status=true;
                }
            }
         }
         return status; 
    }     
 }
    
    /** AfterInsertHandler
     * @author: Martin Gardner
     * @date: 23/03/2015
     * @description: After Insert handler on PMO Object
     * @variables: Definition of variables (optional)
     * @version: 1.0 - created from IC_PMOTrigger
     */
    public class AfterInsertHandler implements Triggers.HandlerInterface{

        public void handle() {
            System.debug(LoggingLevel.INFO, 'Post insert handling ' + Trigger.new );
            new AccessManager().grantAccess((List<PMO__c>)Trigger.new);
            PMOTriggerHandlers.processNewOrUpdatedPMO((List<PMO__c>)Trigger.new);
        }       
    }
    /** BeforeUpdateHandler
     * @author: Martin Gardner
     * @date: 23/03/2015
     * @description: Before Update handler on PMO Object
     * @variables: Definition of variables (optional)
     * @version: 1.0 - created from IC_PMOTrigger
     */
    public class BeforeUpdateHandler implements Triggers.HandlerInterface{

        public void handle() {
            System.debug(LoggingLevel.INFO, 'Pre update handling ' + Trigger.new );
            IC_PMOSetStageDate.setRDStageDate((List<PMO__c>)trigger.new);
        }       
        
    }   
    /** AfterUpdateHandler
     * @author: Martin Gardner
     * @date: 23/03/2015
     * @description: After Update handler on PMO Object
     * @variables: Definition of variables (optional)
     * @version: 1.0 - created from IC_PMOTrigger
     */
    public class AfterUpdateHandler implements Triggers.HandlerInterface{

        public void handle() {
            System.debug(LoggingLevel.INFO, 'Post update handling ' + Trigger.new );
            PMOTriggerHandlers.processNewOrUpdatedPMO((List<PMO__c>)Trigger.new);
        }       
        
    }   

    /** BeforeDeleteHandler
     * @author: Martin Gardner
     * @date: 23/03/2015
     * @description: Before delete handler on PMO Object
     * @variables: Definition of variables (optional)
     * @version: 1.0 - created from IC_PMOTrigger
     * @version: 1.1 - Changed name to BeforeDeleteHandler from AfterDeleteHandler
     */
    public class BeforeDeleteHandler implements Triggers.HandlerInterface{

        public void handle() {
            System.debug(LoggingLevel.INFO, 'Post delete handling ' + Trigger.old );
            PMOTriggerHandlers.processDeletedPMO((List<PMO__c>)Trigger.old);
        }       
        
    }

    /** processNewOrUpdatedPMO
     * @author Martin Gardner
     * @description Processes any new or updated PMO records 
     * @param list<PMO__c> records : the new or updated PMO records to process.
     * @return void
     *
     * @author: Hamayoun Khan
     * @date: 04/28/2015
     * @version: 2.0 - added logic to include Score_Team__c object for Alliance Manager
     */
    private static void processNewOrUpdatedPMO(list<PMO__c> records){
            //create list to hold updating record in 
            Set<Score__c> scoresToUpdate = new Set<Score__c>();
            //create a set of opp ids
            Set<Id> oppIds = new Set<Id>();
        Set <Id> pmoIds = new Set <Id> () ;
            Map<Id, PMO__c> mapPMOByOpp = new Map<Id, PMO__c>(); /** Removes duplicates by Opp Id **/
            for(PMO__c rec: (List<PMO__c>)records){
                oppIds.add(rec.JJ_Opportunity__c);
                mapPMOByOpp.put(rec.JJ_Opportunity__c, rec);
                pmoIds.add(rec.Id);
            }
            System.debug(LoggingLevel.INFO, 'processNewOrUpdatedPMO : Opp Ids ' + oppIds);
            System.debug(LoggingLevel.INFO, 'processNewOrUpdatedPMO : mapPMOByOpp ' + mapPMOByOpp);
            //Create a map of Opp Id to score
            Map<Id, Score__c> mapScores = new Map<Id, Score__c>([SELECT Id, IC_Opportunity__c  
                                                                FROM Score__c 
                                                                WHERE IC_Opportunity__c in :oppIds]);
            System.debug(LoggingLevel.INFO, 'processNewOrUpdatedPMO : mapScores ' + mapScores);
            for(Score__c rec:mapScores.values()){
                rec.Post_Deal_Execution__c = mapPMOByOpp.get(rec.IC_Opportunity__c).Project_Event_Outcome__c;
                scoresToUpdate.add(rec);
            }
            //update scores 
            list<Score__c> result = new List<Score__c>(scoresToUpdate);
            update  result;

        // Now handle Score_Teams
        if (Trigger.isUpdate) {
        // before inserting, delete all STs associated with PMO being updated
            List<Score_Team__c> sts = [select Id from Score_Team__c where PMO__c in :pmoIds] ;
            delete (sts) ;
        }
        // Add new Score_Team rec for all relevant Scores
        List<Score_Team__c> stsToAdd = new List<Score_Team__c>() ;
            for(Score__c rec:mapScores.values()){
                String am = mapPMOByOpp.get(rec.IC_Opportunity__c).JJ_IC_Alliance_Manager__c;
        if (am != null) {
            String pmoId = mapPMOByOpp.get(rec.IC_Opportunity__c).Id;
            Score_Team__c st = new Score_Team__c (Score__c=rec.Id, Role__c='Alliance Manager', User__c=am, Opportunity_Type__c='IC', PMO__c=pmoId);
            stsToAdd.add(st) ;
        }
            }
        insert stsToAdd;
    }

    /** processDeletedPMO
     * @author Martin Gardner
     * @description Processes any new or updated PMO records 
     * @param list<PMO__c> newRecords : the new or updated PMO records to process.
     * @return List<Score__c>
     *
     * @author: Hamayoun Khan
     * @date: 04/28/2015
     * @version: 2.0 - added logic to include Score_Team__c object for Alliance Manager
     */
@TestVisible private static void processDeletedPMO(list<PMO__c> records){
        // Update any Score__c associated with the Opportunity associated with the PMOs being deleted
            Set<Score__c> scoresToUpdate = new Set<Score__c>();
            //create a set of opp ids
            Set<Id> oppIds = new Set<Id>();
        Set <Id> pmoIds = new Set <Id> () ;
            for(PMO__c rec: (List<PMO__c>)records){
                oppIds.add(rec.JJ_Opportunity__c);
                pmoIds.add(rec.Id);
            }
            //Get a map of the scores that need to be updated.
            Map<Id, Score__c> mapScores = new Map<Id, Score__c>([SELECT Id, IC_Opportunity__c 
                                                                FROM Score__c 
                                                                WHERE IC_Opportunity__c in :oppIds]);

            //Logic to run on deletion of PMO object
            for (Score__c s: mapScores.values()) {
                //update Post_Deal_Execution__c and add to set (in case multiple scores existed for same PMO object)   
                s.Post_Deal_Execution__c = null;
                scoresToUpdate.add(s);                           
            }
            //update scores 
            list<Score__c> result = new List<Score__c>(scoresToUpdate);
            update  result;

        // Delete all Score_Team__c for Scores
        List<Score_Team__c> sts = [select Id from Score_Team__c where PMO__c in :pmoIds] ;
        delete (sts) ;
    }               
}

Thanks & regards,
Ashish 
Hi All,

i am new in apex code, I write a trigger and dont have idea about the handler class so i directly write logic in trigger itself. it would be great if anybody let me know how to write handler class and call below trigger code. 
trigger PMO_Trigger on PMO__c (before insert,before update,after Insert){
    
    public static Set<Id> existingOppIds=new Set<Id>();
    public List<PMO__c> fmiList = new List<PMO__c>();
    if(Trigger.isBefore && Trigger.isInsert){
    /*********************Implement different access to the different users**************/
        PMO_ModuleSecurity.UpdateAccess(trigger.new);
     /**********************Limit the creation of PMO Module to only 1 module per opportunity******/ 
        existingOppIds =  PMO_ModuleSecurity.CheckNoOfPMOModule(trigger.new);
        if(existingOppIds.size()>0){                  
            for(Integer i = 0; i < Trigger.new.size(); i++){                
                if(existingOppIds.contains(Trigger.new[i].JJ_Opportunity__c)){
                  //check for existing opportunity 
                    Trigger.new[i].addError('Another PMO module already exists for this opportunity. Only 1 PMO module is allowed per opportunity.');
                }
                else{
                   // existingOppIds.add( Trigger.new[i].opportunity__c);
                }
            }
        }        
    }    
    /******************************* Grant PMOModule Access ******************************/
    if(  Trigger.isInsert && Trigger.isAfter){
        new AccessManager().grantAccess(Trigger.new);
    }     
    if(Trigger.isBefore && Trigger.isUpdate){
    /**********************Limit the creation of PMO Module to only 1 module per opportunity******/ 
        for(PMO__c fmi : Trigger.new){
            PMO__c oldOpportunity = Trigger.oldMap.get(fmi.id);
            if(fmi.JJ_Opportunity__c != oldOpportunity.JJ_Opportunity__c ){
                fmiList.add(fmi);
            }
        }
        if(fmiList.size()>0){
            existingOppIds =  PMO_ModuleSecurity.CheckNoOfPMOModule(trigger.new);
            if(existingOppIds.size()>0){
               for(Integer i = 0; i < Trigger.new.size(); i++){                
                    if(existingOppIds.contains(Trigger.new[i].JJ_Opportunity__c)){
                    //check for existing opportunity 
                    Trigger.new[i].addError('Another PMO module already exists for this opportunity. Only 1 PMO module is allowed per opportunity.');
                    }
                    else{
                        existingOppIds.add( Trigger.new[i].JJ_Opportunity__c);
                    }
                }
            }
        }        
    }
}
 
When and how is the Amount Used  column in the Usage-based entitlements updated? I have a Customer Community Plus Login license and I assigned a user to that license. After that, I logged in. I checked the company information page, Customer Community Plus login license is now less than 1. When I checked the Usage-based entitlements, neither the Power Customer Community Daily Unique Logins or Power Customer Community Logins has a value in the Amount Used.
I thought that when I logged in using the user i just created, there will be a value on the Amount used.
Hi All,

i am new in apex code, I write a trigger and dont have idea about the handler class so i directly write logic in trigger itself. it would be great if anybody let me know how to write handler class and call below trigger code. 
trigger PMO_Trigger on PMO__c (before insert,before update,after Insert){
    
    public static Set<Id> existingOppIds=new Set<Id>();
    public List<PMO__c> fmiList = new List<PMO__c>();
    if(Trigger.isBefore && Trigger.isInsert){
    /*********************Implement different access to the different users**************/
        PMO_ModuleSecurity.UpdateAccess(trigger.new);
     /**********************Limit the creation of PMO Module to only 1 module per opportunity******/ 
        existingOppIds =  PMO_ModuleSecurity.CheckNoOfPMOModule(trigger.new);
        if(existingOppIds.size()>0){                  
            for(Integer i = 0; i < Trigger.new.size(); i++){                
                if(existingOppIds.contains(Trigger.new[i].JJ_Opportunity__c)){
                  //check for existing opportunity 
                    Trigger.new[i].addError('Another PMO module already exists for this opportunity. Only 1 PMO module is allowed per opportunity.');
                }
                else{
                   // existingOppIds.add( Trigger.new[i].opportunity__c);
                }
            }
        }        
    }    
    /******************************* Grant PMOModule Access ******************************/
    if(  Trigger.isInsert && Trigger.isAfter){
        new AccessManager().grantAccess(Trigger.new);
    }     
    if(Trigger.isBefore && Trigger.isUpdate){
    /**********************Limit the creation of PMO Module to only 1 module per opportunity******/ 
        for(PMO__c fmi : Trigger.new){
            PMO__c oldOpportunity = Trigger.oldMap.get(fmi.id);
            if(fmi.JJ_Opportunity__c != oldOpportunity.JJ_Opportunity__c ){
                fmiList.add(fmi);
            }
        }
        if(fmiList.size()>0){
            existingOppIds =  PMO_ModuleSecurity.CheckNoOfPMOModule(trigger.new);
            if(existingOppIds.size()>0){
               for(Integer i = 0; i < Trigger.new.size(); i++){                
                    if(existingOppIds.contains(Trigger.new[i].JJ_Opportunity__c)){
                    //check for existing opportunity 
                    Trigger.new[i].addError('Another PMO module already exists for this opportunity. Only 1 PMO module is allowed per opportunity.');
                    }
                    else{
                        existingOppIds.add( Trigger.new[i].JJ_Opportunity__c);
                    }
                }
            }
        }        
    }
}
 
Hi All,

i am new in apex code i write a trigger and dont have idea about the handler class so i directly write logic in trigger itself. it would be great if anybody let me know how to write handler class and call this trigger code. 
trigger Lookupuserrecord on Account (before insert, before update) {
        
        Set<Id> ParentIds = New Set<Id>();
        For(Account Acc : Trigger.New)
    {
        ParentIds.Add(Acc.Master__c);
    }
       List<Master__c> ParentList = [Select Id,test_user1__c from Master__c where id =: ParentIds];
       
       For(Account CO : Trigger.New)
    {    
        For(Master__c PA : ParentList)
        {
            IF(CO.Master__c == PA.ID)
            {
                IF(PA.test_user1__c != NULL)
                {
                    CO.test_user1__c = PA.test_user1__c ;
                }
            }
        }
    }    
       
        
}