You need to sign in to do that
Don't have an account?
sfdcianp
trigger on Task
i have a relation with account and contact.Account record is having the contact name as a field.
Whenever a new task is created with related to an account i need to equate the contact field on account to the new custom field in the task obj.
COuld any one help me in this scenario..
Thanks in advance
Considering the above points try this
trigger PopulateContactOnTask on Task(before insert){
List<Id> lstid = new List<Id>()[
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
lstid.add(t.WhatId);
}
}
Map<id,account> aMap = new Map<id,account>([Select Id,Contactname__c from Account where id in :lstid]);
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
t.taskcontact__c = aMap.get(t.WhatId).Contactname__c;
}
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
Contact field on account means you have a field named Contact Name in Account? Then what is the relationship? This means Contact is master ans Account is child. But this is not the real scenario. Please share it to analyze it better.
Another question you wrote "Whenever a new task is created with related to an account". Is the WhatId field in Task?
Thanks
Considering the above points try this
trigger PopulateContactOnTask on Task(before insert){
List<Id> lstid = new List<Id>()[
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
lstid.add(t.WhatId);
}
}
Map<id,account> aMap = new Map<id,account>([Select Id,Contactname__c from Account where id in :lstid]);
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
t.taskcontact__c = aMap.get(t.WhatId).Contactname__c;
}
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Ok then check the above written trigger, it is based on the points you mentioned.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Whenever i create a new task for the account record , i have a field called test(custom field) in task that needs to be updated with the contact name.
thanks in advance
As you posted above is it the same scenario? Means are you talking about the Contact Name field in Account?
Thanks
Check this. Is it like this?
trigger PopulateContactOnTask on Task(before insert){
List<Id> lstid = new List<Id>();
List<Id> parentAccId = new List<Id>();
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
lstid.add(t.WhatId);
}
}
List<Account> accList = new List<Account>();
accList = [Select Id,Contactname__c,ParentId from Account where id in :lstid];
for(Account acc : accList){
parentAccId.add(acc.ParentId);
}
Map<id,account> aMap = new Map<id,account>([Select Id,Contactname__c from Account where id in :parentAccId]);
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
t.taskcontact__c = aMap.get(t.WhatId).Contactname__c;
}
}
}
If the post helps you please throw KUDOS.
Check this now
trigger PopulateContactOnTask on Task(before insert){
List<Id> lstid = new List<Id>();
List<Id> lstTaskid = new List<Id>();
List<Id> parentAccId = new List<Id>();
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
lstid.add(t.WhatId);
}
}
for(task t : Trigger.new){
lstTaskid .add(t.id);
}
List<Account> accList = new List<Account>();
accList = [Select Id,Contactname__c,ParentId from Account where id in :lstid];
for(Account acc : accList){
parentAccId.add(acc.ParentId);
}
Map<id,account> aMap = new Map<id,account>([Select Id,(select id,name from Contacts) from Account where id in :parentAccId]);
Map<id,task> tMap = new Map<id,task>([Select Id,What.ParentId from Task where id in :lstTaskid ]);
List<Contact> conList = new List<Contact>();
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
conList = aMap.get(tMap.get(t.Id).What.ParentId).Contacts;
t.test__c = conList.get(0).name;
}
}
}
If the post helps you please throw KUDOS.
How can I set up a trigger to display all the related contacts on account in a custom field (rich text). my code as as below and not working:
trigger AccountTestTrigger on Account (before insert, before update) {
// Use a Set, as add() method will only add an item if it is not already in the set, hence no duplicates...
Set< Id > accountIds = new Set< Id >();
for (Account a : trigger.new) {
accountIds.add(a.Id);
}
//Map<Id, Account> contactAccount = new Map<ID, Account>(Trigger.new);
Contact[] people = new List<Contact>();
//people = [select Id, AccountId, Name from Contact where AccountId in : contactAccount.keySet()];
Map<Id, Account> accountmap = new Map<Id, Account>([ SELECT (SELECT Name FROM Contacts LIMIT 1) FROM Account WHERE Id in: accountIds]);
for (Account a : Trigger.new) {
Account accFromDb = accountmap.get(a.Id);
a.Contact_list__C = accFromDb.Contacts[0].Name + ',';
accountmap.put(a.id,a);
}
}