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
Sameer VitsSameer Vits 

update specific field under contact if contact already exists

I have a trigger autoconverting leads into Contact when someone submits the web to lead form. I want to update 'session' field under Contact, if Contact (converted lead) already exists. If we can check this using email address. I am new with Apex and need help with writing the code.  
Best Answer chosen by Sameer Vits
Narender Singh(Nads)Narender Singh(Nads)
Hi Sameer,
Your code will look something like this:
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
            Leadconvert.setLeadId(LeadIds[0]);
            lead l= [SELECT Id, email FROM Lead WHERE id=:LeadIds[0]];
            LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            contact[] clist=[select id,name,session__c from contact where email=:l.email limit 1 ];
            
            if(clist.size()>0){
                contact c=clist[0];
                c.session__c='PUT_THE_VALUE_YOU_WANT_TO_UPDATE_THE_FIELD_WITH'; //Make sure you are inserting value according to field type.
                update c;
            }
            else{    
                Leadconvert.setConvertedStatus(Leads.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion 
                Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
                System.assert(Leadconverts.isSuccess());
           }
   }
}

Let me know if it helps.
Thanks!

All Answers

Neha AggrawalNeha Aggrawal
Hi Sameer,

I understand your requirement. You need to query contact object using that email. If the query returns any result, that contact field can be updated. And no conversion will occur.
May be you can post your code here, so accordingly we can see what modifications are needed.
Thanks.
Narender Singh(Nads)Narender Singh(Nads)
Hi Sameer, 

Your code will look something like this:
contact c=[select id,name, email,session__c from contact where email=:l.email limit 1];

if(ls.size()>0){
  // Update your  c.session__c 
}
else{
 //Do nothing
}
Please note that code can vary depending upon your trigger.

I might be able to help you better if you could please share your trigger code.

Thanks!
Sameer VitsSameer Vits
So auto-lead conversion code is below 
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
            Leadconvert.setLeadId(LeadIds[0]);
            LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            Leadconvert.setConvertedStatus(Leads.MasterLabel);
            Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion 
            Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
            System.assert(Leadconverts.isSuccess());
   }
}

Requirements that we are trying to work on now is:

1) if email exists, then update contact session field 
2) if email doesn't exits, then create new record
Narender Singh(Nads)Narender Singh(Nads)
Hi Sameer,
Your code will look something like this:
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
            Leadconvert.setLeadId(LeadIds[0]);
            lead l= [SELECT Id, email FROM Lead WHERE id=:LeadIds[0]];
            LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            contact[] clist=[select id,name,session__c from contact where email=:l.email limit 1 ];
            
            if(clist.size()>0){
                contact c=clist[0];
                c.session__c='PUT_THE_VALUE_YOU_WANT_TO_UPDATE_THE_FIELD_WITH'; //Make sure you are inserting value according to field type.
                update c;
            }
            else{    
                Leadconvert.setConvertedStatus(Leads.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion 
                Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
                System.assert(Leadconverts.isSuccess());
           }
   }
}

Let me know if it helps.
Thanks!
This was selected as the best answer
Sameer VitsSameer Vits
@Narender - The session value comes from web to lead form depending on person signing up. It's a picklist. The values are dates as mentioned - May 24; 2 PM - 4 PM, June 28; 9 AM - 12 PM, May 24; 10 AM -12 PM, June 28; 4:30 PM - 7:30 PM, July 26; 9 AM - 12 PM, July 26; 4:30 PM - 7:30 PM

So it depends on what date they select and that date value should be inserted into c.session__c
Narender Singh(Nads)Narender Singh(Nads)
Hi,
For that I suggest you pass that picklist field value a function parameter to your function from your caller.
 
Sameer VitsSameer Vits
I am not quite familiar with that. Can you help me on that as well?
Narender Singh(Nads)Narender Singh(Nads)
Hi Sameer,
Please mark this post as solved and ask your query in a new post..
Post the link of that post here.
Sameer VitsSameer Vits
@Narender - I am getting this exception error on the code -
Error element myRule_1_A1 (FlowActionCall).
An Apex error occurred: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, There was an error converting the lead.
Narender Singh(Nads)Narender Singh(Nads)
Hi,
I tested the Code, it is running fine. Can you please check what are you getting in 'LeadIds' using system.debug statements
Sameer VitsSameer Vits
Variable doesn't exists - LeadIds
Narender Singh(Nads)Narender Singh(Nads)
Please send me the code you are using.
 
Sameer VitsSameer Vits
@Narender I am just using - System.Default(LeadIds) in test anonymous window