AnsweredAssumed Answered

add multiple email addresses to lead in code

Question asked by Francesca Shiekh on Sep 26, 2018
Latest reply on Feb 1, 2019 by jclark

I have a custom scheduler that does a bunch of stuff and then needs to loop through a number of email addresses and add them to a newly created Lead with id $lead_id.


The addresses may or may not already exist.

The following will add the email addresses and relate them to the Lead but it will also set all but the last email address added to deleted = 1




      foreach($all_email_addresses as $address){
        if(!empty($address) && !empty($lead_id) && filter_var($address, FILTER_VALIDATE_EMAIL)){
            $sea = new SugarEmailAddress;
            $sea->addAddress($address, $primary); //add as primary
            $sea->save($lead_id, "Leads");
            $primary = false;



Sugar v7.9.4 but will also need to work in v8.1