Salesforce Apex触发器中的参考对象字段

时间:2019-03-22 05:10:12

标签: salesforce apex apex-trigger

嗨,堆栈溢出社区!

Noob问题,但是我有一个salesforce聊天记录顶点触发器,每次在帐户对象上设置的字段中的字段有更新时,都会在聊天记录中发布一条消息。但是,我想引用帐户名,URL,如果有的话,可以在聊天记录正文中加粗/加下划线。

什么是最好的方法?我真的很挣扎,非常需要帮助!!谢谢!!

我的代码在这里:

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); 

            //chatter body post - this is where I want to add account name and url
            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;

}

0 个答案:

没有答案