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
Cloud AtlasCloud Atlas 

Trigger Misfiring

I have written below Cross Object value transition trigger ...
When the status field on Monthly Activity is either of the 4 values, I would like to capture these 4 values from Approver_Editorial/Financial and ApproverE/F_Date ... And then assign them to 4 fields on ExpertMmonthlyAudit object...

The only connection between the 2 objects is via a lookup.

Unfortunately the fields on the 'ExpertMmonthlyAudit' are still blank.
Not sure what I am doing wrong.
Can any one care to point out.
Thanks!
trigger updateValues on Monthly_Activity__c (before insert, before update) {
    Set<Id> Ids= new Set<Id>();
    for ( Monthly_Activity__c m: Trigger.new){
        if(m.Status__c == 'Rejected by Editorial' || m.Status__c == 'Submitted to Finance' 
        || m.Status__c == 'Rejected by Finance' || m.Status__c == 'Approved'){
               Ids.add(m.Id);
         }
       }  
    List<Monthly_Activity__c> mList = new List<Monthly_Activity__c>([SELECT Id, Status__c, Approver_Editorial__c, Approver_Financial__c,ApproverE_Date__c, ApproverF_Date__c FROM Monthly_Activity__c WHERE Id in:Ids]);
    
    for(Monthly_Activity__c temp : mList){
    
    ExpertMonthlyAudit__c mVal = new ExpertMonthlyAudit__c();
    mVal.EditName__c = temp.Approver_Editorial__c;
    mVal.FinName__c = temp.Approver_Financial__c;
    mVal.EditDate__c =temp.ApproverE_Date__c;
    mVal.FinDate__c = temp.ApproverF_Date__c;
    insert mVal;
                                                                          
   }
}

 
Saurabh BSaurabh B
Hi Cloud, what is the name of lookup field on ExpertMonthlyAudit__c  object? Also, you would like to create a new "ExpertMonthlyAudit" record is that correct?
Cloud AtlasCloud Atlas
Hey Saurabh,

Lookup field API ... Lookup_Monthly_Activity__c
And to asnwer your second question .... No. Status field for a single record will go through multiple steps, and I would like to update the 4 values based on status progression.
The reason is I need all affiliated records based on each status__c value as I need to display all those values on a progress bar.
So I need Name and Date from each Status level.
 
PRABHAKARAN CHOCKALINGAMPRABHAKARAN CHOCKALINGAM
My understanding is that you are trying to capture the version changes in the Audit table based on the status changes. And thus you are inserting new Audit records which is what Saurabh is asking for. Please let me know if my understanding is not correct.
Below is the code based on that,
trigger updateValues on Monthly_Activity__c (before insert, before update) {

    List<ExpertMonthlyAudit__C> expMnthlyAdt = new List<ExpertMonthlyAudit__C>();

    for (Monthly_Activity__c m: Trigger.new) {
        if (m.status__c != Trigger.oldMap.get(m.ID).status__c) {
            if (m.Status__c == 'Rejected by Editorial' || m.Status__c == 'Submitted to Finance'
                    || m.Status__c == 'Rejected by Finance' || m.Status__c == 'Approved') {

                ExpertMonthlyAudit__c mVal = new ExpertMonthlyAudit__c();
                mVal.Lookup_Monthly_Activity__c = m.ID;
                mVal.EditName__c = m.Approver_Editorial__c;
                mVal.FinName__c = m.Approver_Financial__c;
                mVal.EditDate__c = m.ApproverE_Date__c;
                mVal.FinDate__c = m.ApproverF_Date__c;

                expMnthlyAdt.add(mVal);
            }
        }
    }
if(expMnthlyAdt.size() > 0) {
    insert expMnthlyAdt;
}
}
Cloud AtlasCloud Atlas
Hey Prabhakaran,

So the deal is that the entire approval life cycle needs to be shown as an Audit Log..
Unsubmitted >> Submitted By>> <Details of user> >> Approved/Rejected By >><Details of user> >>  Submitted for 2nd approval >> Approved/Rejected By >> <Details of user>

I used your approach , but the fields are still empty...
I checked the list view for the object and found that at every stage, the trigger is creatying a new records...
Here are the 3 screen shots ..

Upon initial submission...
Upon initial submission

After 1st approval..

User-added image

After 2nd Approval...

User-added image

 
PRABHAKARAN CHOCKALINGAMPRABHAKARAN CHOCKALINGAM
But the screenshot showing these fields getting populated after 2nd approval.
Is the fields on the MonthlyAudittable has these fields populated. I know this sounds silly, but wanted to confirm.
Could you add debug statement for these four fields and see how its going after every stage of approval.
Cloud AtlasCloud Atlas
Hey Prabhakaran,

Yes, the fields did get populated.. But by the 3rd record, Salesforce lost the parent record of Monthly Activity for which it is supposed to siplay this information ...
See that Month, Year, User, Timestamp, Type, Total are all filled out for the first screenshot.
Starting second, we have a blank.
Probably that why my Audit Log shows blank for the EditName/EditDate and FinName/FinDate... because Salesforce is not able to capture from where the information is supposed to come in...