• Sam Mohyee
  • NEWBIE
  • 5 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 14
    Replies

Hi,

 

I'm writing a recursive method to discover all the child accounts for a given parent account (at all levels, not just immediate children).

 

My code right now:

 

    //Recursively goes through child accounts and adds to ID_Group list.
    public static List<ID> Get_Children(ID mainID, List<ID> ID_Group){        
        for(Account child :[SELECT ID, ParentID FROM Account WHERE ParentID = :mainID]){
            if(child.ParentId == mainID){ /*IF clause to make sure SOQL returned an actual account*/
                ID_Group.add(child.id);
            	Get_Children(child.id, ID_Group);
            }
        }
return ID_Group; }

 

Basically, the function is passed a list of IDs whose first element is the 'main' or starting ID, from which we want to find all children account (for my larger program I happen to separate out the first element and pass it as a separate variable than the list itself).

 

You can see that as the for loop iterates through all the immediate children, their IDs are added to the main list, and then a recursive branch is created to go through all the children of each of those children, and so on. 

 

The issue I have right now is with the return clause - after all, isn't it true that as written this function will return an ID_Group list for EACH branch of the recursion? I wish for the function to finish all its recursive branches, add all the children IDs to the list, and THEN return the list when there are no more recursions.

 

I thought I might do that by passing a reference to the list, so the list can be populated by the method and not have to be returned afterward (ie, I would just be able to reference the list variable in another method later, and it would still be populated from this recursive method). 

 

How could I accomplish this? I understand passing by reference in APEX/java is different than in C, and may not get me what I want. 

It's easy enough to write a SOQL query to pull all the immediate child accounts of a given account. But what if I want the children's children, etc, all the way to the 'leaf' accounts (the children that have no children)?

 

Is there an existing method provided by Salesforce that would return this when given an Account ID?

 

If not, I figure recursion is the way to go, assuming Salesforce permits it. Something along the lines of this pseudocode

 

list_build_function (account ID){

   Query all children accounts;

   Put IDs in child_ID_lis

   Add IDs onto full_ID_list;

   for (each ID in child_ID_list){
         list_build_function(child ID);
}
}

 

 

 

I'm stumped!

 

The scenario: 

  • Object "ZenDesk Ticket" record is created, with a lookup field to an account, and another lookup field to a contact. 
  • The contact record didn't exist until the ZenDesk connector created the ticket record in SF, at which point it also creates the contact
  • ZenDesk should create the contact with the ticket's related Account also being the contact's related account. Instead, the contact is created as an orphan.
  • Not sure if this is relevant, bu the ZenDesk objects/fields/etc are part of a managed package on our org. 

 

 

Trigger goal:

  • Upon creation of ticket and contact, to identify the account related to the ticket, and update the related contact to also relate to this account. 

 

Method from my test class:

static testmethod void contact_with_no_account(){
    	
    Account testAccount1 = new Account(Name = 'test account',
                                       	  Type = 'Customer',
                                          Country__c = 'Albania');
        
    insert testAccount1;
                                          
    Contact testContact1 = new Contact(FirstName = 'Test',
                                           LastName = 'Contact',                                          
                                       	   City__c = 'Test city');
        
    insert testContact1;
        
    test.startTest();    
    Zendesk__Zendesk_Ticket__c testTicket1 = new Zendesk__Zendesk_Ticket__c(Zendesk__Organization__c = testAccount1.ID,
                                                                            	Zendesk__Requester__c = testContact1.ID);
    insert testTicket1;
    test.stopTest();
     
    System.assertEquals(testContact1.AccountId, testAccount1.ID);
}

 

Pretty basic - I make an account, an orphaned contact, and a ticket that relates to both of them, and insert them all. That should set off this trigger: 

 

(partial) Trigger code: 

 

trigger Zendesk_Ticket_Trigger on Zendesk__Zendesk_Ticket__c (after insert) {

    
    for (Zendesk__Zendesk_Ticket__c ticket1: [SELECT Id, Zendesk__Organization__c, Zendesk__Requester__r.AccountID FROM Zendesk__Zendesk_Ticket__c WHERE Id IN :Trigger.new
                                             AND Zendesk__Requester__r.AccountID = NULL]){
                                                                                         
        ticket1.Zendesk__Requester__r.AccountID = ticket1.Zendesk__Organization__c;                                                                                                 
        update ticket1.Zendesk__Requester__r;                                                                                               
    }

 

 

I wonder if my SOQL query is causing the problem by selecting only the tickets whose related Contact's account ID is NULL - can I fairly assume that to be true when a contact is inserted without an account specified?

 

 

The problem:

 

My system.assertEquals statement is returning the error, because the "requester" contact's account ID is null. Clearly it isn't updating after being assigned ticket1.Organization__c's ID. 

 

Hi all,

 

  • I've got a custom object ("ZenDesk Tickets") that is related via lookup fields to both the Contact and Account objects.
  • Without going into why, the related contact record doesn't have a related account (ie it's an orphan).
  • I'm writing code that will look up the account that the Ticket is related to, and relate it to the orphaned contact as well. 

 

So here's the question: if I assign the account ID of the related account to the contact's AccountID field via a related field on the ticket object, then update the *ticket* (not the contact) using DML, will that work?

 

ticket1.Zendesk__Requester__r.AccountID = ticket1.Zendesk__Organization__c;                                                                                                 

update ticket1;    

 

The Zendesk__Requester is the contact lookup field, and I'm assigning it a parent account via the related field. I feel like my error is coming from updating the ticket, when I really should be updating the contact record directly. 

Hi all,

 

I've got a simple class/trigger that updates an Account field with the number of Active Contracts. My class uses two SOQL queries:

 

public class Contract_Handler{

    public static Integer Count_Active_Contracts(Id acctId){
        /*This method is passed an Account ID, counts the related ACTIVE contracts(ie, in between start and end dates), and updates the Active_Contracts__c field on the account record. Count is also returned for other potential uses. */
        
        Account acct = [SELECT Active_Contracts__c FROM Account WHERE Id =:acctId];
        
        Integer count = [SELECT Count() FROM Contract WHERE AccountId=:acctId AND StartDate <= TODAY AND EndDate >= TODAY];

        acct.Active_Contracts__c = count;
        update acct;
        
        return count;
    }    
}

 

 

I believe the issue with having "Too many SOQL Queries" arises from the Trigger, which loops through each Contract that triggered the code:

 

trigger Contract_Triggers on Contract (after insert, after update, after delete) {

    //Iterates through affected contracts and passes Account ID to 'Count_Active_Contracts' method
    if(trigger.isDelete){
        for(Contract c: trigger.old){
            Contract_Handler.Count_Active_Contracts(c.AccountId);
        }
    }
    else{
        for(Contract c: trigger.new){
    		Contract_Handler.Count_Active_Contracts(c.AccountId);
        }
    }
    
}

 

 

The code seems to work fine when I make a change to an individual contract (makes sense right? only two queries will run). However, when I try to update all existing contracts (179 records) to cause the code to run on all of them, I get the same error for each record:

 

Contract_Triggers: System.LimitException: Too many SOQL queries: 101 Status code: 13"

 

Should I be using Batch Apex to avoid this error? I'm reading up on it now, will post new version of code if I can figure it out. In the meantime any help is appreciated.

I have a trigger on the a custom object called offer.  Within this trigger I need to verify that ther exist no OTHER offers with the same status as the current Offer.  There is only one offer, this is the current offer.  I create a map to store the results and this one offer is of course in the map.

 

How do I disregard the current record when its included in the map.

  • October 23, 2013
  • Like
  • 0

This is the schedular apex class i have created 

global class HolidayScheduledClass Implements Schedulable
            {
            public date d;
            public date d1;
            public string name;
            public integer dayint,monthint,yearint;
            public string datestring;
            
   global void execute(SchedulableContext sc)
    {
    feedchatter();
    system.debug('++++dateaangi+++++'+d);
    }
    public void feedchatter()
    {
    list<holiday>holli=[select id,Activitydate,name from holiday];
    for(holiday h:holli){
       d= h.Activitydate-3;
       d1=h.Activitydate;
       name=h.name;
       monthint=d1.month();
       dayint=d1.day();
       yearint=d1.year();
       datestring=string.valueof(monthint+'/'+dayint+'/'+yearint);
    
    }
    integer i=holli.size();
    user u=[SELECT Id,Username FROM User WHERE Email = 'support@zxc.net'];
    CollaborationGroup cg=[SELECT Id FROM CollaborationGroup WHERE Name = 'Everyone' ];
    if(d==system.today()){
     FeedItem fd = new FeedItem();
                      fd.ParentId =cg.id;
                      fd.CreatedById = u.id; 
                      fd.Body = 'Hello Everyone, we will be observing holiday on ' +datestring+' on account of '+name+'. As usual our support team will still be active during holidays. Thank you :)';
                      insert fd;
    }
  }
 }

 for which this  is the test method 

     static testMethod void validatetesthday(){
              
              Holiday hn= new holiday();
              hn.name='Test';
              hn.activitydate=date.today();
              insert hn;
              HolidayScheduledClass ahd=new HolidayScheduledClass();
              ahd.feedchatter();

 Im getting list has no rows to assign s object in the line 

CollaborationGroup cg=[SELECT Id FROM CollaborationGroup WHERE Name = 'Everyone' ];

I tried to put it inside a list and took the first stored element still same error comes up. can any one help me to write test class for this code.

I'm stumped!

 

The scenario: 

  • Object "ZenDesk Ticket" record is created, with a lookup field to an account, and another lookup field to a contact. 
  • The contact record didn't exist until the ZenDesk connector created the ticket record in SF, at which point it also creates the contact
  • ZenDesk should create the contact with the ticket's related Account also being the contact's related account. Instead, the contact is created as an orphan.
  • Not sure if this is relevant, bu the ZenDesk objects/fields/etc are part of a managed package on our org. 

 

 

Trigger goal:

  • Upon creation of ticket and contact, to identify the account related to the ticket, and update the related contact to also relate to this account. 

 

Method from my test class:

static testmethod void contact_with_no_account(){
    	
    Account testAccount1 = new Account(Name = 'test account',
                                       	  Type = 'Customer',
                                          Country__c = 'Albania');
        
    insert testAccount1;
                                          
    Contact testContact1 = new Contact(FirstName = 'Test',
                                           LastName = 'Contact',                                          
                                       	   City__c = 'Test city');
        
    insert testContact1;
        
    test.startTest();    
    Zendesk__Zendesk_Ticket__c testTicket1 = new Zendesk__Zendesk_Ticket__c(Zendesk__Organization__c = testAccount1.ID,
                                                                            	Zendesk__Requester__c = testContact1.ID);
    insert testTicket1;
    test.stopTest();
     
    System.assertEquals(testContact1.AccountId, testAccount1.ID);
}

 

Pretty basic - I make an account, an orphaned contact, and a ticket that relates to both of them, and insert them all. That should set off this trigger: 

 

(partial) Trigger code: 

 

trigger Zendesk_Ticket_Trigger on Zendesk__Zendesk_Ticket__c (after insert) {

    
    for (Zendesk__Zendesk_Ticket__c ticket1: [SELECT Id, Zendesk__Organization__c, Zendesk__Requester__r.AccountID FROM Zendesk__Zendesk_Ticket__c WHERE Id IN :Trigger.new
                                             AND Zendesk__Requester__r.AccountID = NULL]){
                                                                                         
        ticket1.Zendesk__Requester__r.AccountID = ticket1.Zendesk__Organization__c;                                                                                                 
        update ticket1.Zendesk__Requester__r;                                                                                               
    }

 

 

I wonder if my SOQL query is causing the problem by selecting only the tickets whose related Contact's account ID is NULL - can I fairly assume that to be true when a contact is inserted without an account specified?

 

 

The problem:

 

My system.assertEquals statement is returning the error, because the "requester" contact's account ID is null. Clearly it isn't updating after being assigned ticket1.Organization__c's ID. 

 

I've created the trigger below and now i'm having issues creating a test class. Can someone please help? trigger Parent_Name_Trigger on Account (after insert) { List sr = new List(); for (Account newAccount: Trigger.New) if (newAccount.Student_Name__c <> ''){ sr.add (new Student__c( Account__c = newAccount.ID, Name = newAccount.Student_Name__c)) ; } insert sr; }

We're trying to use Solutions and I have a checkbox for INTERNAL ONLY.  The user creates the record, doesn't check this box so it can be viewed by public.  I have a WFR set up to see if INTERNAL ONLY = false and Status = accepted, check the Visible in Self-Service Portal, but after the approval process runs, its not checking the box.  Its acting like the record hasn't been edited.  If I click Edit and change something and Save it, the box will then get checked.  

 

Does anyone have a trigger with the same criteria to get that box checked?  I'm no good at triggers so I have no idea where to start. Thanks in advance!

  • October 18, 2013
  • Like
  • 0

Hi all,

 

I've got a simple class/trigger that updates an Account field with the number of Active Contracts. My class uses two SOQL queries:

 

public class Contract_Handler{

    public static Integer Count_Active_Contracts(Id acctId){
        /*This method is passed an Account ID, counts the related ACTIVE contracts(ie, in between start and end dates), and updates the Active_Contracts__c field on the account record. Count is also returned for other potential uses. */
        
        Account acct = [SELECT Active_Contracts__c FROM Account WHERE Id =:acctId];
        
        Integer count = [SELECT Count() FROM Contract WHERE AccountId=:acctId AND StartDate <= TODAY AND EndDate >= TODAY];

        acct.Active_Contracts__c = count;
        update acct;
        
        return count;
    }    
}

 

 

I believe the issue with having "Too many SOQL Queries" arises from the Trigger, which loops through each Contract that triggered the code:

 

trigger Contract_Triggers on Contract (after insert, after update, after delete) {

    //Iterates through affected contracts and passes Account ID to 'Count_Active_Contracts' method
    if(trigger.isDelete){
        for(Contract c: trigger.old){
            Contract_Handler.Count_Active_Contracts(c.AccountId);
        }
    }
    else{
        for(Contract c: trigger.new){
    		Contract_Handler.Count_Active_Contracts(c.AccountId);
        }
    }
    
}

 

 

The code seems to work fine when I make a change to an individual contract (makes sense right? only two queries will run). However, when I try to update all existing contracts (179 records) to cause the code to run on all of them, I get the same error for each record:

 

Contract_Triggers: System.LimitException: Too many SOQL queries: 101 Status code: 13"

 

Should I be using Batch Apex to avoid this error? I'm reading up on it now, will post new version of code if I can figure it out. In the meantime any help is appreciated.