+ Start a Discussion
mohimohi 

Re: trigger update chkbox true on updating picklist value

hi i hve custom obj candidateinfo__c:

DemoCountri picklist

trigger testchkboxtrigger on CandidateInfo__c (after update)
{
for(CandidateInfo__c c:trigger.new)
    {
    if(c.DEmoCountry__c =='us')
    {
       c.Home__c = true;
    }
    }
update  trigger.new;
}

plshelp trigger if fire exception

Hemant PatelHemant Patel

Please check the variable name "DemoCountri" and you have used "DEmoCountry__c".

 

If this is only writing mistake, then please provide exception detail.

 

Thanks,

Hemant

SargeSarge

Hi mohi,

 

 

    From your code, the trigger will fire recursivley. Because, it is a after Update trigger and in the code you have inculed "update trigger.new". This statement will again execute the update and then execute after update trigger again which will in turn.....and it goes till it hits governor limit. This might have caused an exception.

 

    If I have understood your requirement correctly, you have to check a check box field named "Home" in case DEmoCountry is "us". For this, you need to use "before update event" and no need for "update trigger.new".

So your code would look like this

 

trigger testchkboxtrigger on CandidateInfo__c (before update)
{
    for(CandidateInfo__c c:trigger.new)
    {
        if(c.DEmoCountry__c =='us')
        {
           c.Home__c = true;
         }
    }
}

Before update trigger executes just before actual update happens and during this time if you happen to do any changes on fields those values will be considered for DML update. And here, the system will automatically perform update, once.

 

Hope this helps