0

I have this awesome code running in my org. I would like to create a case to the accounts in the query I run and want to add a case to track. But I am getting the System.ListException: Before Insert or Upsert list must not have two identically equal elements error and I could not create a case

Below is my code. Please help me understand where I am I missing to write the insert statement.

public class PD_WelcomeMaroPost {

    @future(callout=true)
    public static void sendEmailThroughMaro(string myInpEmail) {
        string successContacts = '';
        string failureContacts = '';

        List<Stripe_Subscripton__c> subsToUpdate = new List<Stripe_Subscripton__c>();
        List<Case> newCase = new List<Case>();
        Case cs = new Case();

        // SQL to fetch FBO who Joined Today
        list<Account> conts = new list<Account> ([SELECT Id, name, Email_FLP_com__c,
        (SELECT Id
        FROM Stripe_Subscriptons__r
        WHERE Start_Date__c= TODAY
            AND Status__c='active'
            AND Welcome_Email__C = false
        LIMIT 1)
    from account
    where ID IN (
        select Distributor__c
        from Stripe_Subscripton__c
        where Start_Date__c= TODAY
            AND Status__c='active'
            AND Welcome_Email__C = false)
    AND  Email_FLP_com__c != NULL
    LIMIT 100]);

        system.debug('>>>>>>>>>>' + conts);
        overallEmail myEmail = new overallEmail();

        for(Account c : conts){

            myEmail.email.campaign_id = 172;
            myEmail.email.bcc = '[email protected]';
            myEmail.email.contact.Email = c.Email_FLP_com__c;
            myEmail.email.contact.first_name = c.name;

            /**MAp<String, String> tags = new Map<String, String>();
            tags.put('firstName', c.name);
            myEmail.email.tags = tags;**/
            system.debug('#### Input JSON: ' + JSON.serialize(myEmail));


            try{
                String endpoint = 'http://api.maropost.com/accounts/1173/emails/deliver.json?auth_token=j-V4sx8ueUT7eKM8us_Cz5JqXBzoRrNS3p1lEZyPUPGcwWNoVNZpKQ';
                HttpRequest req = new HttpRequest();
                req.setEndpoint(endpoint);
                req.setMethod('POST');
                req.setHeader('Content-type', 'application/json');
                req.setbody(JSON.serialize(myEmail));
                Http http = new Http();
                system.debug('Sending email');
                HTTPResponse response = http.send(req); 
                system.debug('sent email');
                string resultBodyGet = '';
                resultBodyGet = response.getBody();
                system.debug('Output response:' + resultBodyGet);
                maroResponse myMaroResponse = new maroResponse();
                myMaroResponse = (maroResponse) JSON.deserialize(resultBodyGet, maroResponse.class);
                system.debug('#### myMaroResponse: ' + myMaroResponse);
                if(myMaroResponse.message == 'Email was sent successfully')
                   successContacts = successContacts + ';' + c.Email_FLP_com__c;
                else
                    failureContacts = failureContacts + ';' + c.Email_FLP_com__c;
            }
            catch (exception e) {
                failureContacts = failureContacts + ';' + c.Email_FLP_com__c;
                system.debug('#### Exception caught: ' + e.getMessage());                
            }

            c.Stripe_Subscriptons__r[0].Welcome_Email__c = true;
            c.Stripe_Subscriptons__r[0].Welcome_Email_Sent_Date__c = system.today();
            subsToUpdate.add(c.Stripe_Subscriptons__r[0]);


               cs.RecordTypeId = '012d0000000hIcH';
               cs.Status = 'Welcome Email Sent';
               cs.AccountId = c.Id;
               cs.OwnerId = '005d000000363QwAAI';
               cs.Subject = 'FLP360 New User';
               cs.Description = 'Email Sent Succesfully';
               cs.origin = 'Email';          
               newCase.add(cs);


        }

        Update subsToUpdate;
        insert newCase;
        system.debug('newCase');

1 Answer 1

3

You're currently declaring the Case outside of the for-loop over Accounts:

Case cs = new Case();
...
for(Account c : conts){
    ...
    cs.RecordTypeId = '012d0000000hIcH';
    cs.Status = 'Welcome Email Sent';
    cs.AccountId = c.Id;
    cs.OwnerId = '005d000000363QwAAI';
    cs.Subject = 'FLP360 New User';
    cs.Description = 'Email Sent Succesfully';
    cs.origin = 'Email';
    newCase.add(cs);
}

So, each iteration is updating the same instance of Case, and adding it to the newCase list each time.

You need to move the Case declaration inside the for loop, so that each cs is different:

for(Account c : conts){
    Case cs = new Case();
    ...
    cs.RecordTypeId = '012d0000000hIcH';
    cs.Status = 'Welcome Email Sent';
    cs.AccountId = c.Id;
    cs.OwnerId = '005d000000363QwAAI';
    cs.Subject = 'FLP360 New User';
    cs.Description = 'Email Sent Succesfully';
    cs.origin = 'Email';
    newCase.add(cs);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .