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
MSVRadMSVRad 

Testing a Trigger that populates a lookup field

I have written a simple trigger that populates a custom lookup field on the Contact object with the Data from a Standard lookup field on the Account object.

 

I am having some issue testing this trigger as everytime I try to create a dummy contact to test on I get an error.

 

Here is the trigger:

 

trigger PopulatePracticeContact on Contact (before update, before insert) { //Set of Account Ids Set<Id> AccountIds = new Set<Id>(); for (Contact contactNew : Trigger.new) { AccountIds.add(contactNew.AccountId); //A set of Accounts } Map<Id, Account> pAccounts = new Map<Id, Account>([SELECT a.id, a.parentid, a.Name FROM Account a WHERE a.id IN :AccountIds]); for(Contact fcon : Trigger.New){ fcon.Related_Practice_Account__c = pAccounts.get(fcon.AccountId).ParentId; } }

 

 

Here is what I have started for the Test Class... I am not really sure how to test this anyway, so any suggestions would be greatly appreciated.

 

public with sharing class PopulatePracticeContactTest {

  static testMethod void PopulatePracticeContactTest(){

 

Account pAcc1 = new Account(Name = 'TestAccount1', BillingState = 'MN', Practice_ID__c = '1234', Operational_status__c = 'Prospect', recordTypeId = '012700000009R7B');

insert pAcc1;

Account fAcc1 = new Account(Name = 'TestAccount2', BillingState = 'OH', Facility_ID__c = '4321', Operational_status__c ='Pending', recordTypeId = '012700000009R7G', ParentId = pAcc1.id);

insert fAcc1;

 

Contact pCon1 = new Contact(LastName = 'TestContact1', AccountId = pAcc1);

insert pCon1;

Contact fCon1 = new Contact(LastName = 'TestContact2', AccountId = fAcc1);  insert fCon1;

 

}

}

 

The error that I get is this:

 

Save error: Invalid initial expression type for field Contact.AccountId, expecting: Id PopulatePracticeContactTest.cls 

 

Update: I didn't review this before I posted it - I did make a copy/paste error I just pasted the trigger twice.  The test class that I actually started writing is now added.

Message Edited by MSVRad on 09-08-2009 06:44 AM
Best Answer chosen by Admin (Salesforce Developers) 
MSVRadMSVRad

I tried account.id and accountid and neither worked they both gave me an error.  I tried

 

accountid = pAcc1.id and it worked! No Errors.

Message Edited by MSVRad on 09-08-2009 10:58 AM

All Answers

AmphroAmphro

You accedently pasted your trigger twice :)

I would guess from the error that your not putting an Id into AccountId. I would think it would work if you are assigning an account into AccountId, but you might try account.Id

Let me know or paste your test :)

MSVRadMSVRad

I tried account.id and accountid and neither worked they both gave me an error.  I tried

 

accountid = pAcc1.id and it worked! No Errors.

Message Edited by MSVRad on 09-08-2009 10:58 AM
This was selected as the best answer
roysmith601.3493067652527424E1roysmith601.3493067652527424E1

Hi,
I have question about trigger.I create a custom object ="Product" on opportunity object .I create three (3) fields on custom object = Product and the filed description is below:
Field # 1:Application Received By(which is lookup(user)field)
field #2 : Closing Stage (pick list field; option is recieved)
field # 3: Closing Stage Owner ((which is lookup(user)field)

I have the requirment that :
If Application Received By not null
then
condition # 1 : set 
Closing Stage = recieved
and 

condition # 2 : set Closing Stage Owner = Application Received By

Example: If  
Application Received By = steve then according to workflow it should update the Closing Stage = recieved and Closing Stage Owner =steve

I create workflow for this and i am able to satisfy the condition # 1  but I am not to create work field update for condition # 2. 
I am new to salesforce and could anyone of you help me for writing this trigger.

thanks