• harish reddy 50
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies
I have written a test class for trigger which auto creates contact based on multipicklist values on account,But problem is how to give multipicklist values in test data .

Here is my code:

@isTest
Private class AutoCreateContactTest 
{     Private Static String Multivalue = 'first;second';
      Private Static Void CreateTestData()
      {
      
       
       List<string>multilist1 = Multivalue.split(';');
          Account acc = New Account();
          acc.name = 'hcl';
          insert acc;
      List<contact> con = new List<contact>();    
          for(string values :multilist1)
          {
              contact conn = new contact();
              conn.lastname = values;
              conn.accountid = acc.id;
              con.add(conn);          
          
          }
        INSERT con;
          
      }  
      
      Private Static  testMethod  void  autocreateTest()
      {
      
           CreateTestData();
          List <Contact> conn1 = [select lastname from contact where id = :acc.id ];
           
           System.AssertsEquals(conn1,multilist1);
           
      
      }
      
     }



 
Below trigger counts no of contacts and updates on noofcontacts__c on account .Now i need to write testclass for this 
As i am new to apex can someone help me with this .
Thanks in advance.


CODE:
trigger countofcontacttrigger on Contact (after insert) {

LIST<ID> ids=new LIST<ID>();

for(contact conn:trigger.new){
    

     ids.add(conn.accountid);
}

LIST<ACCOUNT> acc=[SELECT noofcontacts__c,(SELECT id from contacts) from account where id in:ids];
LIST<ACCOUNT> acc1=new LIST<ACCOUNT>();
for(account acc2:acc){

    acc2.noofcontacts__c=acc2.contacts.size();
    acc1.add(acc2);


}
update acc1;

}
Hi,
     I have two objects one is standard object(contact) and coustom(Payment__c) and thay have lookup from Payment to contacts .My requirement when ever payment record is created corresponding confirmpaymnt checkbox on contact should be tickked.
I have tried trigger but functionality not working can any one help me with this.
Thanks in Advance.

TRIGGER:

rigger checkboTrigger on Payment__c(after Insert) {

List<id> ids = new List<id>();
List<contact> conn3 = new List<contact>();

for(Payment__c pay: Trigger.new)
{
    ids.add(pay.id);
}

List<contact> conn2 = [SELECT id ,PaymentConfimed__c from contact  where id in: ids];

  for(contact conn :conn2)
  {
  
      conn.PaymentConfimed__c = true;
         conn3.add(conn);
  }
    update conn3; 
my scenario: i have multipicklist field multivaue__c (first,second,third,fourth,fifth) on account when ever i select some values in picklist field then corresponding checkbox field on case with same names first__c,second__c...,should be checked,As account and case have lookup relation i have written trigger on case
 
problem:how should i check each multi picklist value in if condition
Here is my code:

trigger boxTrigger on case (before insert) {

     LIST<id> ids = new LIST<id>();
     
     LIST<case> cse=new lIST<case>();
     for(case cse1:trigger.new)
     {
          ids.add(cse1.accountid);
     }
     
 List<account> acc = [select multivalue__c,(select id,first__c,second__c,third__c,fourth__c,fifth__c from cases)from account where id in:ids];
     
for(account acc1:acc)
{
   for( case cse1:acc1.cases)
   {
   
       if(acc1.multivalue__c.contains('first'))
       {
          cse1.first__c= true;
       }
        cse.add(cse1);
   }
}
update cse;
}



 
trigger avgtrigger on Contact (after insert,after update) {
LIST<id> ids=new LIST<id>();
integer value=0;
for(contact conn:trigger.new)
{
   ids.add(conn.accountid);
}
LIST<account> acc=[select avgrating__c,(SELECT id,rating__c from contacts where rating__c!=null)from account];
LIST<account> acc1=new LIST<account>();
for(account acc2:acc)
{
    for(contact con:acc.contact)
    {
         value=value+con.rating__c;
    
    }
  
    acc2.avgrating__c=value/acc2.contacts.size();//here its showing error
    acc1.add(acc);
}
update acc1;
}

can anyone help me..
thanks in advance. 
 
controller:-
public class contactsortclass {
    
    public List<Contact> contacts;
   public String sortDirection = 'ASC'; 
   public String sortExp = 'name'; 
  
   public String sortExpression 
   { 
     get 
     { 
        return sortExp; 
     } 
     set 

     { 

       if (value == sortExp) 
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC'; 
       else 
         sortDirection = 'ASC'; 
       sortExp = value; 
     } 
   } 
 public String getSortDirection() 

 { 
    

    if (sortExpression == null || sortExpression == '') 
      return 'ASC'; 
    else 
     return sortDirection; 
 } 
 public void setSortDirection(String value) 

 {   
   sortDirection = value; 

 } 
   public List<contact> getAccounts() { 
       return contacts; 
   } 

   public PageReference ViewData() { 

        
       string sortFullExp = sortExpression  + ' ' + sortDirection; 

         
       contacts = Database.query('Select id,firstname,accountname,department,mobile order by ' + sortFullExp + ' limit 1000'); 
       return null; 
   } 
    
    public  void save(){
    
    update contacts;
    }
}

apex:-


<apex:page controller="contactsortclass" tabStyle="contact"> 
<apex:sectionHeader title="contacts List with Sorting"></apex:sectionHeader> 
<apex:form > 
<apex:pageBlock title="" id="pageBlock"> 
    <apex:pageMessages ></apex:pageMessages> 
    <apex:pageBlockTable value="{!contacts}" var="a" rendered="{!NOT(ISNULL(accounts))}"> 

       <apex:column > 
         <apex:facet name="header">   
           <apex:commandLink action="{!ViewData}" value=" firstname{!IF(sortExpression=='firstname',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdSort"> 
             <apex:param value="firstname" name="column" assignTo="{!sortExpression}" ></apex:param> 

           </apex:commandLink> 
         </apex:facet> 
         <apex:outputLink value="/{!a.Id}" target="_blank">{!a.Name}</apex:outputLink> 
       </apex:column> 
       
        
    </apex:pageBlockTable> 
    <apex:pageBlockButtons location="bottom"> 
      <apex:commandButton value="save" action="{!getaccounts}" id="theButton" rerender="pageBlock"></apex:commandButton> 
      <apex:commandButton value="edit" action="{!ViewData}" id="theButton1" rerender="pageBlock"></apex:commandButton>
        <apex:commandButton value="cancel" action="{!ViewData}" id="theButton2" rerender="pageBlock"></apex:commandButton>
    </apex:pageBlockButtons>
    <apex:panelGrid columns="1" style="float:left">
      <apex:commandButton value="previous" action="{!viewdata}"/>
    </apex:panelGrid>
    <apex:panelGrid columns="1" style="float:right">
      <apex:commandButton value="next" action="{!viewdata}"/>
    </apex:panelGrid>
</apex:pageBlock> 
</apex:form> 
</apex:page>

 
Below trigger counts no of contacts and updates on noofcontacts__c on account .Now i need to write testclass for this 
As i am new to apex can someone help me with this .
Thanks in advance.


CODE:
trigger countofcontacttrigger on Contact (after insert) {

LIST<ID> ids=new LIST<ID>();

for(contact conn:trigger.new){
    

     ids.add(conn.accountid);
}

LIST<ACCOUNT> acc=[SELECT noofcontacts__c,(SELECT id from contacts) from account where id in:ids];
LIST<ACCOUNT> acc1=new LIST<ACCOUNT>();
for(account acc2:acc){

    acc2.noofcontacts__c=acc2.contacts.size();
    acc1.add(acc2);


}
update acc1;

}
my scenario: i have multipicklist field multivaue__c (first,second,third,fourth,fifth) on account when ever i select some values in picklist field then corresponding checkbox field on case with same names first__c,second__c...,should be checked,As account and case have lookup relation i have written trigger on case
 
problem:how should i check each multi picklist value in if condition
Here is my code:

trigger boxTrigger on case (before insert) {

     LIST<id> ids = new LIST<id>();
     
     LIST<case> cse=new lIST<case>();
     for(case cse1:trigger.new)
     {
          ids.add(cse1.accountid);
     }
     
 List<account> acc = [select multivalue__c,(select id,first__c,second__c,third__c,fourth__c,fifth__c from cases)from account where id in:ids];
     
for(account acc1:acc)
{
   for( case cse1:acc1.cases)
   {
   
       if(acc1.multivalue__c.contains('first'))
       {
          cse1.first__c= true;
       }
        cse.add(cse1);
   }
}
update cse;
}