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
sfdc007sfdc007 

Trigger Req help needed

Hi,

I have a requirement for  insert trigger i need help on it


1) I have a custom object called as Suppressed Domain__c , where there are 2 fields like" Isactive" and "Domain"


2) I want to write a trigger for the below condition which i need help on it


a) If  "IsActive" is true then query contacts and leads with the same email domain

if records found(Yes)

there is another custyom object called as blacklist which i need to the following custom filds

set suppressed domain = true,
optout = true and
opt in = no opt in


if no records found , then set


set suppressed domain = true,
optout = true and
opt in = no opt in
email :temp dummy @dmain.com



kindly let me know how to achieve this with the code pls


Thanks in Advance
 
Best Answer chosen by sfdc007
sandeep sankhlasandeep sankhla
trigger VCESSuppressedDomainObject on Suppressed_Domain__c (after insert , after update) {
{
	map	mapActiveIdToDOmain = new map();
	list lstBlackListObject = new list();
	set setEmailIds = new set();
	
	for(Domain__c objDomain : trigger.new)
	{
		if(objDomain.isActive == true)
		{
			mapActiveIdToDOmain.put(objDomain.Id, objDomain.Domain);
		}
	}
	
	
	for(Contact obj :   [select Id from contact where Email IN :mapActiveIdToDOmain.values()])
	{
		setEmailIds.add(obj.Email);
	}
	
	for(Lead objLead : [select Id from Lead where Email IN :mapActiveIdToDOmain.values()])
	{
		setEmailIds.add(objLead.Email);
	}
	
	for(String strs : mapActiveIdToDOmain.values())
	{
		blacklist objBlack = new blacklist();
		if(setEmailIds.contains(strs))
		{
			objBlack. suppressed domain = 'true';
			objBlack.optout = true; 
			objBlack.opt in = 'no opt in';
		}
		else
		{
			objBlack. suppressed domain = true,
			objBlack.optout = 'true';
			objBlack.opt in = 'no opt in';
			objBlack.email :'temp dummy @dmain.com';
			
		}
		
		lstBlackListObject.add(objBlack);
	}
	if(!lstBlackListObject.isEmpty())
		insert lstBlackListObject;
}

Please refer the bewlo code and correct the API names as I am not aware abou the names...please chcek and let me kniw if it helos..

Thanks,
Sandeep

All Answers

sandeep sankhlasandeep sankhla
Hi,

there is another custyom object called as blacklist which i need to the following custom filds

from above line, is this related to the doman__c object on which we are writting trigger ?
 
sfdc007sfdc007
I am writing my trigger on suppressed domain object
trigger VCESSuppressedDomainObject on Suppressed_Domain__c (after insert , after update) {

List Leademaildomain = new List{};
List contactemaildomain= new List{};


// create a new list to hold the objects to update
List blacklistupdate = new List();

for (Suppressed_Domain__c sd : Trigger.new) {
        
        
        // here is where you check if suppressed domain that is being inserted
        //meets the criteria
        if (sd.Active__c = true) {  

//Quering for Lead and contact object email domain fields



}
}

}
i need to check the email fiels from contact and lead and if they are same i h ave to update the fields in blacklist object , which i am fnding it difficult and need help on it


kindly help me on this regard
 
sandeep sankhlasandeep sankhla
Hey,
 what is the relationship between blacklist object  and lead adn contact?
sfdc007sfdc007
there is no relationship between blacklist object , lead and contact

i just have to verify the emails in both lead and contact are having same domains and then is so i have to update the fields on blacklist object


they are independent to each other
sandeep sankhlasandeep sankhla
Hi,

Okat lets take one example:

I am inserting 3 Suppressed_Domain__c  object okay where isActive is true ..

1.  Suppressed_Domain__c  1   -  email is s@ss.com   -   conatc and lead found 2 with same domain   ----?? new blacklist object you will create here ?
2. Suppressed_Domain__c  2   -   email is s@34.com   - contact and lead found 1 with same domain   ---  ??new object or you will update existing one
3. Suppressed_Domain__c  3   -  email is 5@34444.com - 0 contact and lead found --- ??

Please check above example and let me know what you need

if I have 3 domain object and they are having different email and we found different conatc and lead for all, then which blacklist obhecjt should create or update for whihc??

Thanks,
Sandeep


 
sfdc007sfdc007
Hi,


1.  Suppressed_Domain__c  1   -  email is s@ss.com   -   conatc and lead found 2 with same domain   ----?? new blacklist object you will create here ?


in case 1 if both are matching i have to update the following fields in blacklist object as follows

set suppressed domain = true,
optout = true and
opt in = no opt in


if no records found , then set


set suppressed domain = true,
optout = true and
opt in = no opt in
email :temp dummy @dmain.com






3. Suppressed_Domain__c  3   -  email is 5@34444.com - 0 contact and lead found --- ??

case 3:

i can leave it as it is as the email domains are not matching(no insert needed)



hope it helps
sfdc007sfdc007

insert -> is active true ->yes -> query contact and leads with same email domain -> records found -> yes -> Upsert unique emails to
Blacklist:
Suppressed Domain = TRUE
Opt Out = TRUE
Opt In = No Opt In



insert -> is active true ->yes -> query contact and leads with same email domain -> records found -> No->Insert Dummy Record in Blacklist table
Suppressed Domain = TRUE
Opt Out = TRUE
Opt In = No Opt In
Email: tempDummy@
sandeep sankhlasandeep sankhla
One last question...upsert means what ?? if there is blacklist record then you will update else insert right ? if this is the case then which blaklist record we should update??

if for all records we are creating newly blacklist record with values then it is simple ...but if you are updating means which record you should update ??
sfdc007sfdc007


we are creating a new blacklist record if the condition is met sandeep thats it , no upsert the existing ones on blacklist object


 
sandeep sankhlasandeep sankhla
Thanks!! give me sometime will share the code with you...
sfdc007sfdc007
Thanks dude
sandeep sankhlasandeep sankhla
trigger VCESSuppressedDomainObject on Suppressed_Domain__c (after insert , after update) {
{
	map	mapActiveIdToDOmain = new map();
	list lstBlackListObject = new list();
	set setEmailIds = new set();
	
	for(Domain__c objDomain : trigger.new)
	{
		if(objDomain.isActive == true)
		{
			mapActiveIdToDOmain.put(objDomain.Id, objDomain.Domain);
		}
	}
	
	
	for(Contact obj :   [select Id from contact where Email IN :mapActiveIdToDOmain.values()])
	{
		setEmailIds.add(obj.Email);
	}
	
	for(Lead objLead : [select Id from Lead where Email IN :mapActiveIdToDOmain.values()])
	{
		setEmailIds.add(objLead.Email);
	}
	
	for(String strs : mapActiveIdToDOmain.values())
	{
		blacklist objBlack = new blacklist();
		if(setEmailIds.contains(strs))
		{
			objBlack. suppressed domain = 'true';
			objBlack.optout = true; 
			objBlack.opt in = 'no opt in';
		}
		else
		{
			objBlack. suppressed domain = true,
			objBlack.optout = 'true';
			objBlack.opt in = 'no opt in';
			objBlack.email :'temp dummy @dmain.com';
			
		}
		
		lstBlackListObject.add(objBlack);
	}
	if(!lstBlackListObject.isEmpty())
		insert lstBlackListObject;
}

Please refer the bewlo code and correct the API names as I am not aware abou the names...please chcek and let me kniw if it helos..

Thanks,
Sandeep
This was selected as the best answer
sfdc007sfdc007
thanks dude , i will check
sfdc007sfdc007
Hi,

I tried the following code

getting the error

Error: Compile Error: Incompatible value type String for Map at line 12 column 13

 
trigger VCESSuppressedDomainObject on Suppressed_Domain__c (after insert , after update) {

    Map  mapActiveIdToDOmain = new Map  ();
    List lstBlackListObject = new List();
    Set  setEmailIds = new Set  ();
 
    
    for(Suppressed_Domain__c  objDomain : trigger.new)
    {
        if(objDomain.isActive == true)
        {
            mapActiveIdToDOmain.put(objDomain.Id, objDomain.Domain__c);
        }
    }
    
    
    for(Contact obj :   [select Id from contact where Email IN :mapActiveIdToDOmain.values()])
    {
        setEmailIds.add(obj.Email);
    }
    
    for(Lead objLead : [select Id from Lead where Email IN :mapActiveIdToDOmain.values()])
    {
        setEmailIds.add(objLead.Email);
    }
    
    for(String strs : mapActiveIdToDOmain.values())
    {
        BlackList__c objBlack = new BlackList__c();
        if(setEmailIds.contains(strs))
        {
            objBlack.Domain_Suppressed__c = true;
            objBlack.Opt_Out__c = true; 
            objBlack.Opt_In__c = 'no opt in';
        }
        else
        {
            
            objBlack.Domain_Suppressed__c = true;
            objBlack.Opt_Out__c = true; 
            objBlack.Opt_In__c = 'no opt in';
            objBlack.Email_Address__c ='temp dummy @dmain.com';
            
        }
        
        lstBlackListObject.add(objBlack);
    }
    if(!lstBlackListObject.isEmpty())
        insert lstBlackListObject;
}


line 12 is

            mapActiveIdToDOmain.put(objDomain.Id, objDomain.Domain__c);


kindly help me on this regard

Thanks
sandeep sankhlasandeep sankhla
You can change the map data type..it is currently string...you can keep DOman__C..
sfdc007sfdc007
i changed the code , let me know if its right

 
trigger VCESSuppressedDomainObject on Suppressed_Domain__c (after insert , after update) {

    Map  mapActiveIdToDOmain = new Map  ();
    List lstBlackListObject = new List();
    Set  setEmailIds = new Set  ();
 
    //checking for the entry condition if is active is true
    for(Suppressed_Domain__c  objDomain : trigger.new)
    {
        if(objDomain.Active__c == true)
        {
            mapActiveIdToDOmain.put(objDomain.Id, objDomain.Domain__c);
        }
    }
    
    //Quering the email from the contact object
    for(Contact obj :   [select Id,Email  from contact where Email IN :mapActiveIdToDOmain.values()])
    {
        setEmailIds.add(obj.Email);
    }
    //Quering the email from the lead object
    for(Lead objLead : [select Id,Email  from Lead where Email IN :mapActiveIdToDOmain.values()])
    {
        setEmailIds.add(objLead.Email);
    }
    //Mapping the values and creating the Blacklist object record
    for(String strs : mapActiveIdToDOmain.values())
    {
        BlackList__c objBlack = new BlackList__c();
        if(setEmailIds.contains(strs))
        {
            objBlack.Domain_Suppressed__c = true;
            objBlack.Opt_Out__c = true; 
            objBlack.Opt_In__c = 'No Opt In';
        }
        else
        {
            
            objBlack.Domain_Suppressed__c = true;
            objBlack.Opt_Out__c = true; 
            objBlack.Opt_In__c = 'No Opt In';
            objBlack.Email_Address__c ='temp dummy @domain.com';
            
        }
        
        lstBlackListObject.add(objBlack);
       
    }
     if(!lstBlackListObject.isEmpty())
        insert lstBlackListObject;
}

 
sfdc007sfdc007
I am getting the following error , on creating a new record on suppressed domain object


Apex script unhandled trigger exception by user/organization: 005U0000003gI34/00DJ0000003P150 Source organization: 00DU0000000HiA8 (null)
VCESSuppressedDomaininsert: execution of AfterInsert
 
caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Email Address]: [Email Address]
 
Trigger.VCESSuppressedDomaininsert: line 50, column 1


line 50 :


insert lstBlackListObject;
 
sandeep sankhlasandeep sankhla
Please provide all required fields while inserting record...
sfdc007sfdc007
there are only 2 fields in this object that is

active and domain

email address is from blacklist object only

no iea why this error is thrwong
sandeep sankhlasandeep sankhla
It seems email address is also there which is required..please chck again
sfdc007sfdc007
thanks dude , it worked :)