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
Louisa Pidcock 11Louisa Pidcock 11 

populate checkbox (after insert, after update) - please can anyone help me

Hi Guys 

I am very very new to coding of any kind so please forgive me/ thank you in advance.

Custom Object:  Buyer_Conact__c

I have a workflow rule that updates the due date if it falls on a weekend day. As 'Day__c'  is derived via DATE() function in a formula thisrule only kicks in if the record it edited, which falls short of what is required. 

I have added a checkbox called Weekend_Date_Handler__c which I would like to check after insert (as this will trigger the workflow) 


I am attempting to refactor a piece of code I sucessfully created (you guys then helped me with the test class) for a different purpose and am not getting very far at all. My what I want is for the code to look at any buyer_contact__c records that have Weekend_Date_Handler__c= False and update them to Weekend_Date_Handler__c = True 

Here is what I have written so far. Any guidance here is massively appreciated !


trigger weekendUpdate on Buyer_Contact__c (after insert, after update) {
    
    List<Id> lstId = new List<Id>();    
    
    for(Buyer_Contact__c buyerConTwo: Trigger.new){
        
        List<Buyer_Contact__c> buyerConTwoList = [SELECT Id,Weekend_Date_Handler__c,  FROM Buyer_Contact__c WHERE Weekend_Date_Handler__c = FALSE] ;
        
        buyerConTwo.Weekend_Date_Handler__c= TRUE
        
        update buyerConTwoList;
    }
}



 
Manish  ChoudhariManish Choudhari
Hi Louisa,

It is not a good idea to update the record in "After Update" trigger. Use "Before Update" and "Before Insert" for this operation. Also you do not have to write the update statement ("update buyerConTwoList; ") as the code will automatically either insert or update the records (based on which trigger is executing)

Your code should look like below:
 
trigger weekendUpdate on Buyer_Contact__c (before insert, before update) {
    
    
    //This for loop will go thorough all the records inside trigger
    for(Buyer_Contact__c buyerConTwo: Trigger.new){

        // Below code will check if the record is having current value as false
        // if the value is false, if statemet will get executed 
        // will change the value to true
        // after that system will automatically update or insert the record
        // based on current trigger context
        if(! buyerConTwo.Weekend_Date_Handler__c){
             buyerConTwo.Weekend_Date_Handler__c = true;
        }
    }
}
Let me know if this helps. 


**Please mark this as best answer if this answers your query.**

Thanks,
Manish Choudhari
14x Certified Salesforce Architect

Certification link: http://certification.salesforce.com/certification-detail-print?conId=003G000002gRrrEIAS​
My Blog: http://sfdcfacts.com/
Youtube Channel: https://www.youtube.com/SFDCFacts
LinkedIn: https://www.linkedin.com/in/manish-choudhary/
Trailhead: https://trailhead.salesforce.com/en/me/manish-choudhari
Twitter: https://mobile.twitter.com/manish_sfdc
Louisa Pidcock 11Louisa Pidcock 11
Thank you so much for your reply Manish but this is not working for me .. what I want is to just 'jig' any save records so that the workflow rule kicks in. The way I want to edit the recently created records is to check the checkbox that is unchecked by default upon creation. If i cjheck the box before insert then 
Louisa Pidcock 11Louisa Pidcock 11
oops excuse me that saved to early! 

...if i populate the checkbox before insert then the WFR will not fire.. does this make sense? Essentialy i want to edit any newly created records so that they invoke the wfr that moves the date along ...
Louisa Pidcock 11Louisa Pidcock 11
I am trying to avoid having to write the 'update date' logic in the trigger as I am such a novice and this is time sensitive (else I would love to send time trying to write it all in the trigger) ... or are you saying that it just is not possible? thanks again so much for your guidance!
Louisa Pidcock 11Louisa Pidcock 11
( To be clear, I do not expect anyone to care about my deadlines here, I am just wondering if what I have thought of doing is even viable... 
I have tested your suggested code (thanks again so much) and it is not doing what i need it to unfortunately becuase the wf only fires when the record is edited 
Manish  ChoudhariManish Choudhari
Hi Louisa,

You mean your workflow is not firing? 

What entry criteria do you have? is it "created, and every time it’s edited"

It should always fire whether the record is created or edited. Can you please check debug logs to see if the workflow is fired or not? 

 
Louisa Pidcock 11Louisa Pidcock 11
Hi Manish - thanks for coming back! Yes I guess tha ts what I shoul dhave said , the workflow is not firing. Below is the workflow rule which I did set as per your above advise 

User-added image


I still dont understand how this would trigger the workflow rule if it is 'before insert' but  that is probably because im a bit stupid!?
 I will try to explain it better as I think my explanation might be a part of the problem.... 



1. Process A )  - Creates a Buyer Contact record with a Due Date = TODAY()+2
   
Louisa Pidcock 11Louisa Pidcock 11
1. Process A )  - Creates a Buyer Contact record with a Due Date = TODAY()+2
                           The Buyer contact record has a formula field  'Target Completion Day' which dervies the day from the due date.   
2. Process B)  - Looks at the Target Completion date and if it is a Saturday , add 2 days to the due date , if its Sunday it adds 1 day to the Due date


Unfortunately, process B is only triggered if the record is editied. To try to solve this I created a checkbox,  default unchecked upon creation. I want the trigger to loook for these records and check the box (which is essentially just editing the record so that it enters process B)

Does this make any difference to what you have advised so far? So sorry if I am just being dumb here ...