1

This class is used on a button to create Accounts and contacts. For some reason today when i try to create an account by using the button behind the class, i was neither able to create account nor contact.

What's going wrong here?

public class ParentAccountController{

    public String accId;
    public String accountId{get;set;}

    public Account account{get;set;}
    public Contact contact{get;set;}

    public boolean isAccountExists;
    public boolean isContactExists;
    public boolean accountFlag{get;set;}
    public boolean contactFlag{get;set;}

    public ParentAccountController(){

    }

    public ParentAccountController(ApexPages.StandardController controller){        
        accountFlag = true;
        contactFlag = false;
        isAccountExists = false;
        isContactExists = false;
        account = new Account();
        contact = new Contact();
    }

    public void accountNext(){
        if(accountFlag){ 
           if(account.Name != null){
              List<Account> accList = new List<Account>();
              accList = [SELECT Id,
                                Name  
                         FROM Account
                         WHERE Name =: account.Name];
              if(!accList.isEmpty()){
                 isAccountExists = true;
                 account.Id = accList.get(0).Id;
                 accId = accList.get(0).Id;
                 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Account ' + account.Name + ' has already been created. Do you wish to continue with the existing Account?. If yes then please enter the contact details.');
                 ApexPages.addMessage(myMsg);
              }
           }   
           accountFlag = false; 
           contactFlag = true;    
        }
    }

    public void contactNext(){
        if(contactFlag){
           if(contact.Email != null && isAccountExists){
              List<Contact> contList = new List<Contact>();
              contList = [SELECT Id,
                                 Email,
                                 LastName 
                          FROM Contact
                          WHERE Email =: contact.Email
                          AND LastName =: contact.LastName 
                          AND AccountId =: accId];
              if(!contList.isEmpty()){
                 isContactExists = true;
                 contact.Id = contList.get(0).Id;
                 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact has already been created. Do you wish to continue with the existing Contact?. If yes then please enter the Asset Tag details.');
                 ApexPages.addMessage(myMsg);
              }
           }    
           contactFlag = false;
        }
    }

    public void contactPrevious(){
        accountFlag = true;
        contactFlag = false;
    }

    public void save(){    
        String contactId, projId;
        List<Account> accList = new List<Account>();
        Savepoint sp = Database.setSavepoint();
        try{
            if(isAccountExists){
               accList = [SELECT Id,
                                 Name,
                                 Account_Email__c    
                          FROM Account
                          WHERE Name LIKE: '%' + account.Name];
               if(!accList.isEmpty()){
                  //accList.get(0).Name = account.Name;
                  accList.get(0).Account_Email__c = account.Account_Email__c;
                  //accList.get(0).Site = account.Site;
                  //accList.get(0).Customer_ID__c = account.Customer_ID__c;  
                  update accList.get(0);
                  accountId = accList.get(0).Id;
               }  
            }else{
              accList = [SELECT Id,
                                Customer_ID__c
                         FROM Account 
                         WHERE ParentId = null
                         ORDER BY Customer_ID__c DESC];
              if(!accList.isEmpty()){
                 String customerId = accList.get(0).Customer_ID__c;
                 Integer v = Integer.valueOf(customerId) + 1;
                 account.Project_Id__c = String.valueOf(v);            
              }else{
                 account.Project_Id__c = '1';
              }   
              insert account;
              accountId = account.Id;
            }

            //if(isContactExists){
                  contact.AccountId = account.Id;
                  insert contact;
                  contactId = contact.Id;
            //}

            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!');
            ApexPages.addMessage(myMsg);

            return;
        }catch(Exception e){
            Database.rollback(sp);
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Duplicate Record."Please enter a Unique Asset ID"');
            ApexPages.addMessage(myMsg);
            return;
        }
    }

    public PageReference cancel(){
        PageReference ref = new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
        return ref;
    }
}

VF Page:

<apex:page standardController="Account" 
extensions="ParentAccountController">   
   <apex:form >
       <apex:pagemessages id="msgPanel"/>
       <apex:outputPanel id="accountPanel"> 
           <apex:pageBlock rendered="{!accountFlag}">
               <apex:pageBlockSection title="Account Detail">                
                   <apex:inputField value="{!account.Name}"/>
                   <apex:inputField value="{!account.Account_Email__c}"/>
               </apex:pageBlockSection>
               <apex:pageBlockButtons location="bottom">
                   <apex:commandButton action="{!accountNext}" value="Next" rerender="msgPanel, accountPanel, contactPanel"/>
               <apex:CommandButton action="{!cancel}" value="Cancel" immediate="true"/>
           </apex:pageBlockButtons>
       </apex:pageBlock>    
   </apex:outputPanel>
   <apex:outputPanel id="contactPanel"> 
       <apex:pageBlock rendered="{!contactFlag}">    
           <apex:pageBlockSection title="Contact Detail">
               <apex:inputField value="{!contact.FirstName}"/>
               <apex:inputField value="{!contact.LastName}"/>         
               <apex:inputField value="{!contact.Email}" required="true"/>
               <apex:inputField value="{!contact.Phone}" /> 
           </apex:pageBlockSection>  
           <apex:pageBlockButtons location="bottom">
               <apex:commandButton action="{!contactPrevious}" value="Back" rerender="msgPanel, accountPanel, contactPanel" immediate="true"/>
               <apex:commandButton action="{!save}" value="Save" rerender="msgPanel, contactPanel, populateAccountIdPanel" oncomplete="navigateToAccount();"/>
               <apex:CommandButton action="{!cancel}" value="Cancel" immediate="true"/>
           </apex:pageBlockButtons>
       </apex:pageBlock>                 
   </apex:outputPanel>
   <apex:outputPanel id="populateAccountIdPanel">
       <script>
           var accountId = '{!accountId}';
       </script>
   </apex:outputPanel>
   <script>
       function navigateToAccount(){
           window.opener.location.href = '/'+accountId;
           window.close();
       }
   </script>

4
  • 2
    Can you post your visualforce and the steps you are doing to create accounts, then question will be more clear Apr 7, 2017 at 18:21
  • 1
    leave a blank line and on the next line indent four spaces. All your code must start indented four spaces Apr 7, 2017 at 18:44
  • Markdown help documentation. Apr 7, 2017 at 18:47
  • @SantanuBoral: I have a button on Account detail page and this VF page is called from that button. This VF page is used to create Account and Contact quickly. But now After clicking save, the Both accounts and Contacts records are not created. Instead, after clicking save the user is redirected to home page tab. Apr 7, 2017 at 18:54

2 Answers 2

1

Problem is in below code. Sometimes the account getting queried may have customer_id__c value as null or blank. Integer.valueOf(null) or Integer.valueOf('') will going to through type exception at next line.


else{
              accList = [SELECT Id,
                                Customer_ID__c
                         FROM Account 
                         WHERE ParentId = null
                         ORDER BY Customer_ID__c DESC];
              if(!accList.isEmpty()){
                 String customerId = accList.get(0).Customer_ID__c;
                 Integer v = Integer.valueOf(customerId) + 1;
                 account.Project_Id__c = String.valueOf(v);            
              }else{
                 account.Project_Id__c = '1';
              }   
              insert account;
              accountId = account.Id;
            }

You can make changes as below -


else{
    accList = [SELECT Id,
                        Customer_ID__c
                        FROM Account 
                        WHERE ParentId = null
                        ORDER BY Customer_ID__c DESC];

    if(!accList.isEmpty()){
        String customerId = '0';
        /*put blank check as well if customer_id__c is text field
        if(accList.get(0).Customer_ID__c != null && accList.get(0).Customer_ID__c != '')
        */
        if(accList.get(0).Customer_ID__c != null){
            customerId = accList.get(0).Customer_ID__c;
        }
        Integer v = Integer.valueOf(customerId) + 1;
        account.Project_Id__c = String.valueOf(v);            
    }else{
        account.Project_Id__c = '1';
    }   
    insert account;
    accountId = account.Id;
}
0
-2

Have you tried in your controller (or accidental deleted) your command button "save" does not correspondent to a Pagereference method

meaning public void save(); will not do the trick it should be public pagereference save(){ returning the redirect to the account or null

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .