• Dhanesh Subramanian
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Developer
  • TCS

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 0
    Replies
I have a Sturdy(Custom Object) related list under standard Account object, i do want to make the New button of the Sturdy related list to be displayed only if the Account status is Active?
My Controller code: 
public with sharing class retailprogramExtensionNew
{
    
    public Contact[] Availableprograms {get;set;}
    public program__c[] shoppingCart{get; set;}
    public retail__c theretail {get; set;}
    public String tonSelect{get; set;}
    public String toUnselect{get; set;}
    public String searchString{get; set;}
    public Boolean AccountRT{get; set;}
    public Boolean ContactRT{get; set;}
    public Boolean LeadRT{get; set;}
    public Boolean OpportunityRT{get; set;}
    public String UserID {get; set;}
    
    private program__c[] forDeletion = new program__c[]{};
    private ApexPages.StandardController controller;
    
    //Constructor
    
    public retailprogramExtensionNew (ApexPages.StandardController controller)
    {
        this.controller= controller;
        if(controller.getrecord() == null)
        system.debug(controller.getrecord().id);
        
        //UserID = UserInfo.getname(); 
        
        //retailing Custom Setting object for Recordtypes in retail object
        
        Record_Type_Name__c Accrt = Record_Type_Name__c.getValues('AccRecordTypeID');
        Record_Type_Name__c Conrt = Record_Type_Name__c.getValues('ConRecordTypeID');
        Record_Type_Name__c Leart = Record_Type_Name__c.getValues('LeaRecordTypeID');
        Record_Type_Name__c Opprt = Record_Type_Name__c.getValues('OppRecordTypeID');
        
        //Condition to check the record type for retail object 
        
        if(ApexPages.currentPage().getParameters().get('RecordType') == Accrt.Record_Type__c){
            AccountRT = TRUE;
        }
        if(ApexPages.currentPage().getParameters().get('RecordType') == Conrt.Record_Type__c){
            ContactRT = TRUE;
        }
        if(ApexPages.currentPage().getParameters().get('RecordType') == Leart.Record_Type__c){
            LeadRT = TRUE;
        }
        if(ApexPages.currentPage().getParameters().get('RecordType') == Opprt.Record_Type__c){
            OpportunityRT = TRUE;
        }
       
      { 
            
            shoppingCart = [Select id,name,retailId__c,Contact_Id__c,retail_Date__c,program__c.Contact_Id__r.Name from program__c where id =: tonSelect]; 
            
       }
       updateAvailableList();
     }
      
    public void updateAvailableList() 
    {
      
        UserID = UserInfo.getUserId(); 
        String qString =  'select id, Name, Title, Contact.MailingCity,Contact.MailingState,Contact.Account.Name from Contact  where User_Id__c not in (select ID from User where id =: UserID )' ;
        system.debug(qString);
        
        if(searchString!=null)
        
        {          
            qString+= ' and ( Contact.Name like \'%' + searchString + '%\' or Contact.RACFID__c like \'%' + searchString + '%\' or Contact.Officer_Code__c like \'%' + searchString + '%\') ';                       
        }
        
       Set<Id> selectedEntries = new Set<Id>();
       if(tonSelect!=null)
        for(program__c d : shoppingCart){
            selectedEntries.add(d.Contact_Id__c);
        }
        
        if(selectedEntries.size()>0){
            String tempFilter = ' and id not in (';
            for(id i : selectedEntries){
                tempFilter+= '\'' + (String)i + '\',';
            }
            String extraFilter = tempFilter.substring(0,tempFilter.length()-1);
            extraFilter+= ')';
            
            qString+= extraFilter;
        } 
        
        qString+= ' order by Name';
        qString+= ' limit 12';
        system.debug('qString:' +qString );               
        Availableprograms = database.query(qString);
        system.debug(Availableprograms);
        
    } 
    
    public void addToShoppingCart()
    
    // This function runs when a user hits "select" button next to a program
    
    { 
      for(Contact part : Availableprograms)
       {
        if((String)part.id==tonSelect)
            {
                 
                shoppingCart.add(new program__c (Contact_Id__c =part.id));
                system.debug(shoppingCart);
                system.debug(shoppingCart.size());
                break;
                
            }          
              
        }
         updateAvailableList();
             
    }  
    
      public PageReference removeFromShoppingCart(){
    
        // This function runs when a user hits "remove" on "Selected program" section
    
        Integer count = 0;
    
        for(program__c del : shoppingCart){
            if((String)del.Contact_Id__c==toUnselect){
            
                if(del.Id!=null)
                    forDeletion.add(del);
            
                shoppingCart.remove(count);
                break;
            }
            count++;
        }
        
        updateAvailableList();
        
        return null;
    }
    
     // This function runs when user hits save button
     
     public PageReference onSave(){
             
        try{
            
            PageReference pageRef = controller.save();
            system.debug(controller.getrecord().id);
                        
                if(shoppingCart.size()>0) 
                  
                  for (program__c partmember : shoppingCart ){
                     partmember.retailId__c=controller.getrecord().id;
                     System.debug(partmember.retailId__c);
                  }
                  
                  System.debug('size' +shoppingCart.size());
                  insert(shoppingCart);
                }
           
        
            catch(Exception e){
            ApexPages.addMessages(e);
            return null;
        }  
           System.debug('completed');

        // After save return the user to the retail
       return new PageReference('/' + controller.getrecord().id);  
    }     
}

My Test class
 
@istest(seealldata=true)
Public class Test_retailprogramExtensionNew
{
    Public static testmethod void retailprogramExtensionNew_test()
    {
        Record_Type_Name__c opprt =[select  ID,name,Record_Type__c from Record_Type_Name__c where name ='OppRecordTypeID']; 
        
        Opportunity opp1 = new Opportunity(name='test1', Product_Category__c='ATM', Product__c='ATM',Private__c=true, Private_Code_Name__c='test1',CloseDate=system.today(), StageName='Pursue');
        insert opp1;
        
        contact con = new contact(LastName='Mr. Dhanesh Subramanian',RACFID__c='HINDF', Officer_Code__c='HINDF',Contact_Type__c='Key Employee',Salesforce_User__c=true, );
        insert con;
        
        contact con1 = new contact(LastName='Vidhyasagaran Muralidharan',RACFID__c='MURALVI',Officer_Code__c='HINDF1',Contact_Type__c='Key Employee',Salesforce_User__c=true );
        insert con1;
         
        Test.setCurrentPageReference(new PageReference('Page.retailCustomNew'));
        System.currentPageReference().getParameters().put('id',opprt.id);
        
        retailprogramExtensionNew e = new  retailprogramExtensionNew(new ApexPages.StandardController(opprt));
        
        retail__c  retail = new retail__c();
        retail.Subject__c = 'Test';
        retail.retail_Type__c = 'BD Monthly Update';
        retail.retail_Date__c = system.today();
        retail.Is_Private__c = TRUE;
        retail.Status__c = 'DONE';
        retail.Comments__c = 'sample test for test class';
        retail.recordtypeId = opprt.id;
        insert retail; 
          
        e.searchString ='Mr. Dhanesh Subramanian';
        e.searchString ='SUBRAD2';
        e.searchString ='HINDF';
        e.searchstring ='';
        e.updateAvailableList();  
        e.addToShoppingCart();
        e.removeFromShoppingCart();
        e.onSave();        
        
        test.startTest();
        program__c  part = new program__c  ();
        part.Contact_Id__c = con.id;
        part.retailId__c = retail.id; 
        insert part; 
        test.stopTest();   
    }    
}

 
I have a Call master object and Participant as Detail object. Whenever a call gets created the call owner(user lookup) should be added as a Participant record. Participant name is (Contact Lookup). I used the below trigger, its hitting the governor limits. Since i have used "Select statement" in For loop. Is there any workaround. I do have to map the User name with the Contact name based on the owner id.

trigger CallOwnerAsParticipant on Call__c (after Insert)
{
    List<Participant__c> addparticipant = new List<Participant__c>();
    Map<Id,user> usermap =new Map<Id,user>([Select Id,Name from user]);
    for(Call__c call: trigger.new)
    {
        if(call.OwnerId != null)
        {
            user us = usermap.get(call.Ownerid);
            Contact con = [Select Id, Name from Contact where Name =: us.Name];
            Participant__c  part = new Participant__c();
            part.CallId__c = call.Id;
            part.Contact_Id__c = con.Id;
            addparticipant.add(part);
        }
    }
    if(addparticipant .size()>0)
    {
        database.insert(addparticipant);
    }


 
test class for merge delete
I have my trigger to capture the deleted record during merge and insert into new custom obj. I have a test class for it, but its code coverage is only 46%? how this can be increased?

trigger MergeAccounts on Account (after delete) 
{
    if(trigger.isafter && trigger.isdelete)
    {
        List<Account_Audit__c> deletedaccount = new List<Account_Audit__c>();
    
        for (Account acct:trigger.old)
        {
            If(acct.masterRecordId!=null)
            {
                Account_Audit__c Aaudit = new Account_Audit__c();
                Aaudit.Losing_Account_Name__c= acct.name;
                Aaudit.Losing_Owner__c=acct.OwnerId;
                Aaudit.Merge_date_time__c=date.today();
                Aaudit.Merged_by_user__c=UserInfo.getName() ;
                deletedaccount.add(Aaudit);
            }
        }
        if(deletedaccount.size()>0)
        {
            database.insert(deletedaccount);
        }
    }


testclass

@istest
Public class MergeAccountstest
{
    public static  testMethod void mergetest() 
    {
        Account a = new Account(name='test');
        insert a;
        
        delete a;
    }
    public static  testMethod void Auditmergetest() 
    {
        Account_Audit__c b = new Account_Audit__c(
        Losing_Account_Name__c = 'test',
        Losing_Owner__c = '005q00000012y6X',
        Merge_date_time__c = date.today(),
        Merged_by_user__c = UserInfo.getName());
        
        insert b;
    }
}
I do want to delete the feed automatically and immediately, if the feed is posted from particular salesforce user?