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
MSSBMeghanMSSBMeghan 

Retrieve Contact Name from Contact ID field on Contract (populated from ContactContract Role)

Let me preface this by saying that I am not all that experienced with Apex - I have dabbled enough to be dangerous, but do not have a robust understanding. Tried a search related to this issue and came up with nothing, but my apologies if I overlooked a previous related post.

 

Challenge with Contract Object:

 

I need a way to pull the Primary Contact information from the Related Contact list to the Contract.  I was able to build a trigger that brings the Primary Contact ID onto the Contract from the Related Contact Object, however when trying to populate the Contact Name affiliated with the Primary Contact ID I've hit a wall. Since the ContractContactRole object does not have a built in reference to the contact name I've got a bit more digging to do other than what I have existing - can't simply replicate the trigger and substitute a name field that doesn't exist on the object.

 

Need for the Primary Contact on the Contract: We have a WIL driven button that is designed to pull the Primary Contact name into a specified email template - in place for ease of use for the end user and a check to ensure the end user does not  select the wrong contact on the contract for this email. 

 

Right now the link is pulling the Contact ID, which obviously doesn't work to personalize the salutation in our email.  I need the Primary Contact ID on the Contract for the WIL to properly populate the email recipient, but I also need another field on the Contract that can serve as the merge field on the email template.

 

I tried a formula field, but no luck.  I think I have to go trigger route, but then it gets into an area of timing, which I have no idea how to solve.

 

 

Here is my Trigger for the Primary Contact ID:

 

 

trigger ShowContactID on Contract (before update) {
for (Contract c : Trigger.new) {
ContractContactRole contactRole =
[select ContactID from ContractContactRole where IsPrimary = true and ContractId = :c.id];

if (contactRole != null) {
c.Primary_Contact_ID__c = contactRole.ContactID;
}
}
}

 

 

 

 

Here is my attempt at a formula to populate the Contact Name:

 

 

IF(
Primary_Contact_ID__c = Contact_Lookup__r.Id ,
Contact_Lookup__r.FirstName ,
"Recipient"
)

 

 

 

 Thanks in advanced for helping me figure this out!

 

 

venturecventurec
Let me know if this works for you...


trigger ShowContactID on Contract (before update) {

Map contactRole = new Map([select Id, ContactID from ContractContactRole where IsPrimary = true]);

for (Contract c : Trigger.new) {

if (contactRole.get(c.Id) != null) {
c.Primary_Contact_ID__c = contactRole.get(c.Id).ContactId;
}
}
}
venturecventurec

Repost as the Developer website seems to want to break my code apart and leave off import information :)

 

Try this one instead of the one above:

 

 

trigger ShowContactID on Contract (before update) {

 

Map<Id, ContractContactRole> contactRole = new Map<Id, ContractContactRole>([select Id, ContactID from ContractContactRole where IsPrimary = true]);

 

for (Contract c : Trigger.new) {

 

if (contactRole.get(c.Id) != null) {

c.Primary_Contact_ID__c = contactRole.get(c.Id).ContactId;

}

}

}

MSSBMeghanMSSBMeghan

I think I might be missing something from your post.  I tried your code and it seems to do the exact same thing my working trigger does - it brings the Primary Contact ID onto the Contract from the Related Contact Object. 

 

The piece I am missing is how to translate that value to the name field associated with that contact.  Having the contact ID as I do is necessary to populate the recipient identifier of the WIL that drives the email creation, however I need to have a merge field with the contact name in order to include the proper name in the body of the email and not just the alphanumeric ID of that specific contact.

 

How do I go about identifying the name based on the id value?  As name is not a field referenced on the ContractContactRole object I need to have some sort of lookup between that value and the name.

 

Again, I am a total newbie, but is there a quick and dirty way to differentiate between my code and yours? 

 

And my other concern relates to the timing of the triggers and how they fire - if the first trigger that populates the Contact ID doesn't fire before the one that brings over the name value, then the name value has no reference.  Do I need to specify an order?  

 

Ideally I'd think there would be a formula field or workflow solution to populate the Name once I have the Contact ID on the contract, but I've had no luck going that route.

 

Thanks so much for your help!!

MSSBMeghanMSSBMeghan

In case anyone is encountering a similar issue, I ended up solving this with a 2nd trigger.

 

First Trigger to Pull Related ContactId onto Contract:

 

 

trigger ShowContactID on Contract (before insert, before update) {
for (Contract c : Trigger.new) {
ContractContactRole contactRole =
[select ContactID from ContractContactRole where IsPrimary = true and ContractId = :c.id];

if (contactRole != null) {
c.Primary_Contact_ID__c = contactRole.ContactID;
}
}
}

 

Second Trigger to Pull Related Name onto Contract:

 

 

trigger ShowContactName on Contract (before insert, before update) {
for (Contract c : Trigger.new) {
ContractContactRole contactRole =
[select Contact.Name from ContractContactRole where IsPrimary = true and ContractId = :c.id];

if (contactRole != null) {
c.Primary_Contact_Name_2__c = contactRole.Contact.Name;
}
}
}

 

 OOPS - guess this isn't the solution after all....

 

 

Message Edited by MSSBMeghan on 09-30-2009 07:23 AM
Message Edited by MSSBMeghan on 09-30-2009 08:24 AM
venturec35venturec35

With the above trigger, you will run into Governor limits because the Select statement is in the For Loop.

Move the Select outside out of the For loop and put it into a Map<>.

 

Then use the get() method to get match on the ContactId

 

 

MSSBMeghanMSSBMeghan

venturec35 -

 

Thank you so much for your input.  As a newbie to apex (and non-technical person to boot) it is always so helpful to get help from the more experienced users!

 

I tried moving the SELECT outside of the For loop and put it into a Map as you suggested, but then none of the ContactIDs were populating at all.

 

Any suggestions?

 

Thanks again!

venturec35venturec35
Let me work on this a little more to see if I can get you what you are wanting.