• Enea Gjoka
  • NEWBIE
  • 5 Points
  • Member since 2017
  • Salesforce Consultant
  • Salesfive Consulting GmbH


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 2
    Questions
  • 0
    Replies

Hello,

I have a community portal where we want to let our customer Self-register. The controller which does this doesn't check if a contact with the same email exists in salesforce before creating the contact/user record. So, we new duplicate contacts area created.

/**
 * An apex page controller that supports self registration of users in communities that allow self registration
 */
public class CommunitiesSelfRegController {

    public String firstName {get; set;}
    public String lastName {get; set;}
    public String email {get; set;}
    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
    
    public CommunitiesSelfRegController() {
        String expid = ApexPages.currentPage().getParameters().get('expid');            
        if (expId != null) {
            Site.setExperienceId(expId); 
        }    
    }
    
    private boolean isValidPassword() {
        return password == confirmPassword;
    }

    public PageReference registerUser() {
    
           // it's okay if password is null - we'll send the user a random password in that case
        if (!isValidPassword()) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
            ApexPages.addMessage(msg);
            return null;
        }    

        String profileId = null; // To be filled in by customer.
        String roleEnum = null; // To be filled in by customer.
        String accountId = ''; // To be filled in by customer.
        
        String userName = email;

        User u = new User();
        u.Username = userName;
        u.Email = email;
        u.FirstName = firstName;
        u.LastName = lastName;
        u.CommunityNickname = communityNickname;
    u.ProfileId = profileId;
    
        String userId;

        try {
            userId = Site.createExternalUser(u, accountId, password);
        } catch(Site.ExternalUserCreateException ex) {
            List<String> errors = ex.getDisplayMessages();
            for (String error : errors)  {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error));
            }
            
            // This message is used for debugging. Do not display this in the UI to the end user.
            // It has the information around why the user creation failed.
            System.debug(ex.getMessage());
        }
        
        if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));
            }
            else {
                PageReference page = System.Page.CommunitiesSelfRegConfirm;
                page.setRedirect(true);
                return page;
            }
        }
        return null;
    }
}

How can I modify this apex class to make sure that first it checks if the email given on the registration page exists and if exists use that contact to create the community user. If not, create a new contact (what it does now).

Thank you very much in advance for your help.

 

 

Hi all,
I want to show in child accounts, the related contacts of the parents accounts too. I almost solved this with a Visualforce Page and a Controller, but the related contacts are shown only on the parent account and the current account contacts. 
How do I show the related contacts of the other parent child accounts contacts on the child account too?

Example: I have parent account A, and child Account B and C. When I click on the Account C, i want the visual force page to show me the related contacts of the Account A + the related contacts of account B. 

Thanks in advance for the help.

Here is my Controller:

public with sharing class AccountHierarchyController {
    public Account acc { get; private set; }
    public List<Account> accountList { get; private set; }
    public ApexPages.StandardController controller { get; private set; }
    
    public AccountHierarchyController(ApexPages.StandardController controller) {
        this.acc = (Account)controller.getRecord();
        this.accountList = new List<Account>();
        
        Account baseAccount = [SELECT Id, 
                               ParentId, 
                               Name,
                               Parent.Name,
                               (SELECT Id, Name, Phone, Email, Title from Contacts),
                               (SELECT Id, Name FROM ChildAccounts)
                               FROM Account 
                               WHERE Id = :acc.id];
        accountList.add(baseAccount);
        
        Account currentAccount = baseAccount;
 
        // Traverse the hierarchy upwards.
        while (currentAccount.ParentId != null) {
            currentAccount = [Select id, Name, ParentId, Parent.Name, (Select Id, Name, Phone, Email, Title from Contacts) from Account where Id = :currentAccount.ParentId];
            accountList.add(currentAccount);
        }
        
        // Traverse the hierarchy downwards
        Set<Id> accountsToQuery = new Map<Id, Account>(baseAccount.ChildAccounts).keySet();
        while (accountsToQuery.size() > 0) {
            List<Account> thisLevelAccounts = [SELECT Id, Name, Parent.Name,
                                               (Select Id, Name, Phone, Email, Title  from Contacts),
                                               (SELECT Id, Name FROM ChildAccounts)
                                               FROM Account
                                               WHERE Id IN :accountsToQuery];
            accountsToQuery = new Set<Id>();
            
            for (Account a : thisLevelAccounts) {
                // Add this Account (with its Contacts) to the list.
                accountList.add(a);
                
                // Add this Account's children to the query for the next level.
                for (Account child : a.ChildAccounts) {
                    accountsToQuery.add(child.Id);
                }
            }
        }
    }
}


Here is the Visual force page:

<apex:page title="Contact" standardController="Account" extensions="AccountHierarchyController" lightningStylesheets="true">
    <apex:outputPanel id="cont">
        <apex:pageBlock title="Contacts">
            <apex:repeat value="{! accountList }" var="a">
                <apex:pageBlockSection title="{! a.Name + IF(NOT(ISBLANK(a.ParentId)), ' (child of ' + a.Parent.Name + ')', '') }">
                    <apex:pageBlockTable value="{! a.Contacts }" var="con" id="conlist" title="Contact">
                        <apex:column value="{!con.Name}"/>
                        <apex:column value="{!con.Phone}" />
                        <apex:column value="{!con.Email}" />
                        <apex:column value="{!con.Title}" />
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:repeat>
        </apex:pageBlock>
    </apex:outputPanel>
</apex:page>
Hello,
I tried many times... but receive the same error message : "Challenge Not yet complete... here's what's wrong: 
Formula in custom field 'Account Annual Revenue' is not correct. Check the instructions."
I followed all instructions & get the right results. It's a basic & very simple formula.
Do someone else encounter the same issue ? Any solution ?