• Aashi Aditi
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
public with sharing class UserName {
 public static void accountMethod(List<Contact> con )
    {
       List<User> newUsers = new List<User>();
    Profile p = [select id from Profile where name='Tester'];
    Map<String,Contact> contactsByNameKeys = new Map<String,Contact>();
           for (Contact c : con) 
    {
        if(c.firstname == null){
           //Show error for record not having any employees
           c.addError('Contact must have first name');
        }
        String userName = c.firstname + ' ' + c.lastname;
        contactsByNameKeys.put(userName,c);
        User a = new User(name=userName, profileid = p.id);
        newUsers.add(a);
    }
        insert newUsers;
    }
    }

Trigger to create New User on the basis of Contact’s FirstName , Contact’s Last Name.
For This :
Create new Profile (May be clone the System Admin Profile). And use this profile for tagging on User level.
insertaccount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.insertaccount: line 14, column 1

trigger insertaccount on Contact (after insert,after update) 
{
   set<id> accountIdList = new Set<id>();  
    for(contact con : Trigger.new)  
    {  
        accountIdList.add(con.accountid);  
    }  

    Map<Id,account> mapVar = new Map<Id,account>([SELECT id,name
                                                  FROM account WHERE Id IN : accountIdList]);

    for(contact cont : Trigger.new)
    {
            mapVar.get(cont.accountid).name = cont.firstname + cont.LastName;

    }
    update mapVar.values();
}

when I create a new contact so on basis of first name and last name of contact we have to create new account.
public with sharing class UserName {
 public static void accountMethod(List<Contact> con )
    {
       List<User> newUsers = new List<User>();
    Profile p = [select id from Profile where name='Tester'];
    Map<String,Contact> contactsByNameKeys = new Map<String,Contact>();
           for (Contact c : con) 
    {
        if(c.firstname == null){
           //Show error for record not having any employees
           c.addError('Contact must have first name');
        }
        String userName = c.firstname + ' ' + c.lastname;
        contactsByNameKeys.put(userName,c);
        User a = new User(name=userName, profileid = p.id);
        newUsers.add(a);
    }
        insert newUsers;
    }
    }

Trigger to create New User on the basis of Contact’s FirstName , Contact’s Last Name.
For This :
Create new Profile (May be clone the System Admin Profile). And use this profile for tagging on User level.