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
saracsarac 

Pre-Populate Contact Info in New Opportunity? (OportunityContactRole)

Hello,

I am trying to create an opportunity off of a custom object which has Contact as a lookup. The custom button uses as S-control to pre-populate the Opportunity fields such as name, stage, etc. i am unable to populate a ContactOpportunityRole object for the Opportunity by passing in values for the contact, role, and isPrimary.

Is there a way to do this or are we doomed to doing 2-step data entry for this Opportunity creation? I know that the Opportunity ID is required for OpportunityContactRole creation so it makes sense that the object can't be magically created just by passing the Contact Info into the "New" edit screen, especially since it is not visible on the edit screen and they are query parameters...

This is what I tried, the regular Opportunity fields are pre-populated correctly:
Code:
<script language="JavaScript">
function redirect() {
parent.frames.location.replace("/006/e—retURL=%2F{!Author__c.Id}&opp3={!Author__c.Name}&primary0=1&role0=Business User&contact0_lkid={!Author__c.ContactId__c}&contact0={!Author__c.Contact__c}")
}
redirect();
</script>

Thanks!
Sara

werewolfwerewolf
Well, you can't pre-populate opportunity roles because they're on a related list.  You can pre-populate fields, but not related lists.  A better way to do this would be to write an Apex trigger on after insert of opportunity which creates the new OpportunityContactRole row linked to the opportunity and the contact; then, as soon as the user hits save, he'll see that row on the detail page, as if by magic.
saracsarac
Thanks for your response. I am unfortunately still confused about how to set this information with a Trigger when you are creating the opportunity off of a record type *other than* contact.

So for example, we are creating one type of opportunity off of an existing opportunity. I have written an S-control/button to pre-populate the new opportunity record, and have pulled the contact information in, but how would that information be passed to the trigger when it's only in a query parameter? Unfortunately there is no such field type as "Hidden" which would solve this problem.

Here's my s-control which prepopulates the new opportunity from the existing opportunity and opportunitycontactrole objects
(Please feel free to suggest a cleaner way of doing this if possible):
Code:
<!-- Get the AJAX Toolkit -->
<script src="/soap/ajax/10.0/connection.js"></script>
<script language="JavaScript">

var firstName;
var lastName;
var oppId = "{!Opportunity.Id}";
var contactId;

function getContact(){
  var result=sforce.connection.query("select id, contactId from OpportunityContactRole WHERE opportunityId ='"+oppId+"'"); 
  var record=result.getArray("records");
  contactId = record[0].ContactId;
 
  if (contactId != null)
  {
    var result2 = sforce.connection.retrieve("FirstName, LastName", "Contact", [contactId]); 
    if (result2[0] != null)
    {
      firstName = result2[0].FirstName;
      lastName = result2[0].LastName;
    }
    else
    {
      throw "Could not retrieve Contact";
    }
  } 
  else
  {
    throw "Could not retrieve Contact ID";
  }
}

function redirect() {
parent.frames.location.replace("/006/e—retURL=%2F{!Opportunity.Id}&RecordType=012500000009IDP&opp3="+firstName+", "+lastName+"&opp11=Enrolled&opp9={!Today}&opp4={!Organization.Name}&opp4_lk={!Organization.Id}&CF00N50000001iZrA={!Opportunity.Name}&CF00N50000001iZrA_lk={!Opportunity.Id}");
}
getContact();
redirect();
</script>

 But I am not clear on how my trigger would access the contact information (which is in the OpportunityContactRole of the parent Opportunity, and a query parameter in the URL). I think the only possibility is to have the parent opportunity ID pre-populated into a lookup field in the new opportunity and work backwards from there?

Thanks so much!
Sara

HardhatHardhat
An Apex trigger is a different animal than an Scontrol.  It gets run after the opportunity is saved on the server side, not on the client.  It is not Javascript.  It doesn't matter what object you came from to begin with.  In the trigger, then, you already have all the information that is in the opportunity, and you can use that information to generate your OpportunityContactRole.

If you'd like to learn more about triggers, look up the Force.com Cookbook; it's got some good example code.
HardhatHardhat
That said, though, yes, you will have to have some knowledge from the Opportunity of what contact you'll be wanting.  Using a parent Opportunity lookup field would be one valid method.
saracsarac
Hi Hardhat,

Yes, i am familiar with triggers, I am actually a backend Java/Oracle developer so that part comes *much* more easily to me than the Javascript side!

Specifically I just wasn't sure if there was a mechanism of passing info from the client to the server side (trigger) other than the obvious solution of having it stored in a field in the record for the backend to pull from.

We need for each opportunity to have the contact mapped to it automatically upon creation and short of sticking the contact ID in a read-only field for the trigger to run off of, I don't see how to make it work. Especially since we are creating opportunities off of: contacts, other opportunities, and custom objects.

The downside to that solution is that you are replicating data - a field with the contact iID and an opportunitycontactrole with the same info. then it because important to keep both of those in sync, which requires more trigger work... sigh.

We may just decide to have a contact field on the opportunity and ignore the opportunitycontactrole all together since for us each opportunity can only have one contact. I just hate to ignore a commonly used construct for compatibility reasons.

Ideas?
Thanks!!
Sara
HardhatHardhat
Well, yes, I was going to suggest just adding a lookup field from Opportunity to Contact, and that does sound like it might be the best option for you.  What compatibility concerns do you have?
 
I guess one idea, if you don't want to be duplicating data and you want to use OpportunityContactRole, would be to make a trigger that makes an OpportunityContactRole out of the contact field and then clears out the field.  That's about it though -- other than that it sounds like you've thought of everything.
saracsarac
Thanks very much for the advice. I looked at the non-profit template which does have a "New Donations" button off of the Contact that creates a new OpportunityContactRole upon save.

Tucker seems to have made a hidden field on the opportunity called conid which is populated by a query parameter he passes in. That was pre-trigger time so he has some sort of backend code that then reads that field upon save and does the creation.

If only there was a field type of "Hidden" available! that would make everything much easier.

Thanks again,
Sara