+ Start a Discussion
Jin Kaur 7Jin Kaur 7 

Auto populate Opportunity on Task and Event from Contact

I am trying to create a New Task and New Event button which should auto fill the Related To Opportunity Field to where the Contact (role) is associated.  The contact will only ever have 1 opportunity associated and will have a Contact Role.

I found this 

However, I am getting a create new contact page.

I want to eliminate the user having to look up an Opportunity - reason being is because the users make a lot of calls via New Voice Media and I want the Activity on the Opportunity instead of the Contact.

Any help asap would be appreciated!


To create a Task append the URL with : 
To create a Event append the URL with : 

I guess your URL will still not work, can you do let me know from where you are executing the new creation request, which object?
Jin Kaur 7Jin Kaur 7
So this will be from the Contact
This will not work, because you are coming from a Contact Page, where Opportunity is a child relationship (i.e., multiple is possible) and you cannot get only one value like this.
Jin Kaur 7Jin Kaur 7
What if one Contact will only ever be associated to one open Opportunity at any given time?
That's the restriction you are applying, in salesforce the relationship is one to many.
You will have to write a query in your javacript to get that one opportunity and make the URL accordingly
Something like this:
var query = "SELECT Id,Name from Account LIMIT 2"; 
var records = sforce.connection.query(query); 
var records1 = records.getArray('records'); 
var accountNames = ''; 
for(var i=0;i<records1.length;i++){ 
accountNames = accountNames + records1[i].Name + ','; 
if(records1.length == 1){ 
//window.location.href = 'http://www.google.com'; 
alert('There is no Account'); 
alert('An Error has Occured. Error:' +e); 

Reference : http://www.sfdcpoint.com/salesforce/soql-query-in-javascript-example/