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
YashasviYashasvi 

Requesting help on updating a Lookup field

Hi,

I am trying to create a new Contact upon updating a record in a custom object (Qualification__c).

Fields in Qualification__c object are: (1) Company__c (Lookup to Accounts)  (2) Signer_Name__c (Text)  (3) Email_Signer__c (Email)
Upon updating Qualification__c, all the three fields must be used to create a new Contact.
Signer_Name__c must be Contact's Last Name
Email_Signer__c must be Contact's Email field
Company__c must be Contact's Account.

The trigger I've written creates a new contact with Name and Email populated, but Account field on Contact remains empty. I don't know where my code is wrong. Please see my cide below and help me with this.

trigger CreateNewContact on Qualification__c (after update) {
     
List<Contact> cList = new List<Contact>();
        for (Qualification__c sub : Trigger.New){
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) ){
                 List<Contact> contactExists = [SELECT Id, LastName FROM Contact WHERE LastName = :sub.Signer_Name__c];
                 if(contactExists == null) {
                        Contact c = new Contact();
                        c.LastName = sub.Signer_Name__c;
                        c.Email = sub.Email_Signer__c;
                        c.AccountId = sub.Company_Submitting__r.Id;
                        cList.add(c);
                  }
             }
             insert cList;
       }
}
 
Best Answer chosen by Yashasvi
Amit Chaudhary 8Amit Chaudhary 8
Hi Yashasvi,

I found below issue in your code
1) SOQL inside Loop
2) DML inside Loop.

I fixed all issue. Please try below code:- 
trigger CreateNewContact on Qualification__c (after update) 
{

		Set<String> setID = new Set<String>();
		Set<String> setSignerName = new Set<String>();
        for (Qualification__c sub : Trigger.New)
		{
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) )
			{
				setID.add(sub.id);
				setSignerName.add(sub.Signer_Name__c);
			}	
		}
		
		List<Qualification__c> lstQual = [select id,Signer_Name__c,Email_Signer__c,Company_Submitting__r.Id from Qualification__c where id in :setID]; // Please add field which u want
		List<Contact> lstContact = [ SELECT Id, LastName FROM Contact WHERE LastName in :setSignerName ];

		Map<String,Contact> mapNameWiseContact = new Map<String,Contact>();
		for(Contact cont : lstContact)
		{
			mapNameWiseContact.put(LastName,cont);
		}

		
		List<Contact> cList = new List<Contact>();
        for (Qualification__c sub : lstQual)
		{
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) )
			{
                 if(mapNameWiseContact.containsKey(sub.Signer_Name__c) == false) 
				 {
                        Contact c = new Contact();
                        c.LastName = sub.Signer_Name__c;
                        c.Email = sub.Email_Signer__c;
                        c.AccountId = sub.Company_Submitting__r.Id;
                        cList.add(c);
                 }
             }
       }
	   if(cList.size() > 0 )
	   {
			insert cList;
	   }
}

Always follow trigger best practice 
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help


Thanks
Amit Chaudhary
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Yashasvi,

I found below issue in your code
1) SOQL inside Loop
2) DML inside Loop.

I fixed all issue. Please try below code:- 
trigger CreateNewContact on Qualification__c (after update) 
{

		Set<String> setID = new Set<String>();
		Set<String> setSignerName = new Set<String>();
        for (Qualification__c sub : Trigger.New)
		{
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) )
			{
				setID.add(sub.id);
				setSignerName.add(sub.Signer_Name__c);
			}	
		}
		
		List<Qualification__c> lstQual = [select id,Signer_Name__c,Email_Signer__c,Company_Submitting__r.Id from Qualification__c where id in :setID]; // Please add field which u want
		List<Contact> lstContact = [ SELECT Id, LastName FROM Contact WHERE LastName in :setSignerName ];

		Map<String,Contact> mapNameWiseContact = new Map<String,Contact>();
		for(Contact cont : lstContact)
		{
			mapNameWiseContact.put(LastName,cont);
		}

		
		List<Contact> cList = new List<Contact>();
        for (Qualification__c sub : lstQual)
		{
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) )
			{
                 if(mapNameWiseContact.containsKey(sub.Signer_Name__c) == false) 
				 {
                        Contact c = new Contact();
                        c.LastName = sub.Signer_Name__c;
                        c.Email = sub.Email_Signer__c;
                        c.AccountId = sub.Company_Submitting__r.Id;
                        cList.add(c);
                 }
             }
       }
	   if(cList.size() > 0 )
	   {
			insert cList;
	   }
}

Always follow trigger best practice 
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help


Thanks
Amit Chaudhary
 
This was selected as the best answer
surasura
when you are using trigger.new you can only access the fileds on that  record only  if you want to access parent record fields like this ​sub.Company_Submitting__r.Id they return null . if you want to access records parent record fields you should write a seperate query .

but since you are setting a parent record id you can simply set it as sub.Company_Submitting__c without writing a seperate query.

 
trigger CreateNewContact on Qualification__c (after update) {
     
List<Contact> cList = new List<Contact>();

        for (Qualification__c sub : Trigger.New){
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) ){
                 List<Contact> contactExists = [SELECT Id, LastName FROM Contact WHERE LastName = :sub.Signer_Name__c];
                 if(contactExists == null) {
                        Contact c = new Contact();
                        c.LastName = sub.Signer_Name__c;
                        c.Email = sub.Email_Signer__c;
                        c.AccountId = sub.Company_Submitting__c;
                        cList.add(c);
                  }
             }
             insert cList;
       }
}

* you have  query inside for loop  it is not a best practice use a map to avoid it.

 
YashasviYashasvi
Hi Amit,

It worked. Thank you so much.
YashasviYashasvi
Hi Amit,

In the code you mentioned, I'm trying to use Email as the Map identifier. But, it shows some error as below.
Error: Compile Error: Method does not exist or incorrect signature: [Map<String,Contact>].put(String) at line 18 column 13

The Error part ofcode is as shown:
 Map<String,Contact> mapNameWiseContact = new Map<String,Contact>();
        for(Contact cont : lstContact) {
            mapNameWiseContact.put(cont.Email); // This is line 18
        }
I have included Email in the SOQL Query for Contacts, and rest of the code is same.

Can you please tell me what this error mean, and how to overcome this?
surasura
it should be 
mapNameWiseContact.put(cont.Email,cont); // This is line 18
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-

 List<Contact> lstContact = [ SELECT Id, Email,LastName FROM Contact WHERE LastName in :setSignerName ];
        Map<String,Contact> mapNameWiseContact = new Map<String,Contact>();
        for(Contact cont : lstContact)
        {
            mapNameWiseContact.put(cont.Email,cont);
        }
YashasviYashasvi
I missed that detail. Thank you both.