You need to sign in to do that
Don't have an account?
Yashasvi
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;
}
}
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;
}
}
I found below issue in your code
1) SOQL inside Loop
2) DML inside Loop.
I fixed all issue. Please try below code:-
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
I found below issue in your code
1) SOQL inside Loop
2) DML inside Loop.
I fixed all issue. Please try below code:-
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
but since you are setting a parent record id you can simply set it as sub.Company_Submitting__c without writing a seperate query.
* you have query inside for loop it is not a best practice use a map to avoid it.
It worked. Thank you so much.
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?
mapNameWiseContact.put(cont.Email,cont); // This is line 18
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);
}