function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Manish Verma 4Manish Verma 4 

Contact and account should be created on LEAD CREATION.

Hi friends, 
I have a reuirement : whenever user creates a lead, an account and contact should be created. I have written a trigger for this. 
I am able to create a contact on lead creation but account is not getting created.
here is my trigger,your help is really appreciated !

trigger CreateUserContact on Lead(after insert) {
List<Contact> userContact = new List<Contact>();
Id rtypeId = [SELECT DeveloperName,Id FROM RecordType where SobjectType='Lead' AND 
DeveloperName = 'Birlasoft_Lead' AND IsActive=true limit 1].Id;
for (Lead u: Trigger.New) {
if (u.recordTypeId== rtypeId){
Contact c = new Contact ();
c.FirstName= u.FirstName;
c.LastName= u.LastName;
c.Phone= u.Phone;
c.Email= u.Email;
c.Designation__c= u.Designation__c;
c.Role__c= u.Role__c;
userContact.add(c);
}

}
insert userContact;
}

Thanks and regards
Manish Verma
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Manish,

Where is the Account creation code? it shouldn't be very different to the one you already have for contacts.

Regards
Manish Verma 4Manish Verma 4
Hi Carlos,
here is the complete code..

trigger CreateUserContact2 on Lead(after insert) {
    List<Contact> userContact = new List<Contact>();
    List<Account> userAccount = new List<Account>();
        Id rtypeId = [SELECT DeveloperName,Id FROM RecordType where SobjectType='Lead' AND DeveloperName = 'AMN_Lead' AND
                      IsActive=true limit 1].Id;
            for (Lead u: Trigger.New) {
                if (u.recordTypeId== rtypeId){
                    Contact c = new Contact ();
                        Account a = new Account();
                        a.Name= u.Account_Name__c;
                   c.FirstName= u.FirstName;
                    c.LastName= u.LastName;
                    c.Phone= u.Phone;
                    c.Email= u.Email;
                    c.Designation__c= u.Designation__c;
                    c.Role__c= u.Role__c;
                    userContact.add(c);
                        userAccount.add(a);
                    
}
}
insert userContact;
insert userAccount;
}

Regards,
Manish
                    



 
Waqar Hussain SFWaqar Hussain SF
Please ensure that the below query must return a result, and the lead record type must match the recordType returned by this query, other wise the trigger will not run.
SELECT DeveloperName,Id FROM RecordType where SobjectType='Lead' AND DeveloperName = 'AMN_Lead' AND
                      IsActive=true limit 1

Hava a look at this code. 
 
trigger CreateUserContact2 on Lead(after insert) {
    List<Contact> userContact = new List<Contact>();
    List<Account> userAccount = new List<Account>();
        Id rtypeId = [SELECT DeveloperName,Id FROM RecordType where SobjectType='Lead' AND DeveloperName = 'AMN_Lead' AND
                      IsActive=true limit 1].Id;
            for (Lead u: Trigger.New) {
                if (u.recordTypeId== rtypeId){
                    Contact c = new Contact ();
                        Account a = new Account();
                        a.Name= u.Account_Name__c;
                   c.FirstName= u.FirstName;
                    c.LastName= u.LastName;
                    c.Phone= u.Phone;
                    c.Email= u.Email;
                    c.Designation__c= u.Designation__c;
                    c.Role__c= u.Role__c;
                    userContact.add(c);
                        userAccount.add(a);
                    
				}
			}
	if(userContact.size()>0){
		insert userContact;
	}
	if(userAccount.size()>0){
		insert userAccount;
	}
}