+ Start a Discussion
Lago S.p.a.Lago S.p.a. 

Update a triggered object with a trigger

Hi everyone! I'm a little bit frustrated 'cause I'm trying to write a trigger like this:
trigger MediaTrigger on Media__c (after insert) {

for(integer i=0;i<trigger.new.size();i++){

    if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP'){

            List<task_apt__c> att=[select a.Id from task_apt__c a where a.Id=:trigger.new[i].task__c];

            task_apt__c attivita = att.get(0);

            List <Candidatura_Redesigner__c> cand=[select c.Id from Candidatura_Redesigner__c c where c.Lead__c=:attivita.lead_name__c];

            Candidatura_Redesigner__c cnd=cand.get(0);

            List<Media__c>md=[select m.Id from Media__c m where m.Id=:Trigger.new[i].Id];

            Media__c media=md.get(0);

            media.cand_redesigner__c=cnd.Id;

            update media;

        }

    }

}
But the object named media won't be updated after inserted.
Of course I'm wrong ..but I would like to know where is the error because I'm not able to find it out!
Thank you and have a nice day :)
Best Answer chosen by Lago S.p.a.
William TranWilliam Tran
Did you write test cases to see if you have 100% coverage?  

For example,  if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP') always return false then nothing will happen.

Also you syntax is unusual, typically the syntax is:

if (Trigger.isInsert)
{ for (Media__c a : Trigger.new) { ............Then use the variable a to represent the Media_c instances......}}

Last, you should check each object to see if the object is null.  
You can use System.assert() to halt the process if any of these turn out to be nulls and you did not expect it to be.

Try again and post more questions if you run into issues.

Thx


 

All Answers

William TranWilliam Tran
Did you write test cases to see if you have 100% coverage?  

For example,  if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP') always return false then nothing will happen.

Also you syntax is unusual, typically the syntax is:

if (Trigger.isInsert)
{ for (Media__c a : Trigger.new) { ............Then use the variable a to represent the Media_c instances......}}

Last, you should check each object to see if the object is null.  
You can use System.assert() to halt the process if any of these turn out to be nulls and you did not expect it to be.

Try again and post more questions if you run into issues.

Thx


 
This was selected as the best answer
Jason HardyJason Hardy
I agree with what William has stated, I would also highly recommend that you do not hard code your record type ID's! It's probalmatic in the long term. I would suggest creating the following method, and calling it before your loop. It will return a map of recordtypeinfo with the Id as the key. Then call the other function in your loop (getRtypeName), and compare THAT against the name instead. This will enable you to move from sandbox to sandbox with ease as long as the name remains the same. The best way to do something like this is to also have a custom setting which would contain the name of the record type, that way your admins can modify it on the fly without needing to re-deploy, but I would look at that for your next iteration :)
/**
 * @descirption Will return a map of record types with the name of recordtype as the key
 * @param objectName the name of the object that will have their recordtypes described
 * @return map<String, Schema.RecordTypeInfo> map of the record type name to the actual record type
 */
private map<id, Schema.RecordTypeInfo> getRecordTypeMapByID(string objectName)
{
	list<Schema.DescribeSobjectResult> description = descirbeSobject(objectName);
	Schema.DescribeSObjectResult d = description.get(0);

	return d.getRecordTypeInfosById();
}

private static String getRtypeName(map<id, Schema.RecordTypeInfo> g_id2RType, String rtId)
{
    String returnString = '';
    if(g_id2RType!=null && rtId!=null)
    {
        if(g_id2RType.containsKey(rtId))
        {
            returnString = g_id2RType.get(rtId).getName();
        }
    }
    
    return returnString;
}
Lago S.p.a.Lago S.p.a.

Hi everyone and thanks for your kind reply!
I've undestood all your comments, but I've found a workaround keeping my code as it is, and changing this line
  if (trigger.new[i].task__r.RecordTypeId=='01213000001WGQP'){
with this:

List<task_apt__c> att=[select a.lead_name__c, a.RecordTypeId from task_apt__c a where a.Id=:trigger.new[i].task__c];
    task_apt__c attivita = att.get(0);
    System.debug('attivita='+attivita);
    if (attivita.RecordTypeId=='01213000001WGQP')

maybe in Apex trigger probably the relationship field could be problematic :)
thanks and have a nice day :)