0

My requirement is:

I have standard object Account and two fields Email and Alternate Email.

If the two fields are same, an error message will be displayed and the account won't be inserted.

If the two fields are not same and either one is not null, then account will be inserted.

If the account name matches any existing account name, then that account will be updated.

In my code, the first time, when the two fields are not same, account is inserted. But the second time, when the two fields are not the same, I'm getting error: Account ID: cannot specify Id in an insert call which means the account is already inserted once.

If I give upsert, then the same account gets updated again and again. But that is not what I want. The error message displays fine. It is the inserting and updating that causes problem. Please help:

Apex class:

public with sharing class ExtensionAccount {
private final Account acct;
    public ExtensionAccount(ApexPages.StandardController controller) {
    this.acct = (Account)controller.getRecord();
}
public PageReference saveandErrorMessage() {
if (acct.Email__c == acct.Alternate_Email__c && (acct.Email__c!=null || acct.Alternate_Email__c!=null)) 
{    
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'ERROR:Email and Alternate Email are same.');
ApexPages.addMessage(myMsg); 
}
**try {
insert acct;
}**
 catch (DmlException e) {
}
return null;
}
}

Visualforce page:

<apex:page standardController="Account" extensions="ExtensionAccount">
<apex:pageMessages />
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:inputfield value="{!account.name}"/>
   <apex:pageBlockSectionItem >
         <apex:outputlabel value="Email"/>

         <apex:inputfield value="{!account.Email__c}"/>
  </apex:pageBlockSectionItem>
  <apex:pageBlockSectionItem >
  <apex:outputlabel value="Alternate Email"/>
  <apex:inputfield value="{!account.Alternate_Email__c}"/>
  </apex:pageBlockSectionItem>

</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton value="Save" action="{!saveandErrorMessage}"/>
</apex:form>   
</apex:page>

Please provide your suggestions.

1 Answer 1

2

Adding a message to the page doesn't stop the insert from happening. You need to abort early:

if (acct.Email__c == acct.Alternate_Email__c && (acct.Email__c!=null || acct.Alternate_Email__c!=null)) 
{
    ApexPages.addMessage(
        new ApexPages.Message(
            ApexPages.Severity.ERROR,
            'ERROR:Email and Alternate Email are same.')); 
    return null; // don't try to insert
}

Secondly, if you want to reload the page so the user can insert a new record each time, you need to reset the variable somehow. Personally, I would redirect back to the page again:

try {
    // Do your DML here
} catch(DmlException e) {
    ApexPage.addMessages(e);
    return null;
}
return Page.myVisualforcepage.reload.setRedirect(true);

You'll want to query the accounts table first to find a match, and assign the Id value of a matching account:

Account[] accounts = [SELECT Id FROM Account WHERE Name = :acct.Name LIMIT 1]; 
if(accounts.size() == 1) { 
    acct.Id = accounts[0].Id; 
}
try { 
    upsert acct; 
} catch(DmlException e) {
    ApexPage.addMessages(e);
    return null;
}
2
  • Thanks for your help. With your suggestion, the insert logic is working fine. But when I give the same Account name again, it creates another account of the same name(duplicate account).I can't figure out the update logic. The upsert is giving an error: Error: Invalid field for upsert, must be an External Id custom or standard indexed field: Account.Name. I changed the class logic like this: insert acct; PageReference editList = Page.customTextPage; return editList.setRedirect(true); } If possible, please help regarding the update/upsert?
    – SFDC_CRM
    Jan 22, 2017 at 7:30
  • 1
    Hmm. I would have thought I'd used that logic before somewhere, but I must be mistaken. In that case, you could probably just use Account[] accounts = [SELECT Id FROM Account WHERE Name = :acct.Name LIMIT 1]; if(accounts.size() == 1) { acct.Id = accounts[0].Id; } try { upsert acct; } ...
    – sfdcfox
    Jan 22, 2017 at 7:55

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.