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
developer_forcedeveloper_force 

Update a task field

Hi,

 

Help needed in updating a custom field on Task

 

I have a custom field calld Sales_Targer_Id__c on task object and this field need to be get updated by the Id of Sales_Target__c object

 

And the condition for that is if the officer_code__c of Task is equal to Officer_code__c of sales_Target__c object then update the Sales_Targer_Id__c custom field with the id of the Sales_Target__c custom object.

 

Thanks

fgwarbfgwarb

Assuming that the task is a child of the Sales_Target__c object  (task.WhatId = Sales_Target__c) I'd write a trigger that like this:

 

trigger task_before(before insert, before update){

String keyPrefix = Sales_Target__c.sObjectType.getDescribe().getKeyPrefix(); //the 3 character sequence that all ids for the Sales_Target__c table begin with

for(Task t : trigger.new){

String strWhatId = t.WhatId; //so we can use the String.startsWith(string) method

if(strWhatId != null && strWhatId.startsWith(keyPrefix)) t.Sales_Target_Id__c = strWhatId; //test to see if this task is related to a Sales_Target__c record, and if it is, then copy the value over to your custom field

}

}

 

I've not compiled or tested the trigger, so it might need some polishing

developer_forcedeveloper_force

Thank you for your response.

 

But This an after insert trigger on Sales_Target__c object.

And there is no relation between Task and Sales_Target__c object. Task object just has a Custom field 'Sales_Target__c' which is of data type Text(18)

 

This custom field on task should get updated with the Sales_Target__c object's id.

 

Thanks

fgwarbfgwarb

So, after a Sales_Target__c record has been inserted you want to create a new task with the ID from the newly created Sales_Target__c in the Task.Sales_Target_Id__c field?

developer_forcedeveloper_force

After the Sales_Target__c records are inserted , the custom field 'Sales_Target_ID__c' on Task should get updated with the id of Sales_Target__c object based on the following condition

 

if(

task.Officer_code__c = Sales_Target__c.Officer_code__c and

task.Activitydate <= Sales_Target__c.Target_Period_End__c and

task.Activitydate <= Sales_Target__c.Target_Period_Start__c ) this si condition is met then update the custom field on task

 

Hope its clear now. Its kind of urgent pls

 

Thanks

fgwarbfgwarb
trigger Sales_Target_After(after Insert){

map<String, Id> offCode2stId = new map<String, Id>(); //store the officer code against the Sales Target id
Date targetEnd = system.today();
Date targetBeg = system.today();
for(Sales_Target__c st : trigger.new){
 offCode2stId.put(st.Officer_Code__c, st.Id);
 if(st.Target_Period_End__c > targetEnd) targetEnd = st.Target_Period_End__c;
 if(st.Target_Period_Start__c < targetBeg) targetBeg = st.Target_Period_Start__c;
}

Task[] tasksToUpdate = new Task[]{};
for(Task t : [SELECT Id, ActivityDate FROM Task WHERE ActivityDate <= :targetEnd AND ActivityDate => :targetStart AND Officer_Code__c IN :offCode2stId.keySet()]){

Sales_Target__c st = offCode2stId.get(t.Officer_Code__c);
if(t.ActivityDate <= st.Target_Period_End__c && t.ActivityDate >= st.Target_Period_Start__c){
 t.Sales_Target_Id__c = st.Id;
 tasksToUpdate.add(t);
}

}

if(!tasksToUpdate.isEmpty()) update tasksToUpdate;

}

 

This is closer then... The challenge in your requirement is that each Sales_Target__c has its own start and end date, and querying one by one for the appropriate task is going to hit the Governor limits.  

 

So the trigger finds the earliest start date, and the latest end date and finds all tasks that occur within that range where the Officer code is in the Sales_Target__c set being processed.

 

It then tests each of the tasks to ensure that the task occurs within the time frame of the appropriate Sales_Target__c.

 

developer_forcedeveloper_force

Thank You so much for the quick response but iam getting the error as 

 

Illegal assignment from Id to SOBJECT:Sales_Target__c at line 15 column 1 in the below line

 

Sales_Target__c st = offCode2stId.get(t.Officer_Code__c);

 

Thanks

fgwarbfgwarb
trigger Sales_Target_After(after Insert){

map<String, Sales_Target__c> offCode2st = new map<String, Sales_Target__c>(); //store the officer code against the Sales Target id
Date targetEnd = system.today();
Date targetBeg = system.today();
for(Sales_Target__c st : trigger.new){
 offCode2st.put(st.Officer_Code__c, st);
 if(st.Target_Period_End__c > targetEnd) targetEnd = st.Target_Period_End__c;
 if(st.Target_Period_Start__c < targetBeg) targetBeg = st.Target_Period_Start__c;
}

Task[] tasksToUpdate = new Task[]{};
for(Task t : [SELECT Id, ActivityDate FROM Task WHERE ActivityDate <= :targetEnd AND ActivityDate => :targetStart AND Officer_Code__c IN :offCode2st.keySet()]){

Sales_Target__c st = offCode2st.get(t.Officer_Code__c);
if(t.ActivityDate <= st.Target_Period_End__c && t.ActivityDate >= st.Target_Period_Start__c){
 t.Sales_Target_Id__c = st.Id;
 tasksToUpdate.add(t);
}

}

if(!tasksToUpdate.isEmpty()) update tasksToUpdate;

}

 

New_DeveloperNew_Developer

HI fgwarb,

 

Thanks for the reply

 

Iam the same one holding two usernames , developer_force and this.

 

I tried with yor code but still the Sales_Target__c lookup field on Sales_Opportunity__c object is not getting updated with the id of the Sales_target__c object.

 

pls let me know what can be done. Its urgent pls

 

 

Thanks

fgwarbfgwarb

Hello Again!

 

We've been discussing Task & Sales_Target__c objects.  How does Sales_Opportunity__c fit into your requirement?

New_DeveloperNew_Developer

Yep sorry,

 

There is also the same scenario as task with the Sales_opportunity__c as well

 

Sorry again. Lets work with the Task first then i can do the same thing with the sales_Opportunity__c as well.

 

Task[] tasksToUpdate = new Task[]{};
for(Task t : [SELECT Id, ActivityDate
FROM Task
WHERE ActivityDate <= :targetEnd
AND ActivityDate >= :targetBeg
AND Officer_Code__c IN :offCode.keySet()]){

Sales_Target__c st = offCode.get(t.Officer_Code__c);
if(st.Officer_Code__c!=null){
if(t.ActivityDate <= st.Target_Period_End__c && t.ActivityDate >= st.Target_Period_Start__c){
t.Sales_Target_Id__c = st.Id;
tasksToUpdate.add(t);
}
}
}

 

With the above code , thes Sales_Target_ID__c text field is not getting updated with the id of Sales_target__c

 

Thanks