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
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 
/003/e?Title=Call&who_id=(!Contact.ID)&retURL=(!Contact.Id)&what_id=(!Opportunity.Id)

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!

Thanks

 
VineetKumarVineetKumar
To create a Task append the URL with : 
/00T/e
To create a Event append the URL with : 
/00U/e

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
VineetKumarVineetKumar
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?
VineetKumarVineetKumar
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:
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 
try{ 
var query = "SELECT Id,Name from Account LIMIT 2"; 
var records = sforce.connection.query(query); 
var records1 = records.getArray('records'); 
alert(records); 
var accountNames = ''; 
for(var i=0;i<records1.length;i++){ 
accountNames = accountNames + records1[i].Name + ','; 
} 
alert(accountNames); 
if(records1.length == 1){ 
//window.location.href = 'http://www.google.com'; 
}
else{ 
alert('There is no Account'); 
} 
} 
catch(e){ 
alert('An Error has Occured. Error:' +e); 
}

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