• Microbe
  • NEWBIE
  • 10 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies

I want to create a task within Apex, and want it to be associated with an opportunity. I'm using code as follows (assuming o is a reference to a valid Opportunity):

 

 

Task t = new Task(    
  Subject = 'Subject of task',
  WhatId = o.Id,
  Description = 'This is the description text',
  Status = 'Not Started');

insert t;

This works fine, the task is created. However, if I create an After Insert trigger on the task, the task's What.Type property is null, instead of "Opportunity". The WhatId property correctly references the opportunity's ID.

 

Am I missing a step?

 

 

I'm trying to cause data to be copied when a trigger occurs. When a task attached to an opportunity is created with a subject of "Week In Review", I want it to update two custom fields in the parent opportunity. One of the fields will be updated with the task's description, and the other field will be updated with the current date and time.

 

I've tried a few different things, and this is close to what I want - but it's not right, syntactically or functionally.

 

Please be gentle, this is the very first piece of Apex code I have ever written.

 

 

 

trigger ReviewActivity on Task (after update) {

List<Task> taskIds = [SELECT Id, WhatId FROM Task WHERE Id IN :Trigger.new AND What.Type='Opportunity' AND Subject='Week In Review'];

    Map<Id, Task> taskMap = New Map<ID, Task>([SELECT WhatId, Description FROM Task Where Id IN :taskIds]);

List<Opportunity> opps = [SELECT Id, Last_Week_In_Review__c, Last_Week_In_Review_Date_Time__c From Opportunity WHERE Id IN taskIds.WhatId];

for(Opportunity opp : opps)
{
opp.Last_Week_In_Review__c = taskMap(opp.Id).Description;
opp.Last_Week_In_Review_Date_Time__c = System.now();
}

update opps;
}

 

 

 

I want to create a task within Apex, and want it to be associated with an opportunity. I'm using code as follows (assuming o is a reference to a valid Opportunity):

 

 

Task t = new Task(    
  Subject = 'Subject of task',
  WhatId = o.Id,
  Description = 'This is the description text',
  Status = 'Not Started');

insert t;

This works fine, the task is created. However, if I create an After Insert trigger on the task, the task's What.Type property is null, instead of "Opportunity". The WhatId property correctly references the opportunity's ID.

 

Am I missing a step?

 

 

I'm trying to cause data to be copied when a trigger occurs. When a task attached to an opportunity is created with a subject of "Week In Review", I want it to update two custom fields in the parent opportunity. One of the fields will be updated with the task's description, and the other field will be updated with the current date and time.

 

I've tried a few different things, and this is close to what I want - but it's not right, syntactically or functionally.

 

Please be gentle, this is the very first piece of Apex code I have ever written.

 

 

 

trigger ReviewActivity on Task (after update) {

List<Task> taskIds = [SELECT Id, WhatId FROM Task WHERE Id IN :Trigger.new AND What.Type='Opportunity' AND Subject='Week In Review'];

    Map<Id, Task> taskMap = New Map<ID, Task>([SELECT WhatId, Description FROM Task Where Id IN :taskIds]);

List<Opportunity> opps = [SELECT Id, Last_Week_In_Review__c, Last_Week_In_Review_Date_Time__c From Opportunity WHERE Id IN taskIds.WhatId];

for(Opportunity opp : opps)
{
opp.Last_Week_In_Review__c = taskMap(opp.Id).Description;
opp.Last_Week_In_Review_Date_Time__c = System.now();
}

update opps;
}

 

 

 

Hi There!

 

I have a need to update a date field on an opportunity when the task status is changed to completed.Workflow as follows:

 

Task marked "Completed" > Field "Follow up date" = TODAY() + 5

 

Pretty simple, but I will be using this trigger as framework for other triggers to get all our reps in line to follow through with a process, so it's pretty important. Thanks anyone who's willing to help.

 

 

Hi. I'm looking to create some Apex code that changes the record type of an opportunity to 'Multi' when brands/products from more than one Product Family are added to a single opportunity.

 

So the workflow is this:

 

1. User clicks New button to create a new opportunity

2. User chooses a record type for that opty -- Media, Events, etc.

3. User fills in opportunity info and chooses various products/brands to attach to that opty. Each brand/product has a Product Family.

4. Apex code is triggered when brands/products are added to the opty that are from more than one Product Family (not necessarily just two or more products, which could all be from the same Product Family) -- this would trigger the record type for that opty to be a) changed to Multi and b) the record type set to read-only. (optional) If the products/brands for that opty are changed again, the record type would revert to the original record type.

 

I'm not an Apex developer and I'm not sure where to start here. I'm hoping someone can point me in the right direction. Thanks in advance.