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
Jackie StuartJackie Stuart 

Trigger to pull Account Name / ID when Picklist value selected

Hi,

Newbie at Triggers / Code etc.

I am trying to set up a trigger so that when a certain value within a picklist on the Account Object is selected, it will override whatever the User types into the Account Name with a particular Account.

My trigger worked when I asked the Account name to equal 'ABC Inc'; but of course, it created a new Account, and didn't use the existing 'ABC Inc". So now I am trying by using Account ID ... but I'm not sure how to write 'Account.AccountID ='

Here is the code so far ...
 
trigger AccountTrigger on Account(before insert,before update){
     for(Account acnt : trigger.new){
if(acnt.Relationship_Type__c == 'Monash Staff'){
                acnt.ID = '0012800000jHzKH';}
if(acnt.Relationship_Type__c == 'Monash Students'){
                acnt.ID = '0012800000jHz4D';}
if(acnt.Relationship_Type__c == 'No Company Affiliation'){
                acnt.ID = '0012800000jHz4I';}
           else{ }
}               
}


 
Best Answer chosen by Jackie Stuart
James LoghryJames Loghry
Hi Jackie,

The Id field is not writeable.  One cannot simply say "hey, make this record another record".  In other words, you'll have to delete the duplicate record first, and then update the correct account.

Alternatively, why not use the account hierarchy instead to keep track of all these dupe accounts under one of the three parent accounts? If that's the case, you would associate the child account to one of the three parent accounts using the child's ParentId field.

All Answers

James LoghryJames Loghry
Hi Jackie,

The Id field is not writeable.  One cannot simply say "hey, make this record another record".  In other words, you'll have to delete the duplicate record first, and then update the correct account.

Alternatively, why not use the account hierarchy instead to keep track of all these dupe accounts under one of the three parent accounts? If that's the case, you would associate the child account to one of the three parent accounts using the child's ParentId field.
This was selected as the best answer
Jackie StuartJackie Stuart
Hi Amit and James,

Sorry it has taken me so long to get back to you. 

James, as soon as I read your answer, I had a bit of a 'of course, duh!!' moment :)

Amit, thanks for the help with the code. As I have decided that what I was attempting to do is not a great idea, I haven't tested it. Much I appreciate the help!

Thanks both :)