You need to sign in to do that
Don't have an account?
Shawn Reichner 29
Contacts from parent to child account via Apex trigger - getting Illegal Assignment error
Hello,
I am attempting some apex outside of the normal areas for me as I am new and stil learning.
I am trying to when a child account is created for an existing account, clone the Contacts and then list them under the child account as well. I know this isnt best practice, however it is what I am tasked with and the following is the code I currently have, although I am getting an error on line 29 and the error is - Illegal Assignment From Set to Id Can anyone help me to get thsi working so that I can show in Sandbox and help to show the duplication and why this would be a bad idea?
Thanks,
I am attempting some apex outside of the normal areas for me as I am new and stil learning.
I am trying to when a child account is created for an existing account, clone the Contacts and then list them under the child account as well. I know this isnt best practice, however it is what I am tasked with and the following is the code I currently have, although I am getting an error on line 29 and the error is - Illegal Assignment From Set to Id Can anyone help me to get thsi working so that I can show in Sandbox and help to show the duplication and why this would be a bad idea?
Thanks,
trigger CopyAccountContactsToChild on Account (before insert) { Set<Account> AcctsToUpdate = new Set<Account>(); List<Id> ParentAccounts = new List<Id>(); List<Contact> ConsToSwap = new List<Contact>(); For(Account A : Trigger.New){ If(A.Parent_Account_Custom__c != null){ AcctsToUpdate.add(A); } } For(Account A2 : AcctsToUpdate){ ParentAccounts.add(A2.Parent_Account_Custom__c); } For(Id pAtoLookAt : ParentAccounts){ List<Contact> consToGet = new List<Contact>([SELECT ID, AccountId FROM Contact WHERE AccountId =: pAtoLookAt]); For(Contact c3 : consToGet){ ConsToSwap.add(c3); } } For(Contact AFinal : ConsToSwap){ AFinal.AccountId = AcctsToUpdate; } }
Hope this helps!
Thank you
Prady01
Thank you
PSM
Is your code incomplete? I am just asking to understand completely. because, I could not see any DML to clone (insert) the contacts.
Please consider my suggestion - If the business requirement is to list the contacts of parent accounts as well under the child account, you can always pull the data and present it in business view (pagelayout or VF page)
The query to pull the contact of Parent Account -
Select Contact.Id, Contact.Name, Contact.AccountId, Account.parentid from Contact where accountid in (Select parentid from account where id = <var which holds childs account id>)
Prady01 has given the required code and I dont want to replicate the same here, however,
Few comments in your code to help in future:
trigger CopyAccountContactsToChild on Account (before insert) {
Set<Account> AcctsToUpdate = new Set<Account>();
List<Id> ParentAccounts = new List<Id>();
List<Contact> ConsToSwap = new List<Contact>();
For(Account A : Trigger.New){
If(A.Parent_Account_Custom__c != null){
AcctsToUpdate.add(A);
}
}
For(Account A2 : AcctsToUpdate){
ParentAccounts.add(A2.Parent_Account_Custom__c);
}
For(Id pAtoLookAt : ParentAccounts){ // You may avoid the for loops (in bold) and can put the codes in first for loop within the if condition.
List<Contact> consToGet = new List<Contact>([SELECT ID, AccountId FROM Contact WHERE AccountId =: pAtoLookAt]);
For(Contact c3 : consToGet){
ConsToSwap.add(c3);
}
}
For(Contact AFinal : ConsToSwap){
AFinal.AccountId = AcctsToUpdate; // You are trying to assign set (collection of) data where primitive (single id) is expected.
}
}
VF Page -
Extension Class -