+ Start a Discussion
devsalesforce27devsalesforce27 

Urgent: Apex Trigger not updating Look up field

Hey , 

 

I have a situation in which whenever a TASK is created , it should automatically create a new record in custom object ( XYZ__C) by taking values from the custom fields in TASK object.  Custom objec(XYZ__C) has 3 fields , 2 picklists and one is lookup field. My trigger is fetching the values for picklists but it is not fetching for lookup field. Its is giving the below error. Trigger is also mentioned below . I have checked all the previous threads also. Anyone who can fix the below code would be great

 

Apex trigger createleadinterest caused an unexpected exception, contact your administrator: createleadinterest: execution of AfterUpdate caused by: System.StringException: Invalid id: 2012 Fall: Trigger.createleadinterest: line 13, column 1

 

trigger createleadinterest on Task (after insert,after update) {
    List <XYZ__c> leadinterest = new List <XYZ__c>();
   
    for (Task tk : Trigger.new) {
        
        
        if (tk.Type == 'Web Form') {
        
        XYZ__c ld = new XYZ__c (); //instantiate the object to put values for future record
        
        
        ld.Primary_Source__c = tk.Task_Source__c; // and so on so forth untill you map all the fields. 
        ld.Term__c = tk.Enrollment_Term__c; // Term__c is a look up field on XYZ__c 
        
        leadinterest.add(ld);
        
        }
        
    }
    
    try {
        insert leadinterest; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
    
}

 

Sean TanSean Tan

How does the Enrollment_Term__c link to the Term record in Salesforce? So your XYZ object has a link to the Term__c but the Task's Enrollment_Term__c is not a Term__c Id... what is it?

 

In any case that question should give you the direction you need to go... try something like this (you'll have to fix it to work with your objects)

 

trigger createleadinterest on Task (after insert,after update) {
    List <XYZ__c> leadinterest = new List <XYZ__c>();
    Map<String, Term__c> termMap = new Map<String, Term__c>{};
    
    for (Task tk : Ttrigger.new) {
        if (tk.Type == 'Web Form' && tk.Enrollment_Term__c != null)
        {
            termMap.put(tk.Enrollment_Term__c);
        }
    }
    
    //Change this to the appropriate object name and filter criteria...
    for (Term__c item : [ select Id, Name from Term__c where Name IN :termMap.keySet()) ]
    {
        termMap.put(item.Name, item);
    }
    
    for (Task tk : Trigger.new) {
             
        if (tk.Type == 'Web Form') {
            
            XYZ__c ld = new XYZ__c (); //instantiate the object to put values for future record
            
            
            ld.Primary_Source__c = tk.Task_Source__c; // and so on so forth untill you map all the fields.
            
            Term__c term = termMap.get(tk.Enrollment_Term__c);
            
            if (term != null)
            {
                ld.Term__c = term.Id; // Term__c is a look up field on XYZ__c
            }
            
            leadinterest.add(ld);
            
        }
        
    }
    
    try {
        insert leadinterest;
        } catch (system.Dmlexception e) {
        system.debug (e);
    }
    
}

 Again this will probably not compile for you but should give you a better idea of what you need to do.

devsalesforce27devsalesforce27

I think I was not able to present the question clearly. But I tweaked your solution its not working. It actually confused me more. Let me try to rephrase the question. Enrollment_term__c is a field on Task object and Term__c is field on XYZ__C object. This Term__c field is also look up field on sme other object. So in the scenario , only two objects ( TASK and XYZ__c ) are involved. I just want whenever new task is created , it should trigger new record in XYZ__c by taking the field values from TASK object and populating it to record in XYZ__c object. 

I would really appreciate if you can tweak my slution and let me what I am missing 

 

Thanks

Sean TanSean Tan

I understood what you're saying. What I was recommending to you is based off the error you posted:

 

Apex trigger createleadinterest caused an unexpected exception, contact your administrator: createleadinterest: execution of AfterUpdate caused by: System.StringException: Invalid id: 2012 Fall: Trigger.createleadinterest: line 13, column 1

 

So, based off you're error message, the Task object's Enrollment_term__c is populated with the value: 2012.

 

The value 2012 is not a valid Salesforce Id, and that is why you can't populated it on the Term__c field of the XYZ__c object. So try to see if it's one of the following issues:

 

  1. Validate that whatever is creating the task is populating a proper Id from Object A into the Task's Enrollment_term__c field. If you ensure the Id is proper then your trigger will work correctly.
  2. If #1 is not true, then find out what from Object A is being populated in the Enrollment_term__c field. From there your trigger needs to query Object A matching the field to get the proper Id's to put into the XYZ__c.Term__c field. (This is what I was suggesting for you to do in my first post)