function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
James MersonJames Merson 

Hoping for help with trigger for lookup field.

This may be a stretch but I'm hoping for someone to help me write a trigger.

We have a lookup field on accounts that filters on related contacts.  Based on a series of conditions being met when a record is edited, we'd like to insert into that field the Contact ID for the related contact with title = Owner.

Conditions to be met are:
Steps_to_Close__c = "Enrolled" - picklist field
Number_of_clients__c = 0 - number field
Franchise_Association__c = Null or "HCA" - picklist field

Field to update:
PM__c to be equal to the contact ID for the related contact whose title = "Owner.

Hoping someone can help a guy with not many resources....

James
Best Answer chosen by James Merson
Himanshu ParasharHimanshu Parashar
Hi James,

I am not explaining postive and negative case here but following code should give you 100% code coverage. Try to understand code with inline comments.
@istest(seealldata=false)
public class ContactList_Test {
 
   static testMethod void  testContactList(){

   //insert account
   Account acc = new Account(name='Test Account', Steps_to_Close__c ='',Number_of_clients__c=0,Franchise_Association__c == 'HCA');
   insert acc;
  
   List<Contact> lstContact = new List<Contact>();
   
   Contact con = new Contact(FirstName='Test',Lastname='Contact',Title='Owner',Accoundid=acc.id);
   lstContact.add(con);  
   
   Contact con1 = new Contact(FirstName='Test1',Lastname='Contact1',Title='CEO',Accoundid=acc.id);
   lstContact.add(con1); 

   //insert contacts   
   insert lstContact;
   
   //update account so that trigger can be fired.
  Account updateacc = [select id,Steps_to_Close__c from Account where id =: acc.id];
  updateacc.Steps_to_Close__c ='Enrolled';
  update updateacc;

  //assert values
  Account objupdateacc = [select id,PM__c from Account where id =: acc.id];
  system.assertequals(con.id,objupdateacc.PM__c);

   
  }
  
 }

Thanks,
Himanshu

All Answers

Himanshu ParasharHimanshu Parashar
Hi James,

All conditions fields are on Contact because multiple contact can have title="Owner" ?
Kevin AkermanisKevin Akermanis
To clarify...

"The Conditions to be met" are fields/values that are on the Account object right?
James MersonJames Merson
@Kevin:  Yes.  They fields are on the account object.

@Himanshu:  No, they are on the account object.  I realize that many contacts could have that title, and definitely thought about that.  This trigger should only execute once for a specific subset of accounts where there *should* only be one contact who is an owner.  If otherwise, it can fail and I'm okay with it.
Himanshu ParasharHimanshu Parashar
Hi James,

following code should work. As you said I am assuming here that there will be single contact with title='Owner'
 
Trigger triggername on Account (before update)
{
Map<id,id> AccMap = new Map<id,id>(); 
//Prepare the contact data which will be used to fill PM__c
 for(Contact con : [select id,Accountid from contact where accountid in : Trigger.newmap.keyset() and title='Owner'])
{
  AccMap.put(con.accountid,con.id);
}

//do the actual magic here
for(Account acc : Trigger.new)
{
     check for condition here
     if(acc.Steps_to_Close__c == "Enrolled" && acc.Number_of_clients__c ==0 && 
     (acc.Franchise_Association__c = Null || 
     acc.Franchise_Association__c =="HCA" && AccMap.containskey(acc.id))
     {
        acc.PM__c = accmap.get(acc.id);
     } 
}

}


Thanks,
Himanshu
James MersonJames Merson
Hi Himanshu.  I added the below (with minor tweaks from yours for the quotation marks) and got the following error:  
Error: Compile Error: unexpected token: '{' at line 17 column 5
Trigger triggername on Account (before update)
{
Map<id,id> AccMap = new Map<id,id>(); 
//Prepare the contact data which will be used to fill PM__c
 for(Contact con : [select id,Accountid from contact where accountid in : Trigger.newmap.keyset() and title='Owner'])
{
  AccMap.put(con.accountid,con.id);
}

//do the actual magic here
for(Account acc : Trigger.new)
{
     check;
     if(acc.Steps_to_Close__c == 'Enrolled' && acc.Number_of_clients__c ==0 && 
     (acc.Franchise_Association__c = Null || 
     acc.Franchise_Association__c == 'HCA' && AccMap.containskey(acc.id))
     {
        acc.PM__c = accmap.get(acc.id);
     } 
}

}

Any thoughts?  Thanks for all your help.  Hoping this will actually work!
Himanshu ParasharHimanshu Parashar
there is minor mistake in that, it should work now
 
for(Account acc : Trigger.new)
{
     check;
     if(acc.Steps_to_Close__c == 'Enrolled' && acc.Number_of_clients__c ==0 && 
     (acc.Franchise_Association__c = Null || 
     acc.Franchise_Association__c == 'HCA') && AccMap.containskey(acc.id))
     {
        acc.PM__c = accmap.get(acc.id);
     } 
}

 
Himanshu ParasharHimanshu Parashar
I am not sure why you have added check; at line 13
James MersonJames Merson
It was based on an earlier error.  Here is the error:
Error: Compile Error: expecting a semi-colon, found 'for' at line 13 column 11

and here is the code with your original text on line 13:
Trigger triggername on Account (before update)
{
Map<id,id> AccMap = new Map<id,id>(); 
//Prepare the contact data which will be used to fill PM__c
 for(Contact con : [select id,Accountid from contact where accountid in : Trigger.newmap.keyset() and title='Owner'])
{
  AccMap.put(con.accountid,con.id);
}

//do the actual magic here
for(Account acc : Trigger.new)
{
     check for condition here
     if(acc.Steps_to_Close__c == 'Enrolled' && acc.Number_of_clients__c ==0 && 
     (acc.Franchise_Association__c = Null || 
     acc.Franchise_Association__c == 'HCA') && AccMap.containskey(acc.id))
     {
        acc.PM__c = accmap.get(acc.id);
     } 
}

}

 
Himanshu ParasharHimanshu Parashar
aah sorry my bad,

I was putting comment at that line. you can remove that line or replace that with 
 
//check for condition here

 
James MersonJames Merson
Yay!  That worked great.  Now I just have to figure out how to write unit tests so I can deploy this to production.  Any potential insight?
Himanshu ParasharHimanshu Parashar
Hi James,

I am not explaining postive and negative case here but following code should give you 100% code coverage. Try to understand code with inline comments.
@istest(seealldata=false)
public class ContactList_Test {
 
   static testMethod void  testContactList(){

   //insert account
   Account acc = new Account(name='Test Account', Steps_to_Close__c ='',Number_of_clients__c=0,Franchise_Association__c == 'HCA');
   insert acc;
  
   List<Contact> lstContact = new List<Contact>();
   
   Contact con = new Contact(FirstName='Test',Lastname='Contact',Title='Owner',Accoundid=acc.id);
   lstContact.add(con);  
   
   Contact con1 = new Contact(FirstName='Test1',Lastname='Contact1',Title='CEO',Accoundid=acc.id);
   lstContact.add(con1); 

   //insert contacts   
   insert lstContact;
   
   //update account so that trigger can be fired.
  Account updateacc = [select id,Steps_to_Close__c from Account where id =: acc.id];
  updateacc.Steps_to_Close__c ='Enrolled';
  update updateacc;

  //assert values
  Account objupdateacc = [select id,PM__c from Account where id =: acc.id];
  system.assertequals(con.id,objupdateacc.PM__c);

   
  }
  
 }

Thanks,
Himanshu
This was selected as the best answer
James MersonJames Merson
Thank you! Thank you!  Thank you!