• Mattrey
  • NEWBIE
  • 0 Points
  • Member since 2007

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 9
    Replies

I've got a trigger that runs on FeedComment and I need the id of the user inserting it so according to the documentation I should be able to get this via feedComment.InsertedById but debug shows this as null and if I debug the feed comment I get something like: FeedComment:{ParentId=0055000000XXXXXXXX, CommentBody=my comment body, IsDeleted=false, FeedItemId=0D5R000000XXXXXXXX}

 

Why don't I see an InsertedById?

I'm trying to create a map from a query list I created so I can access it by Id - here's the relevant code:

 

      List<User>userResults= new List<User>();
      set<Id>userIds = new Set<Id>();
      Map<Id,User>userData = new Map<Id,User>();

 

//code that fills user ids

 

    //get the relevant user data      
    userResults=[SELECT Id,MyField1__c,MyField2__c FROM User WHERE Id in: userIds];


    //put it into a map for easier access
    for (User u : userResults){
        userData.add(u.Id,u); //here's where the error is
    }

My List is of type User, the for loop u value is of type User and my map is Id, User so why doesn't it work?

Is there a way to determine the status of a trigger (active/inactive) in a test method?

I have tests that have assert() calls in them that assume the trigger is active. If I need to deactivate the trigger, I then need to go and change the test so that it still passes. It would be nice to be able to do something like:

if (trigger.status='Active'){

  System.assert(trigger was fired);

}

Recently I have been playing around with Canvas, a technology that seems quite promising for the integration of third party applications within salesforce.

 

The canvas JavaScript API provides ways of making cross domain http request at an asynchronous level. I am interested in having synchronous method calls, and I don´t know whether that can be achieved. 

 

The following is the JavaScript example (combining the two examples given in the manual)

 

 

        function displayChatterUsers() {
          var NumberUsers = 0;
          // Reference the Chatter user's URL from Context.Links object.
          var chatterUsersUrl = canvasContext.context.links.chatterUsersUrl;

          // Make an XHR call back to salesforce through the supplied browser proxy.
          Sfdc.canvas.client.ajax(chatterUsersUrl,
            {
              token: canvasContext.oauthToken,
              success: function (data) {
                // Make sure the status code is OK.
                if (data.status === 200) {
                  numberUsers = data.payload.users.length;
                  // Alert with how many Chatter users were returned.
                  alert( "Got back " + numberUsers +
                    " users"); // Returned 2 users
                }
              }
            });
          alert( "Test for the number of users is " + numberUsers );
            var url = canvasContext.context.links.chatterFeedsUrl + "/news/" + canvasContext.context.user.userId + "/feed-items";
            var body = { body: { messageSegments: [{ type: "Text", text: "The number of users is " + numberUsers}]} };
          Sfdc.canvas.client.ajax( url,
            {
              token: canvasContext.oauthToken,
              method: 'POST',
              contentType: "application/json",
              data: JSON.stringify( body ),
              success: function( data ) {
                if ( 201 === data.status ) {
                  alert( "Success" );
                }
              }
            } );
        }

 When running the above mentioned example, you could see that the Sfdc.canvas.client.ajax get delayed in its execution, disallowing me from use of the results of the method call in outer levels of nesting (for instance, in the top level alert()).

I tried adding async:true as part of the values for the method call (as one might infer from the canvas-all.js specification) with no luck.

 

Any hints are greatly appreciated.

 

Andy

I've got a trigger that runs on FeedComment and I need the id of the user inserting it so according to the documentation I should be able to get this via feedComment.InsertedById but debug shows this as null and if I debug the feed comment I get something like: FeedComment:{ParentId=0055000000XXXXXXXX, CommentBody=my comment body, IsDeleted=false, FeedItemId=0D5R000000XXXXXXXX}

 

Why don't I see an InsertedById?

I'm trying to create a map from a query list I created so I can access it by Id - here's the relevant code:

 

      List<User>userResults= new List<User>();
      set<Id>userIds = new Set<Id>();
      Map<Id,User>userData = new Map<Id,User>();

 

//code that fills user ids

 

    //get the relevant user data      
    userResults=[SELECT Id,MyField1__c,MyField2__c FROM User WHERE Id in: userIds];


    //put it into a map for easier access
    for (User u : userResults){
        userData.add(u.Id,u); //here's where the error is
    }

My List is of type User, the for loop u value is of type User and my map is Id, User so why doesn't it work?

Is there a way to determine the status of a trigger (active/inactive) in a test method?

I have tests that have assert() calls in them that assume the trigger is active. If I need to deactivate the trigger, I then need to go and change the test so that it still passes. It would be nice to be able to do something like:

if (trigger.status='Active'){

  System.assert(trigger was fired);

}

When developing a Visualforce page for overiding view page for any object, one problem that creeps up is to display the History details of a record. The standard related list Component doesn't works for History.

 

With the help of some code from Community ( I now can't find the link to it :( ), I wrote my own code  then to display the history of an object. It mimics the standard list as far as possible.  

 

Heres the code. It is for the Case object but it can be used for any other object.

 1.Component Code

 

<apex:component controller="CaseHistoriesComponentController">
<!-- Attribute Definition -->
<apex:attribute name="CaseId" description="Salesforce Id of the Case whose Case History needs to be rendered" type="Id" required="true" assignTo="{!caseId}" />

<!-- Case History Related List -->
<apex:pageBlock title="Case History">
<apex:pageBlockTable value="{!histories}" var="History" >
<apex:column headerValue="Date" value="{!History.thedate}"/>
<apex:column headerValue="User"> <apex:outputLink value="/{!History.userId}"> {!History.who} </apex:outputLink></apex:column>
<apex:column headerValue="Action"><apex:outputText escape="false" value="{!History.action}"/></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:component>

 

 

 

 

2. Apex Code

 

public class CaseHistoriesComponentController {

public Id caseId {get; set;}
public cHistories[] histories;

// Variables
public Static final Map<String, Schema.SObjectField> CaseFieldmap = Schema.SObjectType.Case.fields.getMap();
public Static final List<Schema.PicklistEntry> fieldPicklistValues = CaseHistory.Field.getDescribe().getPicklistValues();

public List<cHistories> getHistories()
{
list<cHistories> histories = new list<cHistories>();
String prevDate = '';
for(CaseHistory cHistory : [Select CreatedDate, CreatedBy.Name, CreatedBy.Id, Field, NewValue, OldValue from CaseHistory where CaseId = :caseId order by CreatedDate desc])
{
if((cHistory.newValue == null && cHistory.oldValue == null)
|| (cHistory.newValue != null && !(string.valueOf(cHistory.newValue).startsWith('005') || string.valueOf(cHistory.newValue).startsWith('00G')))
|| (cHistory.oldValue != null && !(string.valueOf(cHistory.oldValue).startsWith('005') || string.valueOf(cHistory.oldValue).startsWith('00G'))))
{
cHistories tempHistory = new cHistories();
// Set the Date and who performed the action
if(String.valueOf(cHistory.CreatedDate) != prevDate)
{
tempHistory.theDate = String.valueOf(cHistory.CreatedDate);
tempHistory.who = cHistory.CreatedBy.Name;
tempHistory.userId = cHistory.CreatedBy.Id;
}
else
{
tempHistory.theDate = '';
tempHistory.who = '';
tempHistory.userId = cHistory.CreatedBy.Id;
}
prevDate = String.valueOf(cHistory.CreatedDate);

// Get the field label
String fieldLabel = CaseHistoriesComponentController.returnFieldLabel(String.valueOf(cHistory.Field));

// Set the Action value
if (String.valueOf(cHistory.Field) == 'created') { // on Creation
tempHistory.action = 'Created.';
}
else if(cHistory.OldValue != null && cHistory.NewValue == null){ // when deleting a value from a field
// Format the Date and if there's an error, catch it and re
try {
tempHistory.action = 'Deleted ' + Date.valueOf(cHistory.OldValue).format() + ' in <b>' + fieldLabel + '</b>.';
} catch (Exception e){
tempHistory.action = 'Deleted ' + String.valueOf(cHistory.OldValue) + ' in <b>' + fieldLabel + '</b>.';
}
}
else{ // all other scenarios
String fromText = '';
if (cHistory.OldValue != null) {
try {
fromText = ' from ' + Date.valueOf(cHistory.OldValue).format();
} catch (Exception e) {
fromText = ' from ' + String.valueOf(cHistory.OldValue);
}
}

String toText = '';
if (cHistory.OldValue != null) {
try {
toText = Date.valueOf(cHistory.NewValue).format();
} catch (Exception e) {
toText = String.valueOf(cHistory.NewValue);
}
}
if(toText != '')
tempHistory.action = 'Changed <b>' + fieldLabel + '</b>' + fromText + ' to <b>' + toText + '</b>.';
else
tempHistory.action = 'Changed <b>' + fieldLabel;
}

// Add to the list
histories.add(tempHistory);
}
}

return histories;
}

// Function to return Field Label of a Case field given a Field API name
public Static String returnFieldLabel(String fieldName)
{
if(CaseHistoriesComponentController.CaseFieldmap.containsKey(fieldName))
return CaseHistoriesComponentController.CaseFieldmap.get(fieldName).getDescribe().getLabel();
else
{
for(Schema.PicklistEntry pickList : fieldPicklistValues)
{
if(pickList.getValue() == fieldName)
{
if(pickList.getLabel() != null)
return pickList.getLabel();
else
return pickList.getValue();
}
}
}
return '';
}
// Inner Class to store the detail of the case histories
public class cHistories {

public String theDate {get; set;}
public String who {get; set;}
public Id userId {get; set;}
public String action {get; set;}
}
}

  Let me know your views on the code or if you have any questions