+ Start a Discussion
tedevangtedevang 

Associating existing task with new opportunity

I have added a custom button on my task screens which allow our sales reps to create an opportunity directly from the task screen. The button populates certain fields in the new opportunity. What I would like to do is then associate the task that spawned the opportunity with the opportunity that was just created. Is there anything I can do in the button script to do this? Any other way? Below is a copy of the button script:

 

navigateToUrl('/006/e?retURL=%2F{!Account.Id}&accid={!Account.Id}&CF00N80000003yiZu={!Task.Show__c}&00N80000004lILD={!URLENCODE(Task.Description)}&opp17={!Campaign.Name}&conid={!Contact.Id}&opp9={!Today}&opp3=Auto Name on Save&opp11=Verbal Commitment');

 

I was thinking that I might be able to pass the task ID along as one of the parameters and somehow associate it with the opportunity but am unsure how to do this. Help would be greatly appreciated.

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

You can not associates existing task directly to newly created opportunity just by passing as URL parameter [ because this time you dont have any Opportunuty Id ]

 

here is solution

 

1)Create a text field(RelatedTask__c) on Opportunity.

 

2)pass Task.Id  as URL parameter and populate it into this text field(RelatedTask__c)

 

3)save opportunity.

 

4)Define trigger on Opportunity like

 

trigger swapTask on Opportunity<after insert>
{
List<Task> taskToSwap=new List<Task>();
for(Opportunity opp :Trigger.new)
{
if(opp.RelatedTask__c !=null)
{
Task t=new task(Id=opp.RelatedTask__c);
t.WhatId=opp.Id;
taskToSwap.add(t);
}
}
if(taskToSwap.size() > 0)
update taskToSwap;
}

 

******* If you create new field  RelatedTask__c with same API  , just pase above code in in trigger , It will run perfectly

 

This will associated existing task to newly created Opportunity

 

Hope this will help you

 

 

Thanks,

Bala

All Answers

b-Forceb-Force

You can not associates existing task directly to newly created opportunity just by passing as URL parameter [ because this time you dont have any Opportunuty Id ]

 

here is solution

 

1)Create a text field(RelatedTask__c) on Opportunity.

 

2)pass Task.Id  as URL parameter and populate it into this text field(RelatedTask__c)

 

3)save opportunity.

 

4)Define trigger on Opportunity like

 

trigger swapTask on Opportunity<after insert>
{
List<Task> taskToSwap=new List<Task>();
for(Opportunity opp :Trigger.new)
{
if(opp.RelatedTask__c !=null)
{
Task t=new task(Id=opp.RelatedTask__c);
t.WhatId=opp.Id;
taskToSwap.add(t);
}
}
if(taskToSwap.size() > 0)
update taskToSwap;
}

 

******* If you create new field  RelatedTask__c with same API  , just pase above code in in trigger , It will run perfectly

 

This will associated existing task to newly created Opportunity

 

Hope this will help you

 

 

Thanks,

Bala

This was selected as the best answer
tedevangtedevang

Thanks Bala - I will give this a try.