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
Krishna_Krishna_ 

Trigger Issue

hi i am creating "Email" as Custom object. & add to Create a lookup relation for "Email" in contact.Email object have the custom fields of name & email id.if I add one email to the email id equals to contacts then The contact details add to email's releated list.if is not mathes the email id nothing is happen in email - contact related list.If i update that mail id to new one it removes the old contact & update new contact in email related list..I am creating this Trigger but,it show the error in updating time.So How to handle this one for Trigger.new & Trigger.old concept????

 

trigger trg_Lookup_Contact on Email_Suppression__c (after insert,before update)
{
    for(Email_Suppression__c emailobj : Trigger.New)
    {
        contact[] conobj ;
        if(Trigger.Isupdate)
        {
            conobj = [select name,email,Email_Suppression__c from contact where email =: emailobj.EmailId__c OR email =: Trigger.old.EmailId__c];
        }
        else
        {
            conobj = [select name,email,Email_Suppression__c from contact where email =: emailobj.EmailId__c];
        }
        integer inte = conobj.size();
        for(integer i = 0;i < inte;i++)
        {
            if(emailobj.EmailId__c == conobj[i].email)
            {
                conobj[i].Email_Suppression__c = emailobj.id;
                update conobj[i];
            }
            else
            {
                conobj[i].Email_Suppression__c = null;
                update conobj[i];
            }
        }
    }
}

Message Edited by Krishna_ on 04-21-2009 02:59 PM
Best Answer chosen by Admin (Salesforce Developers) 
Michael_KahleMichael_Kahle

Hi Krishna,

 

Trigger.Old and Trigger.New are both lists. You can not access Fields through that. Other than those lists there are Trigger.newMap and Trigger.oldMap which holds a Map<Id,OBJECTTYPE>(in your case Map<Id,Email_Suppression__c>).

 

With this you can gain access to a specific old record :

 

 

mail =: Trigger.oldMap.get(emailobj.Id).EmailId__c

 

 This is how you can gain access to the old record while iterating through Trigger.New.

 

 

Also i noticed, that your trigger is not Bulk Secure as you are making a select call for each Email_suppression__c record you get in the trigger. If someone updates 200 records at once you would need 200 select calls andrun intogovernor limits.

 

Iterate through the trigger once first and get all ids. Then select all contacts and put them in a Map<Id,Contact> and use this for your needs.