+ Start a Discussion
Alex Waddell 12Alex Waddell 12 

Visual Force not creating Account Contact Relation

Hey everyone,

One of our developers who is no longer with the company created a VF that would create and contact under an account called "Next of Kin"

The page would then tie the Next of Kin contact back to the Account that the "Next of Kin" button was pressed

As of a few days ago the Account Contact Relationship is no longer being created.

I need help making that relationship happen again. Thank you in advance

Here is the VF Page:
 
<!--This Page is called from the [Add Next Of Kin] button from the Individual Account record. This page get the input for the Next of Kin Contact infomation and the relationship information 
with the specified individual Account-->
<apex:page standardController="Account" extensions="CreateNextofKinContact">
 <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js" />
    <apex:styleSheet value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/smoothness/jquery-ui.css" />   
    <apex:stylesheet value="{!URLFOR($Resource.stacktable, 'stacktable.css')}"/>
    <apex:stylesheet value="{!URLFOR($Resource.slds120,'assets/styles/salesforce-lightning-design-system.css')}"/>
    <apex:form >
        <apex:pageBlock mode="maindetail" title="Contact Edit">
            <apex:pageBlockButtons >
             <apex:commandButton value="Save" action="{!saveDetails}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
               
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Contact Information">
                <apex:repeat value="{!$ObjectType.Contact.FieldSets.Next_of_Kin_Contact_Creation}" 
                             var="field">
                    <apex:inputField value="{!currContact[field]}" />
                </apex:repeat>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Contact Relationship">              
              <!-- <apex:inputField value="{!currAccountContactrelation.StartDate}"/>
               <apex:inputField value="{!currAccountContactrelation.EndDate}"/>-->
                 <apex:inputField value="{!currAccountContactrelation.Roles}"/>
            </apex:pageBlockSection>            
        </apex:pageBlock>
    </apex:form>
</apex:page>


Here is the Class:
 
/* This Class act as a controller extension for the 'Create_Next_of_Kin_Contact' page. It will create a contact under the 'Next of Kin' Account and create a relationship AccountContact
relation between the  Individual account and the created contact*/
public class CreateNextofKinContact{   
    public Account currAccount;
    Public Contact currContact{get;set;}
    private Account nextOfKinAccount; 
    public AccountContactRelation currAccountContactrelation{get;set;}
    public CreateNextofKinContact(ApexPages.StandardController stdController) {
        this.currAccount = (Account) stdController.getRecord();
        currContact = new Contact();
        currAccountContactrelation = new AccountContactRelation();
        if(Label.Next_of_Kin_Account_Name != Null){
            List <Account> accountLst = [SELECT ID FROM Account WHERE name = : Label.Next_of_Kin_Account_Name];
            if(accountLst.size() > 0)
                nextOfKinAccount = accountLst[0]; 
        } 
    }
    public pagereference saveDetails(){
        if(nextOfKinAccount != Null){
            try{
                currContact.AccountId = nextOfKinAccount.id;
                currContact.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Next of Kin').getRecordTypeId();
                insert currContact;
            }Catch(Exception ex){
                System.Debug('Exception ex'+ex.getMessage());
            }
            AccountContactRelation newAccountContactRelations = new AccountContactRelation();
            newAccountContactRelations.AccountId = currAccount.id;
            newAccountContactRelations.ContactId = currContact.id;
            newAccountContactRelations.IsActive = true;
            newAccountContactRelations.Roles = currAccountContactrelation.Roles;
            newAccountContactRelations.StartDate = currAccountContactrelation.StartDate;
            newAccountContactRelations.EndDate = currAccountContactrelation.EndDate;
            try{
                insert newAccountContactRelations;                
            }Catch(Exception ex){
                System.debug('Exception Ex'+ex.getMessage());
            }
        }
        else{
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR,'We are unbale to find the Next of Kin Account. Please contact the Administrator.');
            ApexPages.addMessage(errorMessage);
            return null;
        }
        return new Pagereference('/'+currAccount.id);
    }
}


 
Akhil ReddyAkhil Reddy
Hi, 
if you are getting this message We are unbale to find the Next of Kin Account. Please contact the Administrator,
please check if there is any Label called Next_of_Kin_Account_Name in custo labels in your organization. Contact and account relationship is actullay being done from the account name stored in that label.

And please provide code for currAccountContactrelation to understand it better, I know it is used for Role, Startdate and End date data. but just need check it for better unstanding.

I just made tweeks around the code, it will atleast help you to understand the error
 
/* This Class act as a controller extension for the 'Create_Next_of_Kin_Contact' page. It will create a contact under the 'Next of Kin' Account and create a relationship AccountContact
relation between the  Individual account and the created contact*/
public class CreateNextofKinContact{   
    public Account currAccount;
    Public Contact currContact{get;set;}
	
    private Account nextOfKinAccount; 
	
	
    public AccountContactRelation currAccountContactrelation{get;set;}
	
	
    public CreateNextofKinContact(ApexPages.StandardController stdController) {
        this.currAccount = (Account) stdController.getRecord();
        currContact = new Contact();
        currAccountContactrelation = new AccountContactRelation();
        if(Label.Next_of_Kin_Account_Name != Null){
            List <Account> accountLst = [SELECT ID FROM Account WHERE name = : Label.Next_of_Kin_Account_Name];
            if(accountLst.size() > 0)
                nextOfKinAccount = accountLst[0]; 
        } 
    }
    public pagereference saveDetails(){
        if(nextOfKinAccount != Null){
            try{
                currContact.AccountId = nextOfKinAccount.id;
                currContact.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Next of Kin').getRecordTypeId();
                insert currContact;
            }Catch(Exception ex){
                System.Debug('Exception ex'+ex.getMessage());
            }
			if (currContact.id != null && currAccount.id != null){
				AccountContactRelation newAccountContactRelations = new AccountContactRelation();
				newAccountContactRelations.AccountId = currAccount.id;
				newAccountContactRelations.ContactId = currContact.id;
				newAccountContactRelations.IsActive = true;
				newAccountContactRelations.Roles = currAccountContactrelation.Roles;
				newAccountContactRelations.StartDate = currAccountContactrelation.StartDate;
				newAccountContactRelations.EndDate = currAccountContactrelation.EndDate;
				try{
					insert newAccountContactRelations;                
				}Catch(Exception ex){
					System.debug('Exception Ex'+ex.getMessage());
				}
			}
			else if (currContact.id == null && currAccount.id == null){
				ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR,'We are unbale to find create relation record because either Contact or Acc is missing');
				ApexPages.addMessage(errorMessage);
				return null;
			}
        }
        else{
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR,'We are unbale to find the Next of Kin Account. Please contact the Administrator.');
            ApexPages.addMessage(errorMessage);
            return null;
        }
        return new Pagereference('/'+currAccount.id);
    }
}


 
Alex Waddell 12Alex Waddell 12
Hey Akhil,

I appreciate your help.

I am not getting any errors on the page. Also i am unable to find the code for CurrAccountCorrelation.

do you know what type of VF this would be? Class, Page, etc?
Akhil ReddyAkhil Reddy
My bad AccountContactRelation is inbuilt object for person account. Anyways since you are not getting any errors means there should be contact and Account records are present in Database. 
/* This Class act as a controller extension for the 'Create_Next_of_Kin_Contact' page. It will create a contact under the 'Next of Kin' Account and create a relationship AccountContact
relation between the  Individual account and the created contact*/
public class CreateNextofKinContact{   
    public Account currAccount;
    Public Contact currContact{get;set;}
	
    private Account nextOfKinAccount; 
	
	
    public AccountContactRelation currAccountContactrelation{get;set;}
	
	
    public CreateNextofKinContact(ApexPages.StandardController stdController) {
        this.currAccount = (Account) stdController.getRecord();
        currContact = new Contact();
        currAccountContactrelation = new AccountContactRelation();
        if(Label.Next_of_Kin_Account_Name != Null){
            List <Account> accountLst = [SELECT ID FROM Account WHERE name = : Label.Next_of_Kin_Account_Name];
            if(accountLst.size() > 0)
                nextOfKinAccount = accountLst[0]; 
        } 
    }
    public pagereference saveDetails(){
        if(nextOfKinAccount != Null){
            try{
                currContact.AccountId = nextOfKinAccount.id;
                currContact.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Next of Kin').getRecordTypeId();
                insert currContact;
            }Catch(Exception ex){
                System.Debug('Exception ex'+ex.getMessage());
            }
			if (currContact.id != null && currAccount.id != null){
				AccountContactRelation newAccountContactRelations = new AccountContactRelation();
				newAccountContactRelations.AccountId = currAccount.id;
				newAccountContactRelations.ContactId = currContact.id;
				newAccountContactRelations.IsActive = true;
				newAccountContactRelations.Roles = currAccountContactrelation.Roles;
				newAccountContactRelations.StartDate = currAccountContactrelation.StartDate;
				newAccountContactRelations.EndDate = currAccountContactrelation.EndDate;
				try{
					insert newAccountContactRelations;    
                    system.debug('====>>>>'+ newAccountContactRelations.id )
				}Catch(Exception ex){
					System.debug('Exception Ex'+ex.getMessage());
				}
			}
			else if (currContact.id == null && currAccount.id == null){
				ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR,'We are unbale to find create relation record because either Contact or Acc is missing');
				ApexPages.addMessage(errorMessage);
				return null;
			}
        }
        else{
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR,'We are unbale to find the Next of Kin Account. Please contact the Administrator.');
            ApexPages.addMessage(errorMessage);
            return null;
        }
        return new Pagereference('/'+currAccount.id);
    }
}
Please open developer console before creating record and open debug log once the record being created and see if you can find anything like ===>>> id in log. This above code should be giving the id of new relatioship record info. 
Alex Waddell 12Alex Waddell 12
Akhil, Unfortunately i do not know how to properly use the Debug function in the developer console Which do i select? Open Execute Anonymous Window CTRL + E Execute Last CRTL + ALT + E Perspective Manager Thank you for all your help. I am only just beginning my training for Developing so this is all a little over my head Alex Waddell IT Support - SalesForce Alex.W@Beechcare.com
Akhil ReddyAkhil Reddy
User-added image
once you made cahnges in ui (say update or insert ) open the log by double clicking the row and select the Debug check box which open debug log
 
Alex Waddell 12Alex Waddell 12
Akhil,

I believe i found the error, it was on the row titled /apex/Create_next_of_Kin_Contact


08:45:23:134 USER_DEBUG [37]|DEBUG|Exception ExInsert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Can't choose a person as the account in this relationship.: []