You need to sign in to do that
Don't have an account?
Newbie Help: Use record owner name to create new record in another object
Hi All,
This is my first attempt at writing an apex trigger and have zero background in coding. The purpose of the code is to create a new account when a new contact is created and is not assigned to an account. Here's what I pieced together:
trigger CreateIndividualAccount on Contact (before insert, before update) {
RecordType arec = [SELECT ID from RecordType WHERE SobjectType='Account' AND Name='Individual' limit 1];
for (Contact c : trigger.new)
{
if(c.AccountID == null)
{
Account[] acc = new Account[0];
string cName = (Owner.Name);
acc.add(new Account (Name = cName, RecordTypeId = arec.Id));
insert acc;
c.AccountID = acc[0].Id;
}
}
}
I get the following error when I attempt to save: Error: Compile Error: Variable does not exist: Owner.Name at line 8 column 29
Please let me know what I am doing wrong, I'm sure it is something really simple.
Cheers
Sorry, did not account for future contacts in same session or new logins...
This should cover it:
All Answers
To solve the immediate problem:
Should link all new contacts for the owner to the account created for that owner
***Edited, See below*****
Hi Starz,
Thanks for helping me out. I just updated my trigger with the code that you had just provided to me. When I create a new contact, the trigger is not creating a new account or creating a relationship between the contact and account.
I'm a complete noob at this and am having a hard time deciphering your code and figuring out what may be wrong.
Any additioanal guidence would be much appreciated.
Sorry about that...
I forgot to update the correct variables after editing the code...
This is tested and works:
This will work for bulk inserts as well
It worked after I removed "Abbott_Account_ID__c = '509834586'" from the code.
You sir are a gentleman & a scholar. I owe you a beer if we ever cross paths in IRL.
Hey Starz,
Sorry to keep bothering you. But I've been trying to trouble shoot an issue. When the user logs out and logs back in, the trigger no longer adds the contacts to the same account, it will aad a new account for every new contact using the contact owner's name.
Sorry, did not account for future contacts in same session or new logins...
This should cover it:
You are a Rock Star!
I need to move this from our sandbox to our production and need some help.
I know I need to create a test class to test the code, which I have done:
I'm a bit lost from there, when I package the two and try to deploy them to production, I get this error:
Failure Message: "System.AssertException: Assertion Failed: An Error occurred inserting the test record:", Failure Stack Trace: "Class.TestCreateIndividualAccount.validateIndividualAccount: line 11, column 1"
I'm guess the account names aren't matching but I may be wrong. Any advice would be helpful.
Anyone?
can you post the debug log?
Your issue is with the managed package rethink....
Not sure what it is doing but the query it is trying to do returns no rows and the way the wrote the query does not handle that sceniero....
Thanks, I just saw that as well. So there is no way I can push this to production with out uninstalling the managed package?
You will have to find out exactly what that managed package needs in order to run with that test record.
The issue will be, even if you uninstall it, you will not be able to use the trigger AND that package after you push it to production......
Once you find out what the packages needs / does, and code for that you will be fine