• jayesh pagare
  • NEWBIE
  • 30 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 17
    Questions
  • 5
    Replies
below is the trigger. Bold lines not covered in test class. (code coverage is 77%) 

trigger updateAccOwner on Account (after update) {
    List<Id> ownerChangedAccountlist = new List<Id>();
    Map<Id,Id> accountownerMap = new Map<Id,Id>();
    List<custObj1__c> lstCust1 =  new List<custObj1__c>();
    List<custObj2__c> lstCust2 = new List<custObj2__c>();
  
    for(Account acc:Trigger.new)
    {
       if(acc.ownerId!=Trigger.oldmap.get(acc.Id).ownerid)
       {          
          ownerChangedAccountlist.add(acc.Id);
          accountownerMap.put(acc.Id,acc.ownerId);
       } 
    }
   list<custObj1__c> custList = [select id,ownerid,Account__c  from custObj1__c where Account__c in:ownerChangedAccountlist];
  for(custObj1__c acct:custList)
   {        
      acct.ownerid = accountownerMap.get(acct.Account__c);
      lstCust1.add(acct); 

   }    
     update lstCust1;   
           List<custObj2__c> accList = [select id,ownerid,Account__c from custObj2__c where Account__c in:ownerChangedAccountlist];
              for(custObj2__c acc : accList)
            {
               acc.OwnerId = accountownerMap.get(acc.Account__c);
               lstCust2.add(acc);

              }
        update lstCust2;
           }


Test class :

     Static testmethod void testupdateAccOwner()
     {   
                       
         Account a = new Account();
        a.Name = 'Test Account';             
        insert a;                
           
       custObj1__c acct = new custObj1__c(Account_Plan_Name__c = 'Test Account Plan'); 
        acct.Account__c = a.Id; 
        acct.OwnerId = a.OwnerId;
        insert acct; 
         
        custObj2__c acc = new custObj2__c();
        acc.Account_acc_Name__c = 'Test account acc';
        acc.custObj1__c = acct.Id;            
        acc.Account__c = a.Id;      
        insert acc;
        
        a.OwnerId = [select id from user limit 1].id;
        update a;
        
        Test.startTest();
            
            acct.OwnerId = a.OwnerId;
             update acct;
            acc.ownerId = acct.OwnerId;
            update acc;
         
        Test.stopTest();  
     }
 
Hi Friends,

My screnaio is I am using custom rollup on account to calculate sum of total amount of all child opportunities(amount field).In my case trigger is working fine if i have more that 2 opportunities and i delete one opportunity.But if the account has only one opportunity as child then even after deleting that single opportunity the total amount field on account does not showing zero amount instead showing amount of last opportunity. 

Here is my Trigger. 
trigger Account_Total_Amount on Opportunity (after delete) {
 List<Account> accList = new list<Account>();
 Set<id> accountIDs  = new set<id>();
 
 if(trigger.isDelete){
     for(Opportunity o : Trigger.old){
         accountIDs.add(o.Accountid);
     }
}

 AggregateResult[] groupedResults = [SELECT SUM(Amount), AccountId FROM opportunity where AccountId IN :AccountIDs GROUP BY AccountId ];
   system.debug('*******groupedResults **********'+groupedResults);      
   for(AggregateResult ar:groupedResults) {
        Id accid = (ID)ar.get('AccountId');          
          Decimal count = (Decimal)ar.get('expr0');       
          Account acc = new Account(Id=accid);
          acc.Total_Amount__c = count;
          accList.add(acc);
       }
      update accList;
}
 
Here is apex class:
public class SampleController {
    public Interest__c interest {get;set;}
    public Member__c member {get;set;}
    public SampleController(){
        interest = new Interest__c();
    }
    
    public void fetchMemberDetails(){
        member = [SELECT Name FROM Member__c WHERE Id = : interest.Member__c];
        interest.Member_Name__c = member.Name;
    }
}
below are two working query for getting close date on current and last fiscal year..but i want a query to get opportunity having close date two years before something like: (CloseDate = LAST_FISCAL_YEAR-1)

1]   [SELECT SUM(Amount),AccountId FROM opportunity where AccountId IN :AccountIDsAND CloseDate=LAST_FISCAL_YEAR GROUP BY AccountId]

2]   [SELECT SUM(Amount),AccountId FROM opportunity where AccountId IN :AccountIDsAND CloseDate=THIS_FISCAL_YEAR GROUP BY AccountId]
 
here is the trigger which update contact phone details on opportunity record aftter update.Please help me to write test class.
Apex Trigger:
trigger ContactAfterUpdate on Contact (after update) {
  // Update information on opportunity when Contact info is updaed.
  Set<Id> ContactIds = new Set<Id>();
  for(Contact c: Trigger.new){
    Contact oldC = Trigger.oldMap.get(c.Id);
    if(c.Fax!=oldC.Fax ||c.Phone!=oldC.Phone||c.MobilePhone!=oldC.MobilePhone){
      ContactIds.add(c.Id);
    }
  }

  if(ContactIds.size()>0){
    List<Opportunity> opps = [select Contact_Phone__c,Contact_Fax__c,Contact_Mobile__c,Contact_Name__c from Opportunity where Contact_Name__c IN :ContactIds];
    if(opps.size()>0){
      for(Opportunity o: opps){
        Contact adj = Trigger.newMap.get(o.Contact_Name__c);
        o.Contact_Phone__c = adj.Phone;
        o.Contact_Fax__c = adj.Fax;
        o.Contact_Mobile__c = adj.MobilePhone;
      } 
      update opps;
    }
  }
}

Test Class:
@isTest
private class AfterUpdateContactTest{
public static testMethod void AfterUpdateContactStaticTestMethod()
{
Account a = new Account();
a.Name = 'Test ';

insert a;
 
  Contact aa =new Contact (LastName='TestAss',Title='ASSID' ,FirstName='AssFName',MobilePhone='0919182320',Phone='02002020',Fax='xyz');
  aa.accountid=a.id;
  insert aa;

  List<Opportunity>oppList = new List<Opportunity> {new Opportunity(Name='TestUser',Contact_Name__c =aa.id,AccountId= a.id  )};   
  insert oppList;

  List<Opportunity> oppList1 = new List<Opportunity>{ [select id,Contact_Phone__c,Contact_Fax__c,Contact_Mobile__c,Contact_Name__c from Opportunity where id in :oppList ]};
  for(Opportunity ppdd:oppList1)  
    ppdd.Contact_Phone__c = aa.Phone;    

    
  update oppList1;  
  update aa;
  }
}
Urgent: I want to stored jsoncoming from javascript to my object

ARRAY :   ["red",["blue","white"],"green",["purple","grey",yellow"]]
Now Above are the options selected by user as per question.Now I want to return each option from array like for first question it would be "red" and for 2nd question it would be "blue" and "white" so how do I get these values from array?
Here is my code and currently i am successfully passing single option selected in array to apex controller but now want to pass multiple option for same question.

Javascript Code:
​<script type="text/javascript"> var ansArray = []; ansArray[0] = ""; ansArray[1] = ""; ansArray[2] = ""; ansArray[3] = ""; ansArray[4] = ""; function changeValue(input,textid,count,id) { ansArray[count] = input.value; alert(ansArray); } </script> <script> function SubmitSurvey(node){ InvokeController(JSON.stringify(ansArray)); <!--alert(JSON.stringify(ansArray));--> } </script>

VF Page:
<apex:variable var="count" value="{!0}"/> <apex:repeat value="{!Questions}" var="q"> <!-- Question style one --> <apex:outputPanel rendered="{!IF(q.Related_Question__r.Type__c='Radio',true,false)}" id="RadioPanel"> <div class="Que1"> <apex:outputField value="{!q.Related_Question__r.Question__c}"/> <div class="OptionDiv"> <apex:form > <div style="padding: 20px;"> <apex:selectRadio styleClass="Source" value="{!SelectedValue}" id="RadioButtonValue" onclick="changeValue(this,'{!$Component.RadioButtonValue}',{!count});" > <apex:selectOption itemLabel="{!q.Option_One__c}" itemValue="{!q.Option_One__c}" ></apex:selectOption> <apex:selectOption itemLabel="{!q.Option_Two__c}" itemValue="{!q.Option_Two__c}" ></apex:selectOption> <apex:selectOption itemLabel="{!q.Option_Three__c}" itemValue="{!q.Option_Three__c}" ></apex:selectOption> <apex:selectOption itemLabel="{!q.Option_Four__c}" itemValue="{!q.Option_Four__c}" ></apex:selectOption> <apex:selectOption itemLabel="{!q.Option_Five__c}" itemValue="{!q.Option_Five__c}" ></apex:selectOption> </apex:selectRadio> </div> <apex:variable value="{!count+1}" var="count"/> </apex:repeat>

 
Hi All,

I have one master table"Question" and child table "Option" now here i want to show questions and its related options( with radio button) in vf page and get option value for respective question. I am able to fetch question and option on vf page but didnt able to pass option value to apex class.Pls Help..

<apex:page Tabstyle="Account" controller="WOS_WrapperExample">
   
       
 <apex:form >
     <div>
        <table>
          <tr>
              <th>Select</th>
              <th>Question</th>           
          </tr>
          
            <apex:repeat value="{!Quest_OptionList}" var="a">
              <tr> <td>{!a.acc.Name}</td> </tr>
             <apex:repeat value="{!a.acc.Option__r}" var="o">
                 <tr>  <td><apex:inputCheckbox value="{!a.selected}" id="checkedone" /></td>                 
                         <td id="theIDD" >{!o.name}</td>                 
                 </tr>  
              </apex:repeat>              
          </apex:repeat>    
        </table>        
    </div> 
 </apex:form>   
</apex:page>

Apex Class :
public class WOS_WrapperExample
{
public List<QuestionOptionWrap> Quest_OptionList {get;set;}
public List<QuestionData__c> selectedQuestions {get;set;}
    
  public WOS_WrapperExample() {
  Quest_OptionList = new list<QuestionOptionWrap>();
  selectedQuestions = new list<QuestionData__c>();
   for(QuestionData__c a : [select Id, Name,(select name from Option__r) from QuestionData__c limit 10])
      Quest_OptionList .add(new QuestionOptionWrap(a));  
  }
    public PageReference Selectedacc()
    {
        selectedQuestions.clear();
        for(QuestionOptionWrap accwrapper : Quest_OptionList )
        if(accwrapper.selected == true)
        selectedQuestions.add(accwrapper.acc);
        return null;
    }  

    public class QuestionOptionWrap
    { 
        public QuestionData__c acc{get; set;} 
        public String optionOne{get; set;}
        public QuestionOptionWrap(QuestionData__c a) // Constructor of wrapper class
        {
            acc = a;
            selected = false;    
        }
    }
}
 
I have related contacts and on account i want to get record count of contacts created this month and record count of contacts created on last month
I have field called adjuster history with record 'rocky-1,james-2,John-3'  for getting first string record  used formula as"LEFT(Cust_field__c, FIND("-", Cust_field__c)-1)" but if i removed rocky from database then adjuster history is like ',james-2,John-3' and i get result as ",James-2 " how do i remove unwanted comma from the result.??????
 
public class theControllerMyTEST {
  
    public user currentuser{get;set;}
    public string emailAddress{get;set;}
    public List<PersonInfo> lstPersonInfo{get;set;} 

    public class PersonInfo {  
        public integer g_id{get;set;}
        public string sportname{get;set;}      
        public string description{get;set;}      
        public string imageUrl{get;set;}      

        public PersonInfo(){     
          
        }  
    } 
   
// Constructor
    
    public theControllerMyTEST () {
      
       currentuser=[Select FirstName,LastName,Email from User where Id=:userinfo.getuserId()];
       emailAddress =currentuser.Email ;
       system.debug('Email Id :'+emailAddress );  
       getGames();
       
    }
  
// getGame method definition
    
  public void getGames()
    {        
          Http httpProtocol = new Http();  
          HttpRequest request = new HttpRequest();       
          String endpoint ='http://mytest.test.com/gedetails.php?id='+emailAddress;      
          request.setEndPoint(endpoint);                                                                                                         
          request.setMethod('GET');
          request.setEndpoint(endpoint);
          request.setTimeOut(60000);
          HTTPResponse response = httpProtocol.send(request);
          String jsonString = response.getBody();         
          System.debug(jsonString);           
          parseJSON(jsonString );

    }
    
// Json Parser Method
    
    public void parseJSON(String jsonstr) {  
        if (jsonstr != null){  
        lstPersonInfo= new List<PersonInfo>();     
        JSONParser parser = JSON.createParser(jsonstr);
       
        while (parser.nextToken() != null)  
        {  
            if (parser.getCurrentToken() == JSONToken.START_ARRAY)  
            {  
                while (parser.nextToken()!=  JSONToken.END_ARRAY)  
                {  
                    if (parser.getCurrentToken() == JSONToken.START_OBJECT)  
                    {                            
                        PersonInfo ci = new PersonInfo();  
                        while (parser.nextToken() !=  JSONToken.END_OBJECT)  
                    {                                                     
                                                                                               
                          if((parser.getCurrentToken() ==JSONToken.FIELD_NAME)&&(parser.getText().tolowercase()== 'id'))  
                                {   
                                    parser.nextToken();                                                                   
                                    ci.g_id = integer.valueof( parser.getText()); 
                                    system.debug('ID OF CELERY: '+ci.g_id);
                                }           
                         
                        if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'description'))  
                                {  
                                    parser.nextToken();  
                                    ci.description= parser.getText();  
                                                       
                                }                                            
                                                        
                        }  
                        lstPersonInfo.add(ci);  
                    }  
                }  
            }  
        }  
      }     
    }  
 
    
}
I have field called adjuster history with record 'rocky-1,james-2,John-3'  for getting first string record  used formula as"LEFT(Cust_field__c, FIND("-", Cust_field__c)-1)" but if i removed rocky from database then adjuster history is like ',james-2,John-3' and i get result as ",James-2 " how do i remove unwanted comma from the result.??????
 
below is the trigger. Bold lines not covered in test class. (code coverage is 77%) 

trigger updateAccOwner on Account (after update) {
    List<Id> ownerChangedAccountlist = new List<Id>();
    Map<Id,Id> accountownerMap = new Map<Id,Id>();
    List<custObj1__c> lstCust1 =  new List<custObj1__c>();
    List<custObj2__c> lstCust2 = new List<custObj2__c>();
  
    for(Account acc:Trigger.new)
    {
       if(acc.ownerId!=Trigger.oldmap.get(acc.Id).ownerid)
       {          
          ownerChangedAccountlist.add(acc.Id);
          accountownerMap.put(acc.Id,acc.ownerId);
       } 
    }
   list<custObj1__c> custList = [select id,ownerid,Account__c  from custObj1__c where Account__c in:ownerChangedAccountlist];
  for(custObj1__c acct:custList)
   {        
      acct.ownerid = accountownerMap.get(acct.Account__c);
      lstCust1.add(acct); 

   }    
     update lstCust1;   
           List<custObj2__c> accList = [select id,ownerid,Account__c from custObj2__c where Account__c in:ownerChangedAccountlist];
              for(custObj2__c acc : accList)
            {
               acc.OwnerId = accountownerMap.get(acc.Account__c);
               lstCust2.add(acc);

              }
        update lstCust2;
           }


Test class :

     Static testmethod void testupdateAccOwner()
     {   
                       
         Account a = new Account();
        a.Name = 'Test Account';             
        insert a;                
           
       custObj1__c acct = new custObj1__c(Account_Plan_Name__c = 'Test Account Plan'); 
        acct.Account__c = a.Id; 
        acct.OwnerId = a.OwnerId;
        insert acct; 
         
        custObj2__c acc = new custObj2__c();
        acc.Account_acc_Name__c = 'Test account acc';
        acc.custObj1__c = acct.Id;            
        acc.Account__c = a.Id;      
        insert acc;
        
        a.OwnerId = [select id from user limit 1].id;
        update a;
        
        Test.startTest();
            
            acct.OwnerId = a.OwnerId;
             update acct;
            acc.ownerId = acct.OwnerId;
            update acc;
         
        Test.stopTest();  
     }
 
Hi Friends,

My screnaio is I am using custom rollup on account to calculate sum of total amount of all child opportunities(amount field).In my case trigger is working fine if i have more that 2 opportunities and i delete one opportunity.But if the account has only one opportunity as child then even after deleting that single opportunity the total amount field on account does not showing zero amount instead showing amount of last opportunity. 

Here is my Trigger. 
trigger Account_Total_Amount on Opportunity (after delete) {
 List<Account> accList = new list<Account>();
 Set<id> accountIDs  = new set<id>();
 
 if(trigger.isDelete){
     for(Opportunity o : Trigger.old){
         accountIDs.add(o.Accountid);
     }
}

 AggregateResult[] groupedResults = [SELECT SUM(Amount), AccountId FROM opportunity where AccountId IN :AccountIDs GROUP BY AccountId ];
   system.debug('*******groupedResults **********'+groupedResults);      
   for(AggregateResult ar:groupedResults) {
        Id accid = (ID)ar.get('AccountId');          
          Decimal count = (Decimal)ar.get('expr0');       
          Account acc = new Account(Id=accid);
          acc.Total_Amount__c = count;
          accList.add(acc);
       }
      update accList;
}
 
below are two working query for getting close date on current and last fiscal year..but i want a query to get opportunity having close date two years before something like: (CloseDate = LAST_FISCAL_YEAR-1)

1]   [SELECT SUM(Amount),AccountId FROM opportunity where AccountId IN :AccountIDsAND CloseDate=LAST_FISCAL_YEAR GROUP BY AccountId]

2]   [SELECT SUM(Amount),AccountId FROM opportunity where AccountId IN :AccountIDsAND CloseDate=THIS_FISCAL_YEAR GROUP BY AccountId]
 
here is the trigger which update contact phone details on opportunity record aftter update.Please help me to write test class.
Apex Trigger:
trigger ContactAfterUpdate on Contact (after update) {
  // Update information on opportunity when Contact info is updaed.
  Set<Id> ContactIds = new Set<Id>();
  for(Contact c: Trigger.new){
    Contact oldC = Trigger.oldMap.get(c.Id);
    if(c.Fax!=oldC.Fax ||c.Phone!=oldC.Phone||c.MobilePhone!=oldC.MobilePhone){
      ContactIds.add(c.Id);
    }
  }

  if(ContactIds.size()>0){
    List<Opportunity> opps = [select Contact_Phone__c,Contact_Fax__c,Contact_Mobile__c,Contact_Name__c from Opportunity where Contact_Name__c IN :ContactIds];
    if(opps.size()>0){
      for(Opportunity o: opps){
        Contact adj = Trigger.newMap.get(o.Contact_Name__c);
        o.Contact_Phone__c = adj.Phone;
        o.Contact_Fax__c = adj.Fax;
        o.Contact_Mobile__c = adj.MobilePhone;
      } 
      update opps;
    }
  }
}

Test Class:
@isTest
private class AfterUpdateContactTest{
public static testMethod void AfterUpdateContactStaticTestMethod()
{
Account a = new Account();
a.Name = 'Test ';

insert a;
 
  Contact aa =new Contact (LastName='TestAss',Title='ASSID' ,FirstName='AssFName',MobilePhone='0919182320',Phone='02002020',Fax='xyz');
  aa.accountid=a.id;
  insert aa;

  List<Opportunity>oppList = new List<Opportunity> {new Opportunity(Name='TestUser',Contact_Name__c =aa.id,AccountId= a.id  )};   
  insert oppList;

  List<Opportunity> oppList1 = new List<Opportunity>{ [select id,Contact_Phone__c,Contact_Fax__c,Contact_Mobile__c,Contact_Name__c from Opportunity where id in :oppList ]};
  for(Opportunity ppdd:oppList1)  
    ppdd.Contact_Phone__c = aa.Phone;    

    
  update oppList1;  
  update aa;
  }
}
I have related contacts and on account i want to get record count of contacts created this month and record count of contacts created on last month