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
chandu kumarchandu kumar 

Account and Contact lookup Fields should be blank on case in Email to Case


I am trying to make account and contact lookup fields for a particular account. i am able to update the status field and send email to respective contact as it is spam. i couldn't able to update the account and contact fields are blank.there is no filters on fields. i tried code provided below
Trigger SpamCatch on Case(before Insert)
{  

    for(Case newCase: Trigger.New)
    {   
        EmailTemplate template=[SELECT HtmlValue,Body FROM EmailTemplate WHERE Name='SpamCatchEmail'];


        if(newCase.Origin=='Email' && newCase.Subject.contains('Auto Response'))
        {
            newCase.addError('This is auto response mail. Aborting case creation.');
        }

        Integer NexComm = 0;
        String k;

        if(newCase.Origin =='Email' && newCase.SuppliedEmail !=null  )
        {      
            for(Contact cObj: [Select id, Name, email, Accountid from contact where email = :newCase.SuppliedEmail and Accountid in (select id from account where name =: System.Label.NexComm)])
            {
                NexComm = 1;
                k=cobj.Name;
            }

            for(Contact cObj: [Select id,Name, email, Accountid from contact where email = :newCase.SuppliedEmail and Accountid in (select id from account where name !=: System.Label.NexComm)])
            {
                NexComm = 0;
                k=cobj.Name;
            }


            if(newCase.Origin =='Email' && newCase.SuppliedEmail !=null  )
            { 
                String hbody='';
                String plainTxtBody='';
                hbody=template.HtmlValue;
                hbody=hbody.replace('{!Contact.Name}',k);
                plainTxtBody=template.Body;

                List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                List<String> sendTo = new List<String>();
                sendTo.add(newCase.SuppliedEmail );
                System.debug(newCase.SuppliedEmail);

                mail.setToAddresses(sendTo);

                OrgWideEmailAddress[] owea = [select Id,DisplayName,Address from OrgWideEmailAddress where Address = 'portal.support@nexenta.com' limit 1];
                //mail.setReplyTo('portal.support@nexenta.com');

                List<String> ccTo = new List<String>();
                ccTo.add('satheesh.thondamanti@nexenta.com');
                mail.setCcAddresses(ccTo);
                mail.setTemplateID(template.Id);//This is the template you are setting
                mail.setSubject(' Email to Case Response');
                //String body = 'Dear ' + Contact.FirstName + Contact.LastName + ', ';
                //body += 'Thank you for your email. Your support request cannot be processed at the moment. Support is available only for Nexenta customers who has valid support contract.For any queries on your community edition, please go to https://community.nexenta.com and post your query. If you have valid support contract, please get in touch with your Nexenta sales team with purchase details to get access to Nexenta support portal.';
                mail.setOrgWideEmailAddressId(owea.get(0).Id);
                mail.setHtmlBody(hbody);
                // mail.setPlainTextBody(plainTxtBody);

                // mail.setTreatTargetObjectAsRecipient(true);
                mails.add(mail);
                System.debug(mail);
                Messaging.sendEmail(mails);
                //Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            } 
            if ( NexComm == 1 )
            {
                newCase.Status='DCL'; 
                newCase.AccountId='';
                newCase.ContactId='';
                system.debug('Status:'+newCase.Status);
                System.debug(NexComm  + '3' + newCase.SuppliedEmail);  

            }
        }
    }
}