0

very new admin on Salesforce here so please forgive what may be a simple question.

I have a need for the 'Account Name' lookup field on the Opportunity object (a standard and required field) to be auto-populated with an account I have created 'Dummy Account'. The reason for this is that we want to adopt multi-partners in opportunities via a custom object and related list but need to satisfy the required field criteria on the Opportunity object.

I've attempted to create a trigger but don't have enough understanding of what I am trying to achieve.

Please could anyone suggest a trigger which may work?

Current Trigger Code:

trigger OpportunityDummyAccountName on Opportunity (before insert, before update) { 

Account defaultAcc = [ SELECT Id FROM Account WHERE Name='""' ]; 
for (Opportunity Account : trigger.New) { }

Thanks in advance

  • Can you post the trigger also. – Reshma Feb 28 '17 at 11:47
  • Just re-confiming, you want to have AccountName on Opportunity record? If yes, when or what are the conditions on which do you wish the account name to be updated? – Mahmood Feb 28 '17 at 12:01
  • Hi guys, here is the trigger syntax I'd tried to use based on research from elsewhere – Rob Sadler Feb 28 '17 at 12:17
  • 2 3 4 5 6 7 8 9 10 11 12 trigger OpportunityDummyAccountName on Opportunity (before insert, before update) { Account defaultAcc = [ SELECT Id FROM Account WHERE Name='""' ]; for (Opportunity Account : trigger.New) { } – Rob Sadler Feb 28 '17 at 12:17
  • My dummy account was simply named as two sets of quotation marks – Rob Sadler Feb 28 '17 at 12:18
0

If you are using the standard UI to create the opportunity then neither a trigger or a WFR would help you as the Account is required on the page layout.

What you need to do there is override the Opportunity New button and use URL Hacking to place the appropriate AccountId in the url. Or create a visual force page to override the new button (overkill)

If you are creating the opportunities via code, PB, or similar non UI then you can simply use a Workflow Rule to populate the value of the Account ID field since it is a known static value.

Using a WFR is easier to maintain and remove when you ultimately decide this was a bad idea to use a dummy account.

It's also a good lesson to learn when to not use code

  • If @Rob Sadler was working in a DE org or sandbox and then porting across to production, how would you manage that here? Would you just update the static Id for the new record? I'm not familiar with change sets, and working in production. – GrumpyTechDude Feb 28 '17 at 13:00
  • 1
    Yes you would have to update it with the correct id. A trivial task compared with code. Also eaiser to remove when it is ultimately decided the account thing was a bad idea lol – Eric Feb 28 '17 at 13:02
  • 1
    @GrumpyTechDude a custom setting could also be used to store the default account id – Eric Feb 28 '17 at 13:07
  • Eric thanks for the tips and as I mentioned in response to @GrumpyTechDude and his help, my users will be using the standard UI so it sounds like I need to override the 'New' button and use URL hacking to place the required AccountID...an altogether separate question! – Rob Sadler Feb 28 '17 at 13:18
-1

If you're only calling from apex, and not via the UI (so 'required field' validations don't occur until AFTER the before triggers kick in):

I have a need for the 'Account Name' lookup field on the Opportunity object (a standard and required field) to be auto-populated with an account I have created 'Dummy Account'.

So you have an Account record where Name is '""', that is 'quotationmark quotationmark'.

First point, your trigger should be "before insert" only. Before update will change the account name on updates to the opportunity too.

In your for loop, you need to set the name to be the name you want.

trigger OpportunityDummyAccountName on Opportunity (before insert) { 

Account defaultAcc = [ SELECT Id FROM Account WHERE Name='""' ]; 
for (Opportunity opp : trigger.New) {
    opp.AccountId = defaultAcc.Id;
}

You don't need to call update or anything in the trigger, because you're in the before, so salesforce will change it here. I'd also be careful in case the user wants to set a different account, as this will always override it. So perhaps add in if (opp.AccountId == null) to your loop.

  • Thanks GTD, I've not worked out how to post an image to the comment so will mention that I've tried this but am getting "Compile Error: expecting a semi-colon, found '}' at line 6 column 0". Please can you recommend where I need syntax changes? – Rob Sadler Feb 28 '17 at 12:54
  • You're entirely right - I missed a semi-colon in my comment. Whoops! I've updated it now, it was oppAccountId = defaultAcc.Id (insert ; here) – GrumpyTechDude Feb 28 '17 at 12:57
  • Cheers GTD, and I added another close bracket at line 7 and it SF allowed me to save. I think I see where Eric is coming from, as users will be clicking on 'New' button at the first page having navigated to the Opportunity object. As the trigger is not populating Account Name until the save stage, the SF required field rule kicks in before the trigger has a chance to function. – Rob Sadler Feb 28 '17 at 13:07
  • It's true, I was assuming you were calling this from code, and not through the UI. I've updated my answer accordingly – GrumpyTechDude Feb 28 '17 at 13:18
  • Thanks and apologies for not clarifying that point – Rob Sadler Feb 28 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.