• Gene T.
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 13
    Replies

I've written some code that updates a parent record whenever a child record is created. I've tested the functionality, and it works for what I need it to do. Now I am stuck on how to write the test class to get the coverage I need to move it to production. Any help would be much appreciated!

 

trigger UpdateProjLastUpdate on Time_Allocation__c (after insert) {
            
     Map<Id,TTLProject__c> projMap = new Map<Id,TTLProject__c>();
     Set<id> Ids = new Set<id>();
         for (Time_Allocation__c  TA : Trigger.new)
         {
             Ids.add(TA.Project__c);
         }
    
     Map<id,TTLProject__c> projMap2 = new Map<id,TTLProject__c>([Select Id, Last_Update__c from TTLProject__c Where Id in :Ids]); 
    
         for (Time_Allocation__c TA : [SELECT Start_Date__c, Work_Done_By_User__c, Work_Done_By_User__r.name, Description__c, Project__c
                                                      FROM Time_Allocation__c
                                                      WHERE id
                                                      IN :Trigger.newMap.keySet()])
         {
             TTLProject__c proj = projMap2.get(TA.Project__c);
             proj.Last_update__c = TA.Start_Date__c.format() + ' - ' + TA.Work_Done_By_User__r.name + ' - ' + TA.Description__c;
             projMap.put(proj.id,proj);
         }
        
         if(!projMap.isEmpty())
         {
             update projMap.values();
         }
}

 

Hi,

 

I have a trigger on the Contact object that creates a new record on a custom object called Student.

 

 

trigger CreateStudentFromContact on Contact (after insert) {
  
    List<Student__c> Students = new List<Student__c>();
    for(contact c:trigger.new)
    {
     student__c s=new student__c(
         name=''+c.firstName +' ' +c.lastName, 
         contact__c=c.id, 
         Birthdate__c=c.Birthdate,
         Description__c=c.Description,
         Email__c=c.Email,
         Family__c=c.AccountID,
         Gender__c=c.Gender__c,
         Home_Phone__c=c.HomePhone,
         Lead_Source__c=c.LeadSource,
         Mailing_City__c=c.MailingCity,
         Mailing_Country__c=c.MailingCountry,
         Mailing_State_Province__c=c.MailingState,
         Mailing_Street__c=c.MailingStreet,
         Mailing_Zip_Postal_Code__c=c.MailingPostalCode,
         Mobile__c=c.MobilePhone,
         Phone__c=c.Phone,
         Program_of_Interest__c=c.Program_of_Interest__c,
         Promotion__c=c.Promotion__c    
     );
        
        
        Students.add(s);
    }
    
    if (!Students.isEmpty()) {
      insert Students;
    }

 I would like to make it so that the trigger only activates if the contact Record Type is equal to "Student". Any Advice on how to do this?

 

 

 

I have 3 custom objects that lookup up to the same custom object. The parent object has a sharing rule in place that shares the records with public groups based off a picklist called "division". This works perfectly for the parent. For each of the child objects, I use a formula to bring over the picklist value from the parent as a text field. Now, when I try to setup the sharing rule, the field is not available. Any ideas on how to approach this?

Is it possible to write a trigger that when an owner of a contact is change, the account owner is updated to the same owner as the contact? Also, if the account has multiple contacts associated with it, will the other contacts ownership be transferred as well? Any advice or tips would be much appreciated.

What is the best way to see/query which records have no activities associated with it?

Is it possible to tag a single activity to multiple records? What would be the best way to approach a solution? Any input would be appreciated.

Hi All,

 

This is my first attempt at writing an apex trigger and have zero background in coding. The purpose of the code is to create a new account when a new contact is created and is not assigned to an account. Here's what I pieced together:

 

trigger CreateIndividualAccount on Contact (before insert, before update) {
RecordType arec = [SELECT ID from RecordType WHERE SobjectType='Account' AND Name='Individual' limit 1];
for (Contact c : trigger.new)
{
if(c.AccountID == null)
{
Account[] acc = new Account[0];
string cName = (Owner.Name);
acc.add(new Account (Name = cName, RecordTypeId = arec.Id));

insert acc;
c.AccountID = acc[0].Id;
}
}
}

 

I get the following error when I attempt to save: Error: Compile Error: Variable does not exist: Owner.Name at line 8 column 29

 

Please let me know what I am doing wrong, I'm sure it is something really simple.

 

Cheers

I've written some code that updates a parent record whenever a child record is created. I've tested the functionality, and it works for what I need it to do. Now I am stuck on how to write the test class to get the coverage I need to move it to production. Any help would be much appreciated!

 

trigger UpdateProjLastUpdate on Time_Allocation__c (after insert) {
            
     Map<Id,TTLProject__c> projMap = new Map<Id,TTLProject__c>();
     Set<id> Ids = new Set<id>();
         for (Time_Allocation__c  TA : Trigger.new)
         {
             Ids.add(TA.Project__c);
         }
    
     Map<id,TTLProject__c> projMap2 = new Map<id,TTLProject__c>([Select Id, Last_Update__c from TTLProject__c Where Id in :Ids]); 
    
         for (Time_Allocation__c TA : [SELECT Start_Date__c, Work_Done_By_User__c, Work_Done_By_User__r.name, Description__c, Project__c
                                                      FROM Time_Allocation__c
                                                      WHERE id
                                                      IN :Trigger.newMap.keySet()])
         {
             TTLProject__c proj = projMap2.get(TA.Project__c);
             proj.Last_update__c = TA.Start_Date__c.format() + ' - ' + TA.Work_Done_By_User__r.name + ' - ' + TA.Description__c;
             projMap.put(proj.id,proj);
         }
        
         if(!projMap.isEmpty())
         {
             update projMap.values();
         }
}

 

Hi,

 

I have a trigger on the Contact object that creates a new record on a custom object called Student.

 

 

trigger CreateStudentFromContact on Contact (after insert) {
  
    List<Student__c> Students = new List<Student__c>();
    for(contact c:trigger.new)
    {
     student__c s=new student__c(
         name=''+c.firstName +' ' +c.lastName, 
         contact__c=c.id, 
         Birthdate__c=c.Birthdate,
         Description__c=c.Description,
         Email__c=c.Email,
         Family__c=c.AccountID,
         Gender__c=c.Gender__c,
         Home_Phone__c=c.HomePhone,
         Lead_Source__c=c.LeadSource,
         Mailing_City__c=c.MailingCity,
         Mailing_Country__c=c.MailingCountry,
         Mailing_State_Province__c=c.MailingState,
         Mailing_Street__c=c.MailingStreet,
         Mailing_Zip_Postal_Code__c=c.MailingPostalCode,
         Mobile__c=c.MobilePhone,
         Phone__c=c.Phone,
         Program_of_Interest__c=c.Program_of_Interest__c,
         Promotion__c=c.Promotion__c    
     );
        
        
        Students.add(s);
    }
    
    if (!Students.isEmpty()) {
      insert Students;
    }

 I would like to make it so that the trigger only activates if the contact Record Type is equal to "Student". Any Advice on how to do this?

 

 

 

Is it possible to write a trigger that when an owner of a contact is change, the account owner is updated to the same owner as the contact? Also, if the account has multiple contacts associated with it, will the other contacts ownership be transferred as well? Any advice or tips would be much appreciated.

Hi All,

 

This is my first attempt at writing an apex trigger and have zero background in coding. The purpose of the code is to create a new account when a new contact is created and is not assigned to an account. Here's what I pieced together:

 

trigger CreateIndividualAccount on Contact (before insert, before update) {
RecordType arec = [SELECT ID from RecordType WHERE SobjectType='Account' AND Name='Individual' limit 1];
for (Contact c : trigger.new)
{
if(c.AccountID == null)
{
Account[] acc = new Account[0];
string cName = (Owner.Name);
acc.add(new Account (Name = cName, RecordTypeId = arec.Id));

insert acc;
c.AccountID = acc[0].Id;
}
}
}

 

I get the following error when I attempt to save: Error: Compile Error: Variable does not exist: Owner.Name at line 8 column 29

 

Please let me know what I am doing wrong, I'm sure it is something really simple.

 

Cheers