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 BeginerSFDC Beginer 

Trigger after insert/after update

Hi am facing below Error.
Logic needs to be implemented :If type is Contract Flow and Document Attached is True then HandOffAttached =True, otherwise False
I have one Designation  custom Object
created 3 fileds Type(picklist), document attached(checkbox) ,  Handoffattached(picklist)..
I have written below code:

trigger DesignationTrigger on Designation__c (after insert, before update) {
    // If type is Contract Flow and Document Attached is True then HandOffAttached =True, otherwise False
 
    for(Designation__c dc:Trigger.new)
    {
        if(dc.type__c=='ContractFlow' && dc.DocumentAttached__c==True)
        {
           dc.HandOffAttached__c='True';
        }
      else
          dc.HandOffAttached__c='True';
    }
  
}

I am getting the below error.  I am not sure whether i follwed correct approch or not for the gven logic .could any pleasae please guide me on this ? I really need your help in these triggers.
Apex trigger DesignationTrigger caused an unexpected exception, contact your administrator: DesignationTrigger: data changed by trigger for field HandoffAttached: bad value for restricted picklist field: True
Best Answer chosen by SFDC Beginer
Jainam ContractorJainam Contractor
Hi,

You should be using only before event in your trigger as you want to update the same record which caused the trigger to fire.

In the IF condition code block, there is something which is not letting the trigger to execute the IF condition.

What are the Possible values for type__c field value. Is 'ContractFlow' the correct API name for the Value in type__c field...???

To be on a safer side, I would suggest you to use curly braces '{}' for the else part of the code as well.

Also please check the debug log whether the code ever executes the IF part...

Please check and let me know if any discrepancy or you need any more assistance.

P.S. Trigger will not update the existing records in the database. There needs to be any DML on those records to invoke the Trigger.

Thanks,
Jainam Contractor

All Answers

Jainam ContractorJainam Contractor
Hi,

It seems that you are trying to assign the bad value to the Picklist field HandOffAttached__c. May i know what are the values in this HandOffAttached__c field. Also you should execute this trigger for Before Insert and Before Update event and not After Insert.

Please let me know the values in the fields HandOffAttached__c so that i can help you further.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Rakshana Cube84Rakshana Cube84
Hi,

It sounds a bad value on the HandOffAttached__c as Jainam said. You might have used 'Yes' value on the picklist value. So keep check with the picklist value and put it as below.

HandOffAttached__c ='Yes' 

I hope it would help you to achieve.



Thanks,
Rakshana.

 
SFDC BeginerSFDC Beginer
Hi Jainam/Rakshna
since my logic is to update the HandoffAttached filed by chekcing 2 other fileds..so I have used before Update, and we have alreay record thatsy I used after insert....
could you please correct me here...using after insert instead of before instead as you said.
and Comming to Handoffattached__C it is Yes or No.

trigger DesignationTrigger on Designation__c (after insert, before update) {
    // If type is Contract Flow and Document Attached is True then HandOffAttached =True, otherwise False
    List<Designation__c> d=new List<Designation__c>();
    
    for(Designation__c dc:Trigger.new)
    {
        if(dc.type__c=='ContractFlow' && dc.DocumentAttached__c==True)
        {
           dc.HandOffAttached__c='Yes';
        }
      else
          dc.HandOffAttached__c='No';
    }

}

now am not facing hte bad value error but I cann see HandoffAttached value always No when I update the record. Could you please suggest me whats wrong i have done..

type is picklist
DocumentAttached is checklist.

Please let me know if you need any further details.
Jainam ContractorJainam Contractor
Hi,

You should be using only before event in your trigger as you want to update the same record which caused the trigger to fire.

In the IF condition code block, there is something which is not letting the trigger to execute the IF condition.

What are the Possible values for type__c field value. Is 'ContractFlow' the correct API name for the Value in type__c field...???

To be on a safer side, I would suggest you to use curly braces '{}' for the else part of the code as well.

Also please check the debug log whether the code ever executes the IF part...

Please check and let me know if any discrepancy or you need any more assistance.

P.S. Trigger will not update the existing records in the database. There needs to be any DML on those records to invoke the Trigger.

Thanks,
Jainam Contractor
This was selected as the best answer
SFDC BeginerSFDC Beginer
Hi Jainam,
yes its wokring good now. But am confused sometimes when to use Before and after triggers.
I have few queries related to this code we have done above.
> becase we want to update the same record we are using Before event triggers 
and while inserting the record it looks for validation and then it will execute before insert trigger fired right?
and once the reocrd is created i want to update it..so any updates that will do can be execute before update only.....but it seems like the update is done the fileds once the updataion is done(after update)
for example

Type not contractflow
Document attached is checked
I set the Handoffattached to Yes...
but once I updated I can see HandoffAttached filed as No.
 Could you give some clarity on why it happens like this..correct me if my thoughts/understandng is wrong 
 
Jainam ContractorJainam Contractor
Hi,

BEFORE Triggers are used when you want to make some changes/ updates any fields on the Same record which triggered the TRIGGER while the AFTER trigger are used if you want to update/ make changes to any related records.

For eg: In your case, you wanted to update the Designation Object record on some condition in the same record so use BEFORE trigger for the same. Say Designation has related Employee Object, so based on the changes in the Designation Object record you want to update related Employee Object record then you should use AFTER trigger.

And BEFORE trigger will always execute before the Custom Validation rule that you define.

In your case, even if you set the Handoffattached to YES, it will fire the BEFORE update event trigger and as it doesn't match the IF condition criteria, it will update the field value to NO.

Please let me know if i am able to help you understand the events.

Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
SFDC BeginerSFDC Beginer
Hi Jainam,
the logic si same but now I want to Update the same filed in Opportynity (Lookup). I have written below code, but it is not updating? what needs to be add to work the logic.

trigger DesignationOpp on Designation__c (after insert,after update) {
    
 //   List<Opportunity> opps=new List<Opportunity>();
    for(Designation__c dc:Trigger.new)
    {
        Opportunity op=new Opportunity();
        if(op.ID==dc.LookupOpp__c)
            {
             if(dc.type__c=='ContractFlow' && dc.DocumentAttached__c==True)
             {
                  op.HandOffAttached__c='Yes';
             }
             else
            {
                op.HandOffAttached__c='No';}
                 
        }
       else
       {
            op.HandOffAttached__c='None';
       }
   
    }
}
SFDC BeginerSFDC Beginer
Hi Jainam,
the exaplanation has given by you is helped me alot :), thank you so much
SFDC BeginerSFDC Beginer
Review all error messages below to correct your data.
Apex trigger DesignationOpp caused an unexpected exception, contact your administrator: DesignationOpp: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.DesignationOpp: line 22, column 1
code is below.
trigger DesignationOpp on Designation__c (after insert,after update) {
    
    List<Opportunity> opps=new List<Opportunity>();
    for(Designation__c dc:Trigger.new)
    {
        Opportunity op=new Opportunity();
        if(op.ID==dc.LookupOpp__c)
            {
             if(dc.type__c=='ContractFlow' && dc.DocumentAttached__c==True)
             {
                  op.HandOffAttached__c='Yes';
             }
             else
            {
                op.HandOffAttached__c='No';}
              
        }
       else
       {
            op.HandOffAttached__c='None';
       }
            update op; 
        //opps.add(op);
    }
    //update opps;
}

could anyone please help me what needs to be modifiled or added
Jainam ContractorJainam Contractor
Hi,

First of all you need to understand the events. If you use BEFORE event triggers and dealing with the same record which triggered the Trigger, you don't need to explicitly add the DML as the triggers are triggered due to some updation on the record and records are updated before the transaction is completed. For AFTER event triggers, the record which triggered the Trigger are read only and you need to explicitly add the DML to the related record/ list that you are trying to update.

Also when you are trying to compare Opp Id with the Id of the Opportunity on the Designation record, you need to fetch all the related Opportunity before you could compare.

Please find the below code and check if it helps you:
trigger DesignationOpp on Designation__c (after insert,after update) {
    List<Opportuntiy> oppUpdList = new list<Opportunity>();
    Set<Id> oppId=new Set<Id>();
    for(Designation__c dc:Trigger.new)
    {
        if(dc.LookupOpp__c != NULL && dc.type__c=='ContractFlow' && dc.DocumentAttached__c==True)
            {
             opps.add(dc.LookupOpp__c);
            }
    }
    List<Opportunity> oppList = [select id, name, HandOffAttached__c from Opportunity where Id IN :oppId];
    for(Designation__c dc : Trigger.new){
        for(Opportunity O : oppList){
            if(dc.LookupOpp__c == O.Id){
                O.HandOffAttached__c = 'Yes';
                oppUpdList.add(O);
            }else{
                O.HandOffAttached__c = 'No';
                oppUpdList.add(O);
            }
        }
    }
    if(oppUpdList != NULL && oppUpdList.size()>0){
        update oppUpdList;
    }
}

Please let me know if it works.

Thanks,
Jainam Contractor​
SFDC BeginerSFDC Beginer
Hi Jainam,

this code doenst worked. I didnt see the Handoffattached filed updation  in opprtunity at all..
what can be the possbible thing we can do now?
Jainam ContractorJainam Contractor
Hi,

Can you please check the debug log to see if you are getting any Opportunity Id in opps set. There is some issue with the Field API names i guess.

Please check the debug log and reconfirm.

Thanks,
Jainam Contractor
SFDC BeginerSFDC Beginer
Hi jainam,
I am the admin for my org (Trail version), still am unable to see any debug logs. User-added image
Jainam ContractorJainam Contractor
Hi,

You need to set the date/time for the Debug log to appear. Select the dates when you are trying to implement/ fire the trigger.

Or you can open developer console and then try to check in the logs for the trigger execution.

Please check and let me know so that i can help you further.

Also i would suggest you to create a new thread/ question which will let other community member also help you

Thanks,
Jainam Contractor