• El.H
  • NEWBIE
  • 10 Points
  • Member since 2018
  • Volunteer

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

I am trying to write a simple test for a trigger:
trigger UpdateHasRelatedCase on Case (after insert, after update) {
    
   List<Contact> toUpdate = new List<Contact>();
    for(Case ca: Trigger.new)
    {
        Boolean closed = ca.closedDate!=null;
        Boolean openCases=false;
        Contact con = [select id,firstname, has_open_case__c from Contact where id =:ca.contactId limit 1];
        
        if(!closed)
        {
            
            if(con!=null)
            {
               
                con.has_open_case__c=true;
            }
                
        }
        else
        {
            list<Case> cases = [select contactId, casenumber,ClosedDate from Case where contactId=:con.id];
            for(Case c: cases)
            {
                if(c.closedDate==null)
                {
                    if(con!=null)
                    	openCases=true;
                    break;
                }
            }
            
            con.has_open_case__c= openCases;  
        }
        toUpdate.add(con);
    }
    update toUpdate;

}

basically it will just check if the current context (case) is open and set a the has_open_case__c field on Contact to true.  If it is not open, it will check all other related Cases and if it finds any that are open, it will set the field to true.
if none of the above occurs then the field is false.

I have written system.debug statements in different places in this trigger, and found that I get the expected results. However, in my test class :
@isTest
public class TestUpdateHasRelatedCase {
    
    @isTest
    static void TestHasRelated()
    {
         Contact cli=new Contact
           (
               firstname='TestCli',
               lastname='test',
               Relationship_Status__c='Prefer not to say',
               Gender__c='Prefer not to say',
               leadSource='Friend',
               mailingstreet='123 SomeTown',
               contact_ethnicity__c='Prefer not to say',
               mailingcity='SomeCity,
               mailingpostalcode='CCC CCC'
           );
        insert cli;
        
        Case ca = new Case
        (
            contactid=cli.id,
            ethnicity__c = 'Other Ethnic Group',
            delegate__c='0050O000008KiQzQAK',
            subject='test',
            origin='Red Cross',
            Reason='New problem',
            recordtypeid='0123Y000000dbAUQAY',
            Status='Closed'
              
        );
        insert ca;
        
        System.assertEquals(true,cli.has_open_case__c);
    }
    

}
the result of has_open_case__c is not the same value as when I printed it inside the trigger, and leading to failure of the test.

Any help on why this is happening would be appreciated !

Thank you ,
El

 
  • January 04, 2023
  • Like
  • 0
Hello,

I have been searching online for a while on the answer to this but can't seem to get anything that works so I am posting here, any assistance is appreciated .

I made a very basic apex trigger in sandbox which will stop the insert of a case record if an existing with some matching criteria is found.  I did a few tests with it in the sandbox and know that it works as I would expect it to. 
I can't make the apex triggers on my live org, so I am trying to deploy this trigger from the sandbox I have to my live org via a change set, but I cannot add this single trigger to a change set, it does not appear under apex triggers or any other heading I have searched.  I was thinking this might be linked to it having 0% code coverage (I am not sure how to properly test)  but I really don't know why it won't appear as an option to add to the change set.

Can anyone help with this?  Its just a small trigger I want to add temporarily to stop duplicate cases being inserted when I use the dataloader.

Thanks,
El
  • November 21, 2020
  • Like
  • 0
Hello,

I am trying to write something small for execute anonymous wherein I can access a field on a related object and copy data there to a new field on the current object. I am struggling to write the correct syntax for this one though so any help or guidance would be appreciated.

The objects I am using are Contact and Cases.
In this scenario, I am trying to copy data from a field on the Case object, to a new one on the related Contact object.
To specify:
There is a field called Health__c, which exists on the Cases, and I have created a custom field named Health_and_support__c which resides on the Contact objects.  I am trying to write a script which will:
  • get a list of all contacts that have a related case and have nothing filled out in health and support already.
  • loop through each contact and copy the information from Health__c, from the related Case record, into Health_and_support__c on the Contact record.

my code so far looks like this:
list<Contact> needHealth = new list <Contact>();
needHealth = [SELECT Id, Name, Health_and_support__c FROM Contact where Id in (select ContactId from Case) AND Health_and_support__c=''];

for (Contact c :needHealth){
    c.Health_and_support__c=c.Case.Health__c;
}

update needHealth;
Currently this code will display error: Line: 6, Column: 31
Variable does not exist: Case

I am fairly confident the answer would be similar to this (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SObjects_field_relationships.htm)
however I can't seem to figure out how to do this with a list of objects.

Thank you for any assistance.

El
 
  • August 24, 2020
  • Like
  • 0
Hello,

First time to post so apologies if this is not in the correct area.

I am working on some code executed in anonymous that will insert some records in a charity's org.   The charity has job records, on those jobs exist shifts, and those shifts can have hours, for context.

The problem at the moment is that several jobs exist in the org which have shifts, but the hours were not correctly added, so I created the script below which can create those hour records and successfully copy over all the information that is needed from the shift/job record.  However, when I attempt to run this in the live version on a single record (which has 21 shifts)  I am getting Error:System.LimitException: Too many SOQL queries:101.

Below is my script:
List<GW_Volunteers__Volunteer_Job__c> badJobs = new list<GW_Volunteers__Volunteer_Job__c>();
badJobs=[SELECT Id FROM GW_Volunteers__Volunteer_Job__c WHERE GW_Volunteers__First_Shift__c!=NULL AND GW_Volunteers__Number_of_Completed_Hours__c=0]; //jobs that have shift with no hr
    
List<GW_Volunteers__Volunteer_Shift__c> needsHr = new list <GW_Volunteers__Volunteer_Shift__c>();
needsHr=[SELECT Id, GW_Volunteers__Start_Date_Time__c,GW_Volunteers__Duration__c, GW_Volunteers__Volunteer_Job__r.Volunteer__c FROM GW_Volunteers__Volunteer_Shift__c WHERE GW_Volunteers__Volunteer_Job__c in :badJobs]; //shift records with no hrs
for (GW_Volunteers__Volunteer_Shift__c s :needsHr){
    DateTime dT = s.GW_Volunteers__Start_Date_Time__c;
    Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
    GW_Volunteers__Volunteer_Hours__c hour = new GW_Volunteers__Volunteer_Hours__c(
        GW_Volunteers__Contact__c=s.GW_Volunteers__Volunteer_Job__r.Volunteer__c,
    	GW_Volunteers__Status__c='Completed',
    	GW_Volunteers__Hours_Worked__c=s.GW_Volunteers__Duration__c,
    	GW_Volunteers__Volunteer_Job__c=s.GW_Volunteers__Volunteer_Job__c,
        GW_Volunteers__Volunteer_Shift__c=s.Id,
    	GW_Volunteers__Start_Date__c=myDate,
        GW_Volunteers__End_Date__c=myDate
        
        
    
    );
    insert hour;
}

I believe I need to adjust this so that it is calling fewer queries, but I am not entirely sure what I can do other than put a LIMIT which won't really work for me as I need to run through all of the shifts on a given job record in one go.
When I change the shift query to the following, the script will run fine and insert hours corresponding to the correct shift records:
needsHr=[SELECT Id, GW_Volunteers__Start_Date_Time__c,GW_Volunteers__Duration__c, GW_Volunteers__Volunteer_Job__r.Volunteer__c FROM GW_Volunteers__Volunteer_Shift__c WHERE GW_Volunteers__Volunteer_Job__c ='a0T3Y00000SLivnUAD' LIMIT 10];

I am grateful for any assistance on this.

Thank you.
El
 
  • March 03, 2020
  • Like
  • 0
Hello,

I am trying to write a simple test for a trigger:
trigger UpdateHasRelatedCase on Case (after insert, after update) {
    
   List<Contact> toUpdate = new List<Contact>();
    for(Case ca: Trigger.new)
    {
        Boolean closed = ca.closedDate!=null;
        Boolean openCases=false;
        Contact con = [select id,firstname, has_open_case__c from Contact where id =:ca.contactId limit 1];
        
        if(!closed)
        {
            
            if(con!=null)
            {
               
                con.has_open_case__c=true;
            }
                
        }
        else
        {
            list<Case> cases = [select contactId, casenumber,ClosedDate from Case where contactId=:con.id];
            for(Case c: cases)
            {
                if(c.closedDate==null)
                {
                    if(con!=null)
                    	openCases=true;
                    break;
                }
            }
            
            con.has_open_case__c= openCases;  
        }
        toUpdate.add(con);
    }
    update toUpdate;

}

basically it will just check if the current context (case) is open and set a the has_open_case__c field on Contact to true.  If it is not open, it will check all other related Cases and if it finds any that are open, it will set the field to true.
if none of the above occurs then the field is false.

I have written system.debug statements in different places in this trigger, and found that I get the expected results. However, in my test class :
@isTest
public class TestUpdateHasRelatedCase {
    
    @isTest
    static void TestHasRelated()
    {
         Contact cli=new Contact
           (
               firstname='TestCli',
               lastname='test',
               Relationship_Status__c='Prefer not to say',
               Gender__c='Prefer not to say',
               leadSource='Friend',
               mailingstreet='123 SomeTown',
               contact_ethnicity__c='Prefer not to say',
               mailingcity='SomeCity,
               mailingpostalcode='CCC CCC'
           );
        insert cli;
        
        Case ca = new Case
        (
            contactid=cli.id,
            ethnicity__c = 'Other Ethnic Group',
            delegate__c='0050O000008KiQzQAK',
            subject='test',
            origin='Red Cross',
            Reason='New problem',
            recordtypeid='0123Y000000dbAUQAY',
            Status='Closed'
              
        );
        insert ca;
        
        System.assertEquals(true,cli.has_open_case__c);
    }
    

}
the result of has_open_case__c is not the same value as when I printed it inside the trigger, and leading to failure of the test.

Any help on why this is happening would be appreciated !

Thank you ,
El

 
  • January 04, 2023
  • Like
  • 0
Hello,

I am trying to write something small for execute anonymous wherein I can access a field on a related object and copy data there to a new field on the current object. I am struggling to write the correct syntax for this one though so any help or guidance would be appreciated.

The objects I am using are Contact and Cases.
In this scenario, I am trying to copy data from a field on the Case object, to a new one on the related Contact object.
To specify:
There is a field called Health__c, which exists on the Cases, and I have created a custom field named Health_and_support__c which resides on the Contact objects.  I am trying to write a script which will:
  • get a list of all contacts that have a related case and have nothing filled out in health and support already.
  • loop through each contact and copy the information from Health__c, from the related Case record, into Health_and_support__c on the Contact record.

my code so far looks like this:
list<Contact> needHealth = new list <Contact>();
needHealth = [SELECT Id, Name, Health_and_support__c FROM Contact where Id in (select ContactId from Case) AND Health_and_support__c=''];

for (Contact c :needHealth){
    c.Health_and_support__c=c.Case.Health__c;
}

update needHealth;
Currently this code will display error: Line: 6, Column: 31
Variable does not exist: Case

I am fairly confident the answer would be similar to this (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SObjects_field_relationships.htm)
however I can't seem to figure out how to do this with a list of objects.

Thank you for any assistance.

El
 
  • August 24, 2020
  • Like
  • 0
Hello,

First time to post so apologies if this is not in the correct area.

I am working on some code executed in anonymous that will insert some records in a charity's org.   The charity has job records, on those jobs exist shifts, and those shifts can have hours, for context.

The problem at the moment is that several jobs exist in the org which have shifts, but the hours were not correctly added, so I created the script below which can create those hour records and successfully copy over all the information that is needed from the shift/job record.  However, when I attempt to run this in the live version on a single record (which has 21 shifts)  I am getting Error:System.LimitException: Too many SOQL queries:101.

Below is my script:
List<GW_Volunteers__Volunteer_Job__c> badJobs = new list<GW_Volunteers__Volunteer_Job__c>();
badJobs=[SELECT Id FROM GW_Volunteers__Volunteer_Job__c WHERE GW_Volunteers__First_Shift__c!=NULL AND GW_Volunteers__Number_of_Completed_Hours__c=0]; //jobs that have shift with no hr
    
List<GW_Volunteers__Volunteer_Shift__c> needsHr = new list <GW_Volunteers__Volunteer_Shift__c>();
needsHr=[SELECT Id, GW_Volunteers__Start_Date_Time__c,GW_Volunteers__Duration__c, GW_Volunteers__Volunteer_Job__r.Volunteer__c FROM GW_Volunteers__Volunteer_Shift__c WHERE GW_Volunteers__Volunteer_Job__c in :badJobs]; //shift records with no hrs
for (GW_Volunteers__Volunteer_Shift__c s :needsHr){
    DateTime dT = s.GW_Volunteers__Start_Date_Time__c;
    Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
    GW_Volunteers__Volunteer_Hours__c hour = new GW_Volunteers__Volunteer_Hours__c(
        GW_Volunteers__Contact__c=s.GW_Volunteers__Volunteer_Job__r.Volunteer__c,
    	GW_Volunteers__Status__c='Completed',
    	GW_Volunteers__Hours_Worked__c=s.GW_Volunteers__Duration__c,
    	GW_Volunteers__Volunteer_Job__c=s.GW_Volunteers__Volunteer_Job__c,
        GW_Volunteers__Volunteer_Shift__c=s.Id,
    	GW_Volunteers__Start_Date__c=myDate,
        GW_Volunteers__End_Date__c=myDate
        
        
    
    );
    insert hour;
}

I believe I need to adjust this so that it is calling fewer queries, but I am not entirely sure what I can do other than put a LIMIT which won't really work for me as I need to run through all of the shifts on a given job record in one go.
When I change the shift query to the following, the script will run fine and insert hours corresponding to the correct shift records:
needsHr=[SELECT Id, GW_Volunteers__Start_Date_Time__c,GW_Volunteers__Duration__c, GW_Volunteers__Volunteer_Job__r.Volunteer__c FROM GW_Volunteers__Volunteer_Shift__c WHERE GW_Volunteers__Volunteer_Job__c ='a0T3Y00000SLivnUAD' LIMIT 10];

I am grateful for any assistance on this.

Thank you.
El
 
  • March 03, 2020
  • Like
  • 0