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
sfdcianpsfdcianp 

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

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

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

souvik9086souvik9086

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

souvik9086souvik9086

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

This was selected as the best answer
sfdcianpsfdcianp
I have a field called contactname in account that will be generated by some trigger.what ever the vaue in the field should be assigned to a field called taskcontact(customfield) in task. its whatid field only
souvik9086souvik9086

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

sfdcianpsfdcianp
Thanks got the solution !!!
sfdcianpsfdcianp
I have an account and i have taken the parent account look up and filled another account. the parent account is associated with the contact.(there is a lookup for account on contact)

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
souvik9086souvik9086

As you posted above is it the same scenario? Means are you talking about the Contact Name field in Account?

 

Thanks

sfdcianpsfdcianp
NO thats different
souvik9086souvik9086

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.

souvik9086souvik9086

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.

Maf_007Maf_007

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);
}
}