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
dcgb2332dcgb2332 

Auto Populate Job Number (custom field) in Opportunity Tasks (pulling from Opportunity_Number__c)

I've tried work flow rules, process builder and apex triggers and keep running into a wall. Does anyone have a best practice for this? I simply need th Opportunity Record ID Number populated into my custom Job Number field in the Task in Lightning (shows up in Classic but lightning for some reason??)

Apex trigger or formula suggestions would be greatly appreciated.

Thanks!
Navin Selvaraj23Navin Selvaraj23
Hi Sabrina,

I have written the trigger on task object to update job number field in task object from the opportunity number field from opportunity object.
 
trigger TaskTrigger on Task (before insert,after insert) {
    List<Id> taskIds = new List<Id>();
    List<Id> oppIds = new List<Id>();
    Map<Id,Decimal>  oppMap = new Map<Id,Decimal>();
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            for(Task taskObj : Trigger.New){
                taskIds.add(taskObj.Id);
                //WhatId is the opportunity Id when creating task from opprotunity
                oppIds.add(taskObj.WhatId);
            }
            List<Task> tskObjLst = [SELECT Id,WhatId FROM Task where Id in: taskIds ];    
            List<Opportunity> oppLst = [SELECT Id,Opportunity_Number__c FROM Opportunity where Id in: oppIds ];   
            for(Opportunity oppObj : oppLst){
                oppMap.put(oppObj.Id,oppObj.Opportunity_Number__c);
            }
            for(Task taskObj : tskObjLst){
                taskObj.Job_Number__c = oppMap.get(taskObj.WhatId);
            }
            update tskObjLst;
        }
    } 
}
This is working for me. Please check it and let me know if it helps for you.

Note: If it helps for you, Please mark it as the best answer.

Best Regards,
Navin 
dcgb2332dcgb2332
Hi Navin, I received the following errors: [cid:image001.png@01D4E633.67129A60]
Navin Selvaraj23Navin Selvaraj23
Hi Sabrina,
Are you refering any images in your code? Could you please explain the requirement little more? As per your requirement, the above trigger wil help you to update the task job number with opportunity number. Is there any other jobs that you are simultaneously doing?
dcgb2332dcgb2332
Navin, The only other trigger I’m running is an “overwrite” of the Created By field: trigger Created_By on Task (before insert) { for (Task taskRe :Trigger.New) { taskRe.CreatedById = taskRe.OwnerId; } } Thanks! Sabrina Bokhari Service Coordinator Midwest Engineered Systems Inc. W238N1800 Rockwood Drive Waukesha, WI 53188 Direct Tel: (262) 522 1814 Email: sabrinab@mwes.com Visit our Website: www.MWES.com
Navin Selvaraj23Navin Selvaraj23
Hi Sabrina,
Please put debug on trigger and check where the exception occurs in trigger. if trigger successfully runs, then there will be no error in the trigger or the code i provided.
This error is from some othe parts of your implementation apart from trigger implementation.

Regards,
Navin S
dcgb2332dcgb2332
Navin,
I'm at a loss, here are the errors I'm receiving:

Line 15 - Method does not exist or incorreect signature: void put(Id, String) from the type Map
Line 18 - Illegal assignment from Decimal to String

Thank you for your help, I really appreciate it. 
dcgb2332dcgb2332
Ok, I managed to parse out those errors and there's nothing showing up in the Debug log, but it's still not populating the number in the Job Number field:

trigger TaskTrigger on Task (before insert, after insert) {
    List<Id> taskIds = new List<Id>();
    List<Id> oppIds = new List<Id>();
    Map<Id,Id>  oppMap = new Map<Id,Id>();
    if(Trigger.isAfter){
        if(Trigger.isInsert){
           for(Task taskObj : Trigger.New){
                taskIds.add(taskObj.Id);
            }
            List<Task> tskObjLst = [SELECT Id,WhatId FROM Task where Id in: taskIds ];   
            List<Opportunity> oppLst = [SELECT Id,Opportunity_Number__c FROM Opportunity where Id in: oppIds ];  
            for(Opportunity oppObj : oppLst){
                oppMap.put(oppObj.Id,oppObj.Opportunity_Number__c);
            }
            for(Task taskObj : tskObjLst){
                taskObj.Job_Number__c = oppMap.get(taskObj.WhatId);
            }
            update tskObjLst;
        }
    }
}
Navin Selvaraj23Navin Selvaraj23
Sabrina,
in first for loop(Task loop using Trigger.New), you have missed the following line
oppIds.add(taskObj.WhatId); --> See my code that i shared line number 10
Then, after querying the data, please put system.debug whther any records are returning via SOQL list. 
finally, put one system.debug inside the last for loop, that values from map is correclty getting
System.debug(oppMap.get(taskObj.WhatId));\
If the data comes correclty, sure the value gets updated.
In my personal org, it is working.

Regards,
Navin
dcgb2332dcgb2332
Hi Nevin,

I applied the fix and here's the error I receive once I perform the action of a new task: 

TaskTrigger: execution of AfterInsert caused by: System.StringException: Invalid id: 102722 External entry point

Thanks
Navin Selvaraj23Navin Selvaraj23
Hi Sabrina,
could you share your code. not exact code if it is confidential. just rename object name and column names. let me check

Regards,
Navin
dcgb2332dcgb2332
Navin, Here is the code: trigger TaskTrigger on Task (before insert, after insert) { List taskIds = new List(); List oppIds = new List(); Map oppMap = new Map(); if(Trigger.isbefore){ if(Trigger.isupdate){ for(Task taskObj : Trigger.New){ taskIds.add(taskObj.Id); //WhatId is the opportunity Id when creating task from opportunity oppIds.add(taskObj.WhatId); } List tskObjLst = [SELECT Id,WhatId FROM Task where Id in: taskIds ]; List oppLst = [SELECT Id,Opportunity_Number__c FROM Opportunity where Id in: oppIds ]; for(Opportunity oppObj : oppLst){ oppMap.put(oppObj.Id,oppObj.Opportunity_Number__c); } for(Task taskObj : tskObjLst){ taskObj.Job_Number__c = oppMap.get(taskObj.WhatId); } update tskObjLst; } } } Right now I have the following errors: Line 4 – Invalid type argument count for Map: expected 2 but found 1 Line 15 – Variable does not exist: oppMap Line 18 – Variable does not exist: oppMap Thank you, Sabrina
Navin Selvaraj23Navin Selvaraj23
Sabrina,

1. When you need to perform this operation?
2. before or after? either insert or update?
3.Did you remove the what type of list and map from the code you provoided right? EX: list should be declared like the following as well as map
        List<DataType> oppIds = new List<DataType>();
        Map<Key,Value>  oppMap = new Map<Key,Value>();
4. Invalid type argument count for Map : it is expecting key value pair. But you have given only one argument instead of two.
5.Variable does not exist: oppMap: visibility is not at the line number


 
dcgb2332dcgb2332
1) I need the operation to occur after the task is created
2) So it would need to be at insert (after I'm assuming?)
3) I don't recall removing the list.
4) I gave the argument you included in the code
5) See my above


I just copied and pasted your code and made a few amendments to the object names, nothing else. 
Navin Selvaraj23Navin Selvaraj23
Sabrina,

I can able to execute without error in my org with the follwoing code.

In system.debug i can able to get the values.
 
trigger TaskTrigger on Task (after insert) {
    List<Id> taskIds = new List<Id>();
    List<Id> oppIds = new List<Id>();
    Map<Id,Id>  oppMap = new Map<Id,Id>();
    if(Trigger.isAfter){
        if(Trigger.isInsert){
           for(Task taskObj : Trigger.New){
                taskIds.add(taskObj.Id);
            }
System.debug("taskIds: " + taskIds);
            List<Task> tskObjLst = [SELECT Id,WhatId FROM Task where Id in: taskIds ];
System.debug("tskObjLst  : " + tskObjLst );   
            List<Opportunity> oppLst = [SELECT Id,Opportunity_Number__c FROM Opportunity where Id in: oppIds ];  
System.debug("oppLst: " + oppLst);   
            for(Opportunity oppObj : oppLst){
                oppMap.put(oppObj.Id,oppObj.Opportunity_Number__c);
            }
            for(Task taskObj : tskObjLst){
                taskObj.Job_Number__c = oppMap.get(taskObj.WhatId);
            }
System.debug("tskObjLst : " + tskObjLst)
            update tskObjLst;
        }
    }
}

Please execute the above code and make sure there is no error. then create the task on the opportunity. check the values are coming into the list via system.debug. if the values are coming in list, it will work.

Regards,
Navin
dcgb2332dcgb2332
Thanks, still not working. I'm not going to waste your time anymore. I think I'll need to start from scratch. 
Thanks for your help.