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

Lead Apex Trigger last activity subject
This is the first time I have used an Apex trigger and I am struggling to get it to work. I took the code used from here https://developer.salesforce.com/forums?id=906F00000008yXUIAY. And then adjusted it so it fit for leads and the names of fields I currently have in my for my leads. I am not getting any errors now, but the code does not seem to be doing anything and is active. The items I changed were changing any opportunity or opp to lead, whatid to id to get rid of errors, and the name of fields. What i want to have happen is the subject of activities to become its own field, and only display the last activity subject. The Last_Activity_Date_c field is a field I created that uses a simple formula that pulls the last activity date through. If anyone has any insight it would be greatly appreciated. Here is my code for reference:
trigger NextTastInfo on Lead (after insert, after update) {
if(Trigger.new.size() == 1 ) {
Lead tk = Trigger.New[0];
String str = tk.id;
if(str != null && str.substring(0,3)== '006')
{
Lead lead = [select OwnerId,Last_Activity_Subject__c,Last_Activity_Date__c from Lead where Id = :tk.id ];
List<Task> tskMin = [Select ActivityDate,Subject From Task where id=:tk.id and what.type = 'Lead' and isClosed = false order By ActivityDate limit 1];
if (tskMin.size()>0) {
lead.Next_Step_Date__c=tskMin[0].ActivityDate;
lead.Last_Activity_Subject__c=tskMin[0].Subject;
}
else {
lead.Next_Step_Date__c=null;
lead.Last_Activity_Subject__c='';
}
update lead;
}
}
}
trigger NextTastInfo on Lead (after insert, after update) {
if(Trigger.new.size() == 1 ) {
Lead tk = Trigger.New[0];
String str = tk.id;
if(str != null && str.substring(0,3)== '006')
{
Lead lead = [select OwnerId,Last_Activity_Subject__c,Last_Activity_Date__c from Lead where Id = :tk.id ];
List<Task> tskMin = [Select ActivityDate,Subject From Task where id=:tk.id and what.type = 'Lead' and isClosed = false order By ActivityDate limit 1];
if (tskMin.size()>0) {
lead.Next_Step_Date__c=tskMin[0].ActivityDate;
lead.Last_Activity_Subject__c=tskMin[0].Subject;
}
else {
lead.Next_Step_Date__c=null;
lead.Last_Activity_Subject__c='';
}
update lead;
}
}
}
You don't need a Trigger on Lead rather the Trigger should be on Task whenever a new Task is inserted ,the Trigger would copy the Subject of the Task and update the Last_Activity_Subject_C of related Lead.So,you should be looking at something like below :- If this helps,please mark it as best answer to help others :)
All Answers
Please go through below code.
thanks,
Ramesh
thanks,
Ramesh
You don't need a Trigger on Lead rather the Trigger should be on Task whenever a new Task is inserted ,the Trigger would copy the Subject of the Task and update the Last_Activity_Subject_C of related Lead.So,you should be looking at something like below :- If this helps,please mark it as best answer to help others :)
trigger updateRelatedOpportunity on Task (after insert,after update) {
List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();
for(Task t :trigger.new)
{
if(t.whoId!=null)
{
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Opportunity.Schema.SObjectType)
{
OpportunityIds.add(t.WhoId);
}
}
}
{
//Querying the related Opportunity based on whoId on Task
Map<Id,Opportunity> OpportunityMap = new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
for(Task t :Trigger.new)
for(Opportunity l : OpportunityMap.Values())
{
l.Last_Activity_Subject__C = t.subject;
OpportunityList.add(l);
}
}
// updating the Opportunity
if(OpportunityList.size()>0)
{
update OpportunityList;
}
}
whoId needs to be replaced with whatId .
updateRelatedLead Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
updateRelatedOpportunity Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Deploy Error Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AYGqIAO#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=906F0000000AYeUIAW
I am trying to write similar trigger can you please help me in coding this trigger.
In the Opportunity Object I have created a field called Next Activity. This Field should display the Subject of the next scheduled activity... I am literally confused. I have to Write trigger on Opportunity or On Task and Event? If I have to write on Opportunity can you please help me in writing the code. Thanks In advance!