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
SFDC_2706SFDC_2706 

updating picklist fields using triggers...

this is my query...plzzz someone help me out...
 
-Create a picklist on Account object named Color Codes. Add values Red, Green and Blue to it. Create same picklist on Contact also.
 

-Create a trigger, if Red is selected delete the all related contacts, if Green is selected map the same value on related contacts, if Blue is selected clone all contacts to the parent account (Parent Account is a field on Account object) record.

 

thanks in advance...

 

this is what i have made...

 

trigger ColorCodeTrigger on Account (after insert,after update)
{
Set<Id> accIds = new Set<Id>();

for(Account con :trigger.new)
{
accIds.add(con.Id);
}

List<contact> conlist = new List<contact>([select id,AccountId from contact
where id in: accIds]);

for(Account con : trigger.new)
{
if(conlist.size() > 0 && conlist != null && con.color_codes__c == 'Red')
{
conlist.clear();
}

update conlist;
}
}

 

PremanathPremanath

Hi may be this code useful for you

 

 

trigger ColorCodeTrigger on Account (after insert,after update)
{
    Map<Id,String> accIds = new Map<Id,String>();
    Map<id,Contact> conmap=new Map<id,Contact>();
    List<contact> del=New List<Contact>();
    for(Account acc :trigger.new)
    {
        accIds.put(acc.Id,color_codes__c);
    }

    List<contact> conlist = new List<contact>([select id,AccountId,color_codes__c from contact where id in: accIds.keyset()]);
    for(integer i=0;i<conlist.size();i++){
        conmap.put(conlist[i].AccountId,conlist[i]);
    }
    for(integer i=0;i<accIds.size();i++){
      // For Deletion operation
        if(accIds.get(accIds[i])=='Red'){
            for(integer i=0;i<conlist.size();i++){
                if(accIds.containsKey(conlist[i].AccountId)){
                    del.add(conmap.get(conlist[i].AccountId));
                }
            }
        }
    }

    delete del;
    }
}

The Above code is for Delete operation when we select Red color
Like this you can do for another two colors also....