• Sam Epstein
  • NEWBIE
  • 30 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 3
    Replies
Hello!

​​​​​​I am building a process where partner users make multiple field updates to an object Fatburger_Menu__c (all the fields are currency), which will activate an approval process for one of our team members.

If the approval is rejected, I would want to create an Apex trigger that will revert all the field values that were changed back to their original values.
Another issue is that this object has over 100 fields available for editing so can't use field tracking in any way. I would also like to have an email triggered to the approver showing a list of the updated fields like this:

Hello,
Here are the list of fields that are being requested for update:

Field 1 Name: Field 1 Prior Value | Field 1 New Value
Field 2 Name: Field 2 Prior Value | Field 2 New Value
etc...

I do have an apex trigger that will run if the approval process is approved since this trigger stores the old and new values in a separate object so we can run reports. This grabs the fields from a field set on the object. Here is the code:
 
trigger FatburgerMenuPriceTracker on Fatburger_Menu__c (after update) {

final List<Schema.FieldSetMember> trackedFields = 
    SObjectType.Fatburger_Menu__c.FieldSets.MenuFieldSet.getFields();

if (trackedFields.isEmpty()) return;

final List<Menu_Price_Tracker__c> fieldChanges = 
    new List<Menu_Price_Tracker__c>();

if(!trigger.isUpdate)
    return;

for (Fatburger_Menu__c newAccount : trigger.new) {

    final Fatburger_Menu__c oldAccount = trigger.oldmap.get(newAccount.Id);

    for (Schema.FieldSetMember fsm : trackedFields) {

        String fieldName  = fsm.getFieldPath();
        String fieldLabel = fsm.getLabel();

        if (newAccount.get(fieldName) == oldAccount.get(fieldName))
            continue;

        String oldValue = String.valueOf(oldAccount.get(fieldName));
        String newValue = String.valueOf(newAccount.get(fieldName));
        String brand = newAccount.Brand__c;
        String locationName = newAccount.Location__c;


        if (oldValue != null && oldValue.length()>255) 
            oldValue = oldValue.substring(0,255);

        if (newValue != null && newValue.length()>255) 
            newValue = newValue.substring(0,255); 

        final Menu_Price_Tracker__c accountHistory = 
            new Menu_Price_Tracker__c();

        accountHistory.name         = fieldLabel;
        accountHistory.apiName__c   = fieldName;
        accountHistory.OldValue__c  = oldValue;
        accountHistory.NewValue__c  = newValue;

        accountHistory.Location_Name__c = locationName;
        accountHistory.Brand__c = locationName;


        fieldChanges.add(accountHistory);
    }
}

if (!fieldChanges.isEmpty()) {
    insert fieldChanges;
}
}
}

Any help on this would be amazing and thank you all so much for your help!
Hello,

I am trying to deploy this apex trigger from sandbox to production but keep getting this error:

Your organization's code coverage is 74%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.

I'm wondering what would be the best way to increase the code coverage with my trigger below. This trigger is used with Dropbox. Any help would be so appreciated!
 
trigger Dropbox_Trigger_Vendor on Vendor__c (after update, after delete) {
    
        if(Trigger.isAfter && Trigger.isUpdate){
            Dropbox_for_SF.HandleRecordChange.OnRecordChange(Trigger.old, Trigger.new);
        }
    
        if(Trigger.isAfter && Trigger.isDelete){
            Dropbox_for_SF.HandleRecordChange.HandleMerge(Trigger.old);
        }
}

 
Hi Everyone,

I have a chatter apex trigger that fires everytime there is an update to a field in my field set on my account obect. However, I want to reference the account name and the url as well as bolding/underlining the text in the chatter post. 

What would be the best way to do that?

Here is my code:
 
trigger AccountChatter on Account (after update) {
    List<Schema.FieldSetMember> lstTrackedFields = SObjectType.Account.FieldSets.AccountChatter.getFields();

    if (lstTrackedFields.isEmpty()) return;
    
    List<FeedItem> lstFieldChanges = new List<FeedItem>();
    
    if(!trigger.isUpdate) return;
    
    for (Account objNewAccount : trigger.new) {
        String chatterPostBody = '';
        final Account oldAccount = trigger.oldmap.get(objNewAccount.Id);
        // Iterate over all fields in Fieldset
        for (Schema.FieldSetMember objField : lstTrackedFields) {
            String fieldName  = objField.getFieldPath();
            String fieldLabel = objField.getLabel();          	

            if (objNewAccount.get(fieldName) == oldAccount.get(fieldName))
                continue;

            String oldValue = String.valueOf(oldAccount.get(fieldName));
            String newValue = String.valueOf(objNewAccount.get(fieldName));
     

            if (oldValue != null && oldValue.length()>255)
                oldValue = oldValue.substring(0,255);

            if (newValue != null && newValue.length()>255)
                newValue = newValue.substring(0,255); 
            
            chatterPostBody += UserInfo.getName()+' changed '+fieldLabel+' from '+oldValue +' to '+newValue+'\n'+'\n';       
        }
        if(String.isNotEmpty(chatterPostBody)){
            FeedItem post = new FeedItem();
            post.ParentId = objNewAccount.Id; // RecordId
            post.Body = chatterPostBody;
            
           
            FeedItem groupPost = new FeedItem();
            groupPost.ParentId = '0F94B0000008kd1SAA'; // GroupId
            groupPost.Body = chatterPostBody;
        
            lstFieldChanges.add(post);
            lstFieldChanges.add(groupPost);
        }
    }
    
  

    if (!lstFieldChanges.isEmpty()) insert lstFieldChanges;

}

 
Hi Community!

So I made a flow for closing a feedback case that works as a standard lightning component. However, when I put the flow in as part of a guided action list. The flow runs but doesn't update anything.

I noticed that this iw why the standard version works is because of this check mark where I pass my feedbackcaseID variable as the recordID:

User-added image

What would be the best way to grab the current record ID in my flow so that I can run it through the guided action list?

More than happy to attach screenshots of my current flow if that helps.

Thank you all this would be amazing if I can get this solved!!

 
Hi Everyone,
I have an apex trigger that tracks field updates on the custom object Menu__c. When an field in the object Menu__c field set is changed, a new record is created under the custom object 'Menu Price Changes'. 

So far, I only track the field name that is changed and the old and new value. I also want to add the record name to be added to the new Menu Price Changes record, specifically the location name or the Menu Name. 

What would be the best way to do this? If anything it would be great to have a lookup value field for the Menu Name under the Menu Pice Changes object and have it automatically filled in when a field is changed on the Menu Object. 

Any help on this would be amazing!!! I think I am missing something small in my code. 

User-added imageUser-added image
 
trigger AccountHistoryTracker on Menu__c (after update) {

    final List<Schema.FieldSetMember> trackedFields = 
        SObjectType.Menu__c.FieldSets.MenuHistoryFieldSet.getFields();

    if (trackedFields.isEmpty()) return;

    final List<Menu_Price_Tracker__c> fieldChanges = 
        new List<Menu_Price_Tracker__c>();

    if(!trigger.isUpdate)
        return;

    for (Menu__c newAccount : trigger.new) {

        final Menu__c oldAccount = trigger.oldmap.get(newAccount.Id);

        for (Schema.FieldSetMember fsm : trackedFields) {

            String fieldName  = fsm.getFieldPath();
            String fieldLabel = fsm.getLabel();

            if (newAccount.get(fieldName) == oldAccount.get(fieldName))
                continue;

            String oldValue = String.valueOf(oldAccount.get(fieldName));
            String newValue = String.valueOf(newAccount.get(fieldName));

            if (oldValue != null && oldValue.length()>255) 
                oldValue = oldValue.substring(0,255);

            if (newValue != null && newValue.length()>255) 
                newValue = newValue.substring(0,255); 

            final Menu_Price_Tracker__c accountHistory = 
                new Menu_Price_Tracker__c();

            accountHistory.name         = fieldLabel;
            accountHistory.apiName__c   = fieldName;
            accountHistory.OldValue__c  = oldValue;
            accountHistory.NewValue__c  = newValue;

            fieldChanges.add(accountHistory);
        }
    }

    if (!fieldChanges.isEmpty()) {
        insert fieldChanges;
    }
}

 
Hello,

I am trying to deploy this apex trigger from sandbox to production but keep getting this error:

Your organization's code coverage is 74%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.

I'm wondering what would be the best way to increase the code coverage with my trigger below. This trigger is used with Dropbox. Any help would be so appreciated!
 
trigger Dropbox_Trigger_Vendor on Vendor__c (after update, after delete) {
    
        if(Trigger.isAfter && Trigger.isUpdate){
            Dropbox_for_SF.HandleRecordChange.OnRecordChange(Trigger.old, Trigger.new);
        }
    
        if(Trigger.isAfter && Trigger.isDelete){
            Dropbox_for_SF.HandleRecordChange.HandleMerge(Trigger.old);
        }
}

 
Hi Everyone,
I have an apex trigger that tracks field updates on the custom object Menu__c. When an field in the object Menu__c field set is changed, a new record is created under the custom object 'Menu Price Changes'. 

So far, I only track the field name that is changed and the old and new value. I also want to add the record name to be added to the new Menu Price Changes record, specifically the location name or the Menu Name. 

What would be the best way to do this? If anything it would be great to have a lookup value field for the Menu Name under the Menu Pice Changes object and have it automatically filled in when a field is changed on the Menu Object. 

Any help on this would be amazing!!! I think I am missing something small in my code. 

User-added imageUser-added image
 
trigger AccountHistoryTracker on Menu__c (after update) {

    final List<Schema.FieldSetMember> trackedFields = 
        SObjectType.Menu__c.FieldSets.MenuHistoryFieldSet.getFields();

    if (trackedFields.isEmpty()) return;

    final List<Menu_Price_Tracker__c> fieldChanges = 
        new List<Menu_Price_Tracker__c>();

    if(!trigger.isUpdate)
        return;

    for (Menu__c newAccount : trigger.new) {

        final Menu__c oldAccount = trigger.oldmap.get(newAccount.Id);

        for (Schema.FieldSetMember fsm : trackedFields) {

            String fieldName  = fsm.getFieldPath();
            String fieldLabel = fsm.getLabel();

            if (newAccount.get(fieldName) == oldAccount.get(fieldName))
                continue;

            String oldValue = String.valueOf(oldAccount.get(fieldName));
            String newValue = String.valueOf(newAccount.get(fieldName));

            if (oldValue != null && oldValue.length()>255) 
                oldValue = oldValue.substring(0,255);

            if (newValue != null && newValue.length()>255) 
                newValue = newValue.substring(0,255); 

            final Menu_Price_Tracker__c accountHistory = 
                new Menu_Price_Tracker__c();

            accountHistory.name         = fieldLabel;
            accountHistory.apiName__c   = fieldName;
            accountHistory.OldValue__c  = oldValue;
            accountHistory.NewValue__c  = newValue;

            fieldChanges.add(accountHistory);
        }
    }

    if (!fieldChanges.isEmpty()) {
        insert fieldChanges;
    }
}