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
sales4cesales4ce 

Get the Id of Inserted Contact

Hello,

 

I am doing email to apex.In this regard, i need to get the Id of contact that is inserted and then use that Id to create a Task.

For some reason i am unable to do it.

There is another post which says to get the Inserted record Id as "Object.Id". but i am unable to do it.

 

Here is my Code:

 

global class ProcessApplicants implements
Messaging.InboundEmailHandler
{

    contact[] mycontact=new contact[0];
    
    global Messaging.InboundEmailResult handleInboundEmail
    (Messaging.InboundEmail email,Messaging.InboundEnvelope env)
    {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        string emailaddress=env.fromaddress;
        string[] emailbody=email.plainTextBody.split('\n',0);
        string fname=emailbody[0];
        string lname=emailbody[0];
        try
        {
            mycontact.add(new contact(FirstName=fname,LastName=lname));
            insert mycontact;         
            
        }
        catch(system.DMLexception e)
        {
           System.debug('Error:Not able to create Contact:'+e); 
        }
       
       
        Task myTask=new Task();
        myTask.Subject='Make a Call';
        myTask.Description='For Testing Purpose';
        myTask.Priority='Normal';
        //myTask.WhatId=c_Id.Id;
        
        
            
        
return result;
}

Can any one help me here. I need to get the inserted contact Id then use that contact Id to assign a task.

 

Thanks in advance!

Sales4ce

David VPDavid VP

You have access to the Id immediately after you've saved the record :

 

Contact c = new Contact();
c.LastName = 'Test';
insert c;
System.Debug(c.Id);

 

 

sales4cesales4ce

Thanks David for reply!

 

Within my code after "Insert mycontact" i tried to do the following:

 

Id c_id=mycontact.Id;

 

But this results in an compilation error. I need to immediately use that ID to insert a new task for that contact.

Am i doing anything wrong?

 

Thanks,

Sales4ce

pcmca_2007@yahoo.co.inpcmca_2007@yahoo.co.in

Hi,

     I am new to salesforce.i want to write a trigger on contacts and associate a new task to that contact after checking condition that if newly created task is already not associated with that contact.

 

 

With regards

Prabhash