• Bonz
  • NEWBIE
  • 10 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies

Hi all!

 

I'm still pretty new to writing apex triggers but I got this one to (kind of) work in my sandbox.  The trigger does part of what I need it to, but not all of what I need it to.  Here is what I'm trying to do:

 

The Contact record has a checkbox on it called Primary Contact (Primary_Contact__c) and the Account record has a lookup field on it called Primary Contact (also called Primary_Contact__c).  When the box is checked on the Contact it should populate the lookup field with that Contacts name.  So far I have that part working!  What I cannot get to work is if the Contact checkbox becomes unchecked and having that remove the Contact name from the Primary Contact lookup field on the Account.

 

Thanks in advance for any assistance.  Any help on the test class would be greatly appreciated too!!  Here is my trigger:

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
trigger UpdateAccount on Contact (after update) {

    Map<Id, Contact> contactsToCheck = new Map<Id, Contact>();
    for (Contact newCon : Trigger.New)
    {
        Contact oldCon = trigger.oldMap.get(newCon.Id);
        if (newCon.Primary_Contact__c != oldCon.Primary_Contact__c)
        {
            contactsToCheck.put(newCon.AccountId,newCon);
        }
    }

  
    List<Account> accs= [select id,Primary_Contact__c from Account where id IN: contactsToCheck.keySet()];
    for(Contact c : Trigger.new){
        if(c.Primary_Contact__c==TRUE ){
         for(Account acc : accs){
             acc.Primary_Contact__c = c.id;
         }
        }
    }
    update accs;
}

 

 

 

 

  • December 24, 2012
  • Like
  • 0

Hello!

I'm new to writing apex triggers but I finally got one to work in my sandbox.  My problem now is that I can't put it inot production without a test class, and I'm clueless on how to write it.  Here is what I'm trying to do:

 

When a Lead is created links via a lookup to a custom object called Site; When the field "First Payment Date" on the Site is updated the trigger updats a field named "First Payment Date" on the Lead.

 

How the heck do I write a test class for this??  Any help would be greatly appreciated!!

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
trigger updateFirstPaymentDate on Site__c(after update, after insert) {
    Integer i = 0;
    Set<ID> leadIds = new Set<ID>();
    Map<ID, Date> id2Date = new Map<ID, Date>();
    for (Site__c o : Trigger.new)
    {
        if ((Trigger.isInsert && o.First_Payment_Date__c != null) ||
            Trigger.old[i].First_Payment_Date__c != o.First_Payment_Date__c)
        {
            leadIds.add(o.lead__c);
            id2Date.put(o.lead__c, o.First_Payment_Date__c);
        }     
        i++;    
    }
    
    List<Lead> leads = [select id  from Lead where id in :leadIds];
    
    for (Lead c : leads)
    {
        c.First_Payment_Date__c = id2Date.get(c.Id);
    }
    
    update leads;
}

  • October 23, 2012
  • Like
  • 0

Hi all!

 

I'm still pretty new to writing apex triggers but I got this one to (kind of) work in my sandbox.  The trigger does part of what I need it to, but not all of what I need it to.  Here is what I'm trying to do:

 

The Contact record has a checkbox on it called Primary Contact (Primary_Contact__c) and the Account record has a lookup field on it called Primary Contact (also called Primary_Contact__c).  When the box is checked on the Contact it should populate the lookup field with that Contacts name.  So far I have that part working!  What I cannot get to work is if the Contact checkbox becomes unchecked and having that remove the Contact name from the Primary Contact lookup field on the Account.

 

Thanks in advance for any assistance.  Any help on the test class would be greatly appreciated too!!  Here is my trigger:

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
trigger UpdateAccount on Contact (after update) {

    Map<Id, Contact> contactsToCheck = new Map<Id, Contact>();
    for (Contact newCon : Trigger.New)
    {
        Contact oldCon = trigger.oldMap.get(newCon.Id);
        if (newCon.Primary_Contact__c != oldCon.Primary_Contact__c)
        {
            contactsToCheck.put(newCon.AccountId,newCon);
        }
    }

  
    List<Account> accs= [select id,Primary_Contact__c from Account where id IN: contactsToCheck.keySet()];
    for(Contact c : Trigger.new){
        if(c.Primary_Contact__c==TRUE ){
         for(Account acc : accs){
             acc.Primary_Contact__c = c.id;
         }
        }
    }
    update accs;
}

 

 

 

 

  • December 24, 2012
  • Like
  • 0

Hello!

I'm new to writing apex triggers but I finally got one to work in my sandbox.  My problem now is that I can't put it inot production without a test class, and I'm clueless on how to write it.  Here is what I'm trying to do:

 

When a Lead is created links via a lookup to a custom object called Site; When the field "First Payment Date" on the Site is updated the trigger updats a field named "First Payment Date" on the Lead.

 

How the heck do I write a test class for this??  Any help would be greatly appreciated!!

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
trigger updateFirstPaymentDate on Site__c(after update, after insert) {
    Integer i = 0;
    Set<ID> leadIds = new Set<ID>();
    Map<ID, Date> id2Date = new Map<ID, Date>();
    for (Site__c o : Trigger.new)
    {
        if ((Trigger.isInsert && o.First_Payment_Date__c != null) ||
            Trigger.old[i].First_Payment_Date__c != o.First_Payment_Date__c)
        {
            leadIds.add(o.lead__c);
            id2Date.put(o.lead__c, o.First_Payment_Date__c);
        }     
        i++;    
    }
    
    List<Lead> leads = [select id  from Lead where id in :leadIds];
    
    for (Lead c : leads)
    {
        c.First_Payment_Date__c = id2Date.get(c.Id);
    }
    
    update leads;
}

  • October 23, 2012
  • Like
  • 0