• DePlunge
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

HI, 

 

I'm pretty new to this APEX lark and am not sure if I'm doing this right, so any assistance would be greatly appreciated. 

 

I have written a trigger (adapted from one that I have found online). The triggers purpose is to (before update) map the OwnerId field to a custom User Lookup of my custom object Change_Request__c. 

 

TRIGGER DETAIL

 

 

 
trigger onChangeReqBeforeUpdate on Change_Request__c (before update) {
    
    Set<Id> ownerIds = new Set<Id>();
    Map<Id, List<Change_Request__c>> owner = new Map<Id, List<Change_Request__c>>();
        for(Change_Request__c cr : trigger.new){
            ownerIds.add(cr.OwnerId);
            List<Change_Request__c> crq = new List<Change_Request__c>();
            if(owner.containsKey(cr.OwnerId)) crq = owner.get(cr.OwnerId);
               crq.add(cr);
               owner.put(cr.OwnerId, crq);
            } 
        for(User u : [SELECT Id FROM User WHERE Id IN :ownerIds]){
        for(Change_Request__c cr : owner.get(u.id)){
        
        cr.Assigned_To__c = u.Id;
        }
    }
    
}

 

Now this trigger works as expected. With this in mind I wanted to create a similar trigger that runs 'before insert' only. I copied the above code to a new trigger (below) on the same object. However this time I wish to map the CreatedById field to a custom User Lookup field. 

The new trigger however doesn't work, but I have no idea why... 

 
trigger onChangeReqBeforeInsert on Change_Request__c (before insert) {

    Set<Id> createdbyIds = new Set<Id>();
    Map<Id, List<Change_Request__c>> createdby = new Map<Id, List<Change_Request__c>>();

        for(Change_Request__c cr : trigger.new){
            createdbyIds.add(cr.CreatedById);
            List<Change_Request__c> crq = new List<Change_Request__c>();

            if(createdby.containsKey(cr.CreatedById)) crq = createdby.get(cr.CreatedById);
               crq.add(cr);
               createdby.put(cr.CreatedById, crq);
            } 

        for(User u : [SELECT Id FROM User WHERE Id IN :createdbyIds]){
        for(Change_Request__c cr : createdby.get(u.id)){
        
        cr.Requested_By__c = u.Id;

        }
    }
    
}

 

I would be most greaftul for some guidance in terms of writing test classes for this. 
Thanks in advance
Ben

 

 

HI, 

 

I'm pretty new to this APEX lark and am not sure if I'm doing this right, so any assistance would be greatly appreciated. 

 

I have written a trigger (adapted from one that I have found online). The triggers purpose is to (before update) map the OwnerId field to a custom User Lookup of my custom object Change_Request__c. 

 

TRIGGER DETAIL

 

 

 
trigger onChangeReqBeforeUpdate on Change_Request__c (before update) {
    
    Set<Id> ownerIds = new Set<Id>();
    Map<Id, List<Change_Request__c>> owner = new Map<Id, List<Change_Request__c>>();
        for(Change_Request__c cr : trigger.new){
            ownerIds.add(cr.OwnerId);
            List<Change_Request__c> crq = new List<Change_Request__c>();
            if(owner.containsKey(cr.OwnerId)) crq = owner.get(cr.OwnerId);
               crq.add(cr);
               owner.put(cr.OwnerId, crq);
            } 
        for(User u : [SELECT Id FROM User WHERE Id IN :ownerIds]){
        for(Change_Request__c cr : owner.get(u.id)){
        
        cr.Assigned_To__c = u.Id;
        }
    }
    
}

 

Now this trigger works as expected. With this in mind I wanted to create a similar trigger that runs 'before insert' only. I copied the above code to a new trigger (below) on the same object. However this time I wish to map the CreatedById field to a custom User Lookup field. 

The new trigger however doesn't work, but I have no idea why... 

 
trigger onChangeReqBeforeInsert on Change_Request__c (before insert) {

    Set<Id> createdbyIds = new Set<Id>();
    Map<Id, List<Change_Request__c>> createdby = new Map<Id, List<Change_Request__c>>();

        for(Change_Request__c cr : trigger.new){
            createdbyIds.add(cr.CreatedById);
            List<Change_Request__c> crq = new List<Change_Request__c>();

            if(createdby.containsKey(cr.CreatedById)) crq = createdby.get(cr.CreatedById);
               crq.add(cr);
               createdby.put(cr.CreatedById, crq);
            } 

        for(User u : [SELECT Id FROM User WHERE Id IN :createdbyIds]){
        for(Change_Request__c cr : createdby.get(u.id)){
        
        cr.Requested_By__c = u.Id;

        }
    }
    
}

 

I would be most greaftul for some guidance in terms of writing test classes for this. 
Thanks in advance
Ben