You need to sign in to do that
Don't have an account?

Trigger to Update Custom Object status based on Task
Good evening,
my second post today...
so I'm trying to create a trigger that will update the status of a custom object (Sales_Order__c) based on if a related task is open or if task is completed:
Open Task > Update Sales Order Status to "Pending Question"
Completed Task > Update Sales Order Status to "Sales Order Revised"
I'm not close to being a developer, so some of my coding probably has some major flaws:
trigger SalesOrderUpdateonTask on task (after insert, after update ){
set<id> Sales_Order = new set<id>();
for(Task ts : trigger.new)
{
Sales_Order.add(ts.whatid);
Sales_Order =[Select status__c from Sales_Order__c where id in :Sales_Order];
if(String.valueOf(ts.whatId).startsWith('a1M')==TRUE)
{
Sales_Order__c.Status__c='Pending Question';
update Sales_Order;
}
}
}
On save, get a compile error: illegal assignment from List <Sales_Order__c> to set <id> at line 6...also, have another trigger to Update on task completion:
trigger SalesOrderStatusUpdate on Task (after update)
{
List<ID> Sales_Order = new List<ID>();
for(Task t: Trigger.new)
{
if(t.status == 'Completed')
{
Sales_Order__c.add(t.whatid);
}
}
Map<ID, Sales_Order__c> objMap = new Map<ID, Sales_Order__c>([select id, status__c from Sales_Order__c where id in :Sales_Order__cIDList ]);
List<Sales_Order__c> objList = new List<Sales_Order__c>();
for(Task t: Trigger.new)
{
if(t.Status__c == 'Completed')
{
Sales_Order__c so = objMap.get(t.whatid);
so.status__c = 'Sales Order Revised';
//update remaining fields.
objList.add(so);
}
}
if(objList.size() > 0)
update objList;
}
not sure how to change the variable Sales_Order__c IDlist on line 12 where it will accept, and how to combine the two triggers together into one. Again, excuse the basic errors since i'm not a developer, but trying to find existing codes that is similar to what i'm looking for. Thanks once again.
my second post today...
so I'm trying to create a trigger that will update the status of a custom object (Sales_Order__c) based on if a related task is open or if task is completed:
Open Task > Update Sales Order Status to "Pending Question"
Completed Task > Update Sales Order Status to "Sales Order Revised"
I'm not close to being a developer, so some of my coding probably has some major flaws:
trigger SalesOrderUpdateonTask on task (after insert, after update ){
set<id> Sales_Order = new set<id>();
for(Task ts : trigger.new)
{
Sales_Order.add(ts.whatid);
Sales_Order =[Select status__c from Sales_Order__c where id in :Sales_Order];
if(String.valueOf(ts.whatId).startsWith('a1M')==TRUE)
{
Sales_Order__c.Status__c='Pending Question';
update Sales_Order;
}
}
}
On save, get a compile error: illegal assignment from List <Sales_Order__c> to set <id> at line 6...also, have another trigger to Update on task completion:
trigger SalesOrderStatusUpdate on Task (after update)
{
List<ID> Sales_Order = new List<ID>();
for(Task t: Trigger.new)
{
if(t.status == 'Completed')
{
Sales_Order__c.add(t.whatid);
}
}
Map<ID, Sales_Order__c> objMap = new Map<ID, Sales_Order__c>([select id, status__c from Sales_Order__c where id in :Sales_Order__cIDList ]);
List<Sales_Order__c> objList = new List<Sales_Order__c>();
for(Task t: Trigger.new)
{
if(t.Status__c == 'Completed')
{
Sales_Order__c so = objMap.get(t.whatid);
so.status__c = 'Sales Order Revised';
//update remaining fields.
objList.add(so);
}
}
if(objList.size() > 0)
update objList;
}
not sure how to change the variable Sales_Order__c IDlist on line 12 where it will accept, and how to combine the two triggers together into one. Again, excuse the basic errors since i'm not a developer, but trying to find existing codes that is similar to what i'm looking for. Thanks once again.
Sorry about the delayed response. I tested this in my sandbox, so it should be working in yours as well, here you go:
trigger SalesOrderUpdateonTask on task (after insert, after update ){
Set<id> StatusOpen= new Set<Id>();
Set<id> StatusCompleted= new Set<Id>();
for (Task ts: trigger.new) {
//This condition grabs the whatId whenever the status of the task is anything besides Completed and changes the status of the customObject to Pending Question. You can change it whenever the status is Open or just In Progress
if (ts.status!='Completed') {
StatusOpen.add(ts.whatId);
}
else if (ts.status=='Completed') {
StatusCompleted.add(ts.whatId);
}
}
if (StatusOpen.size()> 0) {
List<Sales_Order__c> customObject= [Select id, status__c from Sales_Order__c where id =: StatusOpen];
for(Sales_Order__c thisObject: customObject) {
//Please make sure the picklist value is this; pending question, not sure but i think it is case sensitive
thisObject.status__c='pending question';
}
update customObject;
}
if (statusCompleted.size() > 0) {
List<Sales_Order__c> customObject= [Select id, status__c from Sales_Order__c where id =: statusCompleted];
for(Sales_Order__c thisObject: customObject) {
thisObject.status__c='sales order revised';
}
update customObject;
}
}
Thanks!
All Answers
Line 6: Sales_Order =[Select status__c from Sales_Order__c where id in :Sales_Order];
it should be List<Sales_Order__c> sorder=[Select status__c from Sales_Order__c where id in :Sales_Order];
so your trigger would be:
trigger SalesOrderUpdateonTask on task (after insert, after update ){
set<id> Sales_Order = new set<id>();
if (trigger.isInsert) {
for(Task ts : trigger.new)
{
Sales_Order.add(ts.whatid);
}
List<Sales_order__c> sOrder =[Select status__c from Sales_Order__c where id in :Sales_Order];
for (Sales_order__c sOrd: sOrder) {
if(String.valueOf(ts.whatId).startsWith('a1M')==TRUE)
{
sOrd.Status__c='Pending Question';
}
}
update Sales_Order;
}
if (trigger.isUpdate) {
for (Task ts:trigger.new) {
if(ts.status__c=='Completed')
Sales_Order.add(ts.whatid);
}
List<Sales_order__c> sOrder =[Select status__c from Sales_Order__c where id in :Sales_Order];
for (Sales_order__c sOrd: sOrder) {
sOrd.Status__c='Sales Order Revised';
}
update Sales_Order;
}
}
==================================================================================================
I think that should do it. Pretty much!!!
looks like it's giving a "Variable does not exist for the ts.whatId" at line 11, if(String.valueOf(ts.whatId).startsWith('a1M')==TRUE) ...
is it the way i'm referencing my custom object?
appreciate your help
Could you replace that line with
if(String.valueOf(Sales_Order.whatId).startsWith('a1M')==true)....
This is because at line 4 we created Task ts but we closed it at line 8, that is why you were getting the error saying; "Variable does not exist"
I had to update to Sales_Order__c.Id, which seems to take. But received an DML requires SObject or SObject list type: Set <ID> at line 17, update Sales_Order;
I'm not a developer but making an attempt, so some of my errors may be pretty basic
On both lines wherever you are performing the update, can you replace your code with this instead;
update sOrder;
Thanks!
I do have a question on whether i'm using the right object on this trigger: since I'm using the existence of an open Task to update the Sales Order status (custom object), should the trigger be built on task or on Sales_Order__c. It's now giving me an incorrect SObject type on Task, and looks like when i update to the Sales_Order__c (line 1) then the Loop variable on line 4 should be Sales_Order__c as well. I'm not an experienced developer so I'm wondering if the structure is correct on this trigger.....i would think that line 4 should stay on task.
thanks for your help Justin
Not a problem at all. We all learn more and more everyday.
Would you mind telling me your requirement so that I can provide you with a better solution. Please be specific about on what action what should happen?
Thanks!
so what i'm looking to do is have a trigger that will update the Status of a Custom object record (Sales_Order__c) depending on if there's an open Task on the record. So if a task is created from the Sales Order, then the Status (status__c) of the Sales Order becomes "Pending Question" (or if any task is open on the record, if more than one). And when the task(s) are completed, then the Sales Order status updates to "Sales Order Revised". Seems direct enough, which is why i'm attempting to build myself, of course there are always challenges.
thanks Justin
Sorry about the delayed response. I tested this in my sandbox, so it should be working in yours as well, here you go:
trigger SalesOrderUpdateonTask on task (after insert, after update ){
Set<id> StatusOpen= new Set<Id>();
Set<id> StatusCompleted= new Set<Id>();
for (Task ts: trigger.new) {
//This condition grabs the whatId whenever the status of the task is anything besides Completed and changes the status of the customObject to Pending Question. You can change it whenever the status is Open or just In Progress
if (ts.status!='Completed') {
StatusOpen.add(ts.whatId);
}
else if (ts.status=='Completed') {
StatusCompleted.add(ts.whatId);
}
}
if (StatusOpen.size()> 0) {
List<Sales_Order__c> customObject= [Select id, status__c from Sales_Order__c where id =: StatusOpen];
for(Sales_Order__c thisObject: customObject) {
//Please make sure the picklist value is this; pending question, not sure but i think it is case sensitive
thisObject.status__c='pending question';
}
update customObject;
}
if (statusCompleted.size() > 0) {
List<Sales_Order__c> customObject= [Select id, status__c from Sales_Order__c where id =: statusCompleted];
for(Sales_Order__c thisObject: customObject) {
thisObject.status__c='sales order revised';
}
update customObject;
}
}
Thanks!
I'm not sure what you are saying. Are you not able to send this code? I have the same code in my sandbox and it is working fine.
Could you verify the API name of the sObject as well as the fields and the picklist values?
Thank!
thanks for your help, figured out why i was getting that sOject error... I was trying to save the trigger under the custom object instead of the task. Once i saved under Task, your code works! Appreciate all your help on this, you're an asset.
-danny