+ Start a Discussion
Swaroopa Akula 1Swaroopa Akula 1 

Compile Error: Unexpected token 'public'. at line 91 column 5

Below is the code:


public without sharing class AryaPortalUserCreationTriggerHandler{
    private static Map<Id, String> emailMap = null;
    private static List<Contact> conListToInsert = null;

    public static void beforeUpdate(Self_Register_User_Request__c[] srs) {
    
    String DUMMY_ACCOUNT  = 'Dummy Account' ;
    List<Contact> conListToInsert = new List<Contact>();
    
    Map<Id, String> emailMap = new Map<Id, String>();
    Map<Id, Self_Register_User_Request__c> dummyMap = new Map<Id, Self_Register_User_Request__c>();
    
    
    for(Self_Register_User_Request__c sr :(List<Self_Register_User_Request__c>) trigger.new)  { 
        System.debug('here is FN: '+sr.FirstName__c);
        System.debug('here is FN: '+sr.LastName__c);
        System.debug('here is FN: '+sr.Email__c);
        System.debug('here is FN: '+sr.AccountName__c);
        if(sr.FirstName__c == null || sr.LastName__c == null || sr.Email__c == null)  
            continue;      
           if(sr.Is_it_Approved__c  = TRUE){
            if(sr.AccountName__c != null ){
                Contact conRecord = new Contact(
                    FirstName = sr.FirstName__c,
                    LastName = sr.LastName__c,
                    Email = sr.Email__c,
                    AccountId = sr.AccountName__c
                );              
                System.debug('adding contact from line 36');
                conListToInsert.add(conRecord); 
            }
           
            if(sr.Email__c != null){
                emailMap.put(sr.Id, sr.Email__c);           
            }
            
            dummyMap.put(sr.Id, sr);                    
        }
    }
    //create Portal User
    if(conListToInsert != null && conListToInsert.size() > 0) {
        insert conListToInsert;
        List<Profile> pList = [select id from Profile where Name = 'Partner Community Login User - Custom'];
        if(!pList.isEmpty()){
            Profile p = pList[0];
            List<User> userListToInsert = new List<User>();
            for(Contact con :conListToInsert) {             
                String alias1 = '';
                if(con.FirstName.length() >= 0)
                    alias1 = con.FirstName.substring(0,1);
                    
                String alias2 = '';
                if(con.LastName.length() >= 4){
                    alias2 = con.LastName.substring(0, 4);        
                }else{
                    alias2 = con.LastName;
                }
                String alias = alias1 + alias2;
                User u = new User(
                    Alias = alias, 
                    Email = con.Email,
                    EmailEncodingKey = 'UTF-8', 
                    FirstName = con.FirstName,
                    LastName = con.LastName, 
                    LanguageLocaleKey = 'en_US', 
                    LocalesIdKey = 'en_US', 
                    ProfileId = p.Id, 
                    ContactId = con.id,
                    TimeZonesIdKey = 'America/Los_Angeles', 
                    UserName = con.Email
                );
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.EmailHeader.triggerUserEmail = true;
                u.setOptions(dmo);
                userListToInsert.add(u);
            }
                
            Database.SaveResult[] results = database.insert(userListToInsert, false);
            for(Database.SaveResult sr :results){
               if(!sr.isSuccess()){
                  Database.Error err = sr.getErrors()[0];
                  throw new CustomException(err.getMessage());
                 /* break;*/
               }
            }                       
        }        
    }  
    
   
    //define your custom exception
    public class CustomException extends Exception{}
 
 }   
}
Best Answer chosen by Swaroopa Akula 1
Steven NsubugaSteven Nsubuga
You defined the class inside a method.
Try this
public without sharing class AryaPortalUserCreationTriggerHandler{
    private static Map<Id, String> emailMap = null;
    private static List<Contact> conListToInsert = null;

    public static void beforeUpdate(Self_Register_User_Request__c[] srs) {
    
    String DUMMY_ACCOUNT  = 'Dummy Account' ;
    List<Contact> conListToInsert = new List<Contact>();
    
    Map<Id, String> emailMap = new Map<Id, String>();
    Map<Id, Self_Register_User_Request__c> dummyMap = new Map<Id, Self_Register_User_Request__c>();
    
    
    for(Self_Register_User_Request__c sr :(List<Self_Register_User_Request__c>) trigger.new)  { 
        System.debug('here is FN: '+sr.FirstName__c);
        System.debug('here is FN: '+sr.LastName__c);
        System.debug('here is FN: '+sr.Email__c);
        System.debug('here is FN: '+sr.AccountName__c);
        if(sr.FirstName__c == null || sr.LastName__c == null || sr.Email__c == null)  
            continue;      
           if(sr.Is_it_Approved__c  = TRUE){
            if(sr.AccountName__c != null ){
                Contact conRecord = new Contact(
                    FirstName = sr.FirstName__c,
                    LastName = sr.LastName__c,
                    Email = sr.Email__c,
                    AccountId = sr.AccountName__c
                );              
                System.debug('adding contact from line 36');
                conListToInsert.add(conRecord); 
            }
           
            if(sr.Email__c != null){
                emailMap.put(sr.Id, sr.Email__c);           
            }
            
            dummyMap.put(sr.Id, sr);                    
        }
    }
    //create Portal User
    if(conListToInsert != null && conListToInsert.size() > 0) {
        insert conListToInsert;
        List<Profile> pList = [select id from Profile where Name = 'Partner Community Login User - Custom'];
        if(!pList.isEmpty()){
            Profile p = pList[0];
            List<User> userListToInsert = new List<User>();
            for(Contact con :conListToInsert) {             
                String alias1 = '';
                if(con.FirstName.length() >= 0)
                    alias1 = con.FirstName.substring(0,1);
                    
                String alias2 = '';
                if(con.LastName.length() >= 4){
                    alias2 = con.LastName.substring(0, 4);        
                }else{
                    alias2 = con.LastName;
                }
                String alias = alias1 + alias2;
                User u = new User(
                    Alias = alias, 
                    Email = con.Email,
                    EmailEncodingKey = 'UTF-8', 
                    FirstName = con.FirstName,
                    LastName = con.LastName, 
                    LanguageLocaleKey = 'en_US', 
                    LocalesIdKey = 'en_US', 
                    ProfileId = p.Id, 
                    ContactId = con.id,
                    TimeZonesIdKey = 'America/Los_Angeles', 
                    UserName = con.Email
                );
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.EmailHeader.triggerUserEmail = true;
                u.setOptions(dmo);
                userListToInsert.add(u);
            }
                
            Database.SaveResult[] results = database.insert(userListToInsert, false);
            for(Database.SaveResult sr :results){
               if(!sr.isSuccess()){
                  Database.Error err = sr.getErrors()[0];
                  throw new CustomException(err.getMessage());
                 /* break;*/
               }
            }                       
        }        
    }  
    
   
    //define your custom exception
    
 
 }  
public class CustomException extends Exception{} 
}

 

All Answers

Swaroopa Akula 1Swaroopa Akula 1
This is the error
Steven NsubugaSteven Nsubuga
You defined the class inside a method.
Try this
public without sharing class AryaPortalUserCreationTriggerHandler{
    private static Map<Id, String> emailMap = null;
    private static List<Contact> conListToInsert = null;

    public static void beforeUpdate(Self_Register_User_Request__c[] srs) {
    
    String DUMMY_ACCOUNT  = 'Dummy Account' ;
    List<Contact> conListToInsert = new List<Contact>();
    
    Map<Id, String> emailMap = new Map<Id, String>();
    Map<Id, Self_Register_User_Request__c> dummyMap = new Map<Id, Self_Register_User_Request__c>();
    
    
    for(Self_Register_User_Request__c sr :(List<Self_Register_User_Request__c>) trigger.new)  { 
        System.debug('here is FN: '+sr.FirstName__c);
        System.debug('here is FN: '+sr.LastName__c);
        System.debug('here is FN: '+sr.Email__c);
        System.debug('here is FN: '+sr.AccountName__c);
        if(sr.FirstName__c == null || sr.LastName__c == null || sr.Email__c == null)  
            continue;      
           if(sr.Is_it_Approved__c  = TRUE){
            if(sr.AccountName__c != null ){
                Contact conRecord = new Contact(
                    FirstName = sr.FirstName__c,
                    LastName = sr.LastName__c,
                    Email = sr.Email__c,
                    AccountId = sr.AccountName__c
                );              
                System.debug('adding contact from line 36');
                conListToInsert.add(conRecord); 
            }
           
            if(sr.Email__c != null){
                emailMap.put(sr.Id, sr.Email__c);           
            }
            
            dummyMap.put(sr.Id, sr);                    
        }
    }
    //create Portal User
    if(conListToInsert != null && conListToInsert.size() > 0) {
        insert conListToInsert;
        List<Profile> pList = [select id from Profile where Name = 'Partner Community Login User - Custom'];
        if(!pList.isEmpty()){
            Profile p = pList[0];
            List<User> userListToInsert = new List<User>();
            for(Contact con :conListToInsert) {             
                String alias1 = '';
                if(con.FirstName.length() >= 0)
                    alias1 = con.FirstName.substring(0,1);
                    
                String alias2 = '';
                if(con.LastName.length() >= 4){
                    alias2 = con.LastName.substring(0, 4);        
                }else{
                    alias2 = con.LastName;
                }
                String alias = alias1 + alias2;
                User u = new User(
                    Alias = alias, 
                    Email = con.Email,
                    EmailEncodingKey = 'UTF-8', 
                    FirstName = con.FirstName,
                    LastName = con.LastName, 
                    LanguageLocaleKey = 'en_US', 
                    LocalesIdKey = 'en_US', 
                    ProfileId = p.Id, 
                    ContactId = con.id,
                    TimeZonesIdKey = 'America/Los_Angeles', 
                    UserName = con.Email
                );
                Database.DMLOptions dmo = new Database.DMLOptions();
                dmo.EmailHeader.triggerUserEmail = true;
                u.setOptions(dmo);
                userListToInsert.add(u);
            }
                
            Database.SaveResult[] results = database.insert(userListToInsert, false);
            for(Database.SaveResult sr :results){
               if(!sr.isSuccess()){
                  Database.Error err = sr.getErrors()[0];
                  throw new CustomException(err.getMessage());
                 /* break;*/
               }
            }                       
        }        
    }  
    
   
    //define your custom exception
    
 
 }  
public class CustomException extends Exception{} 
}

 
This was selected as the best answer
Swaroopa Akula 1Swaroopa Akula 1
worked!! thanks