Error
This content is not currently supported on this browser.
+ Start a Discussion
feliksfeliks 

Trigger for update fields in Custom-Object by Task

Hi at all,

 

i'm a newbie in triggers and i hope someone can help me?

The function should be:

There is a Task, related to a Custom object (1:1). If the Task is updated (Status = Completed), some informations  should transferred and updated to the related object__c (Task.custom_code_c --> object__c.custom_code__c and object__c.closed=true)

 

Could somebody give me an advice?

 

Thanks

 

feliks

 

Imran MohammedImran Mohammed

Hi,

 

Make changes accordingly to the code.

I assumed that you wanna update object when Task related to that object reaches Completed status.

Correct me if i am wrong.

 

trigger taskOnObject on Task (after update)

{

List<ID> objectToBeUpdatedIDList = new List<ID>();

 

for(Task t: Trigger.new)

{

if(t.status == 'Completed')

{

objectToBeUpdatedIDList.add(t.whatid);

}

}

Map<ID, Object__c> objMap = new Map<ID, Object__c>([select id, fieldToBeUpdated1__c, fieldToBeUpdated2__c from Object__c where id in :objectToBeUpdatedIDList ]);

List<Object__c> objList = new List<Object__c>();

for(Task t: Trigger.new)

{

if(t.Status__c == 'Completed')

{

Object__c o = objMap.get(t.whatid);

o.fieldToBeUpdated1__c = 'Value you want to update';

//update remaining fields.

objList.add(o);

}

}

if(objList.size() > 0)

update objList;
}

 

Let me know if any issues faced.

feliksfeliks

Imran,

 

thats great! I've just modified the code and it works fine :-)

Now i've got two questions:

1. One Update should a transfer of a Value from the Task to a field in the custom object.

My attempt:

 

 

 

Object__c o = objMap.get(t.whatid);

//o.fieldToBeUpdated1__c = 'Value you want to update';

o.fieldToBeUpdated1__c = Task.custom_field__c;

//update remaining fields.

 

While compile i receive an error:

 

Illegal assignment from Schema.SObjectField to Decimal (--> But both fields are decimal [18,9])

 

 

2. I forgott that the trigger sould be for Tasks with a special Record-Type (When RecType=B then fire the trigger)

 

How can i solve this?

 

Thanks for your help.

 

regards

 

feliks

Imran MohammedImran Mohammed

Can you post the actual code so that it can be helpful to find out the issue?

When a trigger is written on an object using after update event for example,  it will get fired whenever an update is done on that object.

 

For further filtering records, you have to write custom logic to do that.

In my previous post i am filtering and gathering only the records that have Completed status.

Similarly, you have to filter based on the Record Type inside the trigger.

 

If you post the entire code, i can give you directions on where to make changes.

feliksfeliks

...i tried something like this:

I solved the transfer in a list.

 

 

trigger anoShipmentUpdater on Task (after update) {
	
	if (trigger.size > 10) return;
	
	public List <Shipment__c> ssL = new List <Shipment__c>();
	
	for(Task t: Trigger.new) {
	//search	
		if((t.RecordTypeId == '00000000000000cAAA') && (t.Status == 'Completed')) {
	
	//check		
			if (t.WhatId != null) {
	
	//List 		
				ssL = [select Id, ano_order_number__c, Logistic_checked__c
					   from Shipment__c 
					   where id = :t.WhatId
					   limit 1];
					   
				if (ssL.size() > 0) {
					
					if (t.ano_order_number__c != 'null') {
						
						ssL[0].ano_Order_Number__c = t.ano_order_number__c;
						ssL[0].Logistic_checked__c = true;
						update ssL[0];
						
					}	
				}
	//Error-Message			
				else t.whatId.addError('Reference to Shipment not found.');
			}
			
			else t.whatId.addError('You have to specify a relation to a Shipment.');	
					
		}
	}
}

 

The trigger works fine.

 

Thank you for bring me in right direction :-)

 

 

 

Imran MohammedImran Mohammed

Are you still facing an error or are you done with the solution?

feliksfeliks

Imran,

 

at this time it's done. No errors :-)

Maybe when i extend it, i'll post a new question.

 

thank you

emandra_kfemandra_kf

I realize this is a pretty old thread but the topic is spot on with what I'm attempting to do. I've updated my code based on your sample, but am getting an error with line #9: objectToBeUpdatedIDList.add(t.whatid);

 

The error message is:  Incompatible element type Id for collection of SOBJECT:

 

From other threads, this seems to occur pretty easily with strongly typed lists. Nonetheless, I'm not sure what the solution might be. Any suggestions are appreciated.

 

Best, KF