• ShayneO
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 5
    Replies
Hello - I'm fairly new to Apex and am hoping I could get some help/examples of how to properly query data from related objects inside of a trigger.

Essentially, my scenario is as such:

Objective - Whenever a ChildObject__c is created, I'd like to automatically create a new JunctionObect__c for each contact lookup field that is populated on ParentObject__c, and for each  related Contact I'd also like to create a new JunctionObject__c for that Contact's Account.

my code looks like this:

trigger createJuntion on ChidObject__c (after insert) {
    List<JunctionObject__c> insertList = new List<JunctionObject__c>();
   
    for(ChildObject__c co : Trigger.new){
        if (co.Contact__c != null ) {
            JunctionObject__c jo = new JunctionObject__c();
            jo.Contact__c = co.Contact__c;
            jo.Company__c = co.Contact__r.AccountID;

            insertList.add(rnContact);
        }
       insert inserList;
}


There are no errors thrown, but the Company field is not populated on the junction object.  I understand that I probably need to query the Contact's accountID through an alternative means, but would like some direction as to how.

Thanks!!!

I'd like to build a trigger that pulls that updates a text field on a record with the report ID of a particular report.  I'm not sure how to go about obtaining the report ID.

Need help with a Apex Sharing based on a User lookup.  I've followed the basic salesforce documentation, but need to be able to handle updates (if the user changes) and if the user lookup field is blank.

 

Right now the trigger works, but I get the following error when the Marketing_Coordinator__c field is left blank:

Error: Invalid Data. 
Review all error messages below to correct your data.
Unable to grant sharing access due to following exception: Required fields are missing: [User/Group]

 

Any input would be appreciated.

 

trigger TransactionApexSharing on Commdev3__Transaction__c (after insert) {
    
    if(trigger.isInsert){
        // Create a new list of sharing objects for Job
        List<Commdev3__Transaction__Share> ts  = new List<Commdev3__Transaction__Share>();
        
        // Declare variables for recruiting and hiring manager sharing
        Commdev3__Transaction__Share mktc;
        //Commdev3__Transcation__Share hmShr;
        
        for(Commdev3__Transaction__c t : trigger.new){
            // Instantiate the sharing objects
            mktc = new Commdev3__Transaction__Share();
           // hmShr = new Job__Share();
            
            // Set the ID of record being shared
            mktc.ParentId = t.Id;
            //hmShr.ParentId = job.Id;
            
            // Set the ID of user or group being granted access
            mktc.UserOrGroupId = t.Marketing_Coordinator__c;
            //hmShr.UserOrGroupId = job.Hiring_Manager__c;
            
            // Set the access level
            mktc.AccessLevel = 'edit';
            //hmShr.AccessLevel = 'read';
            
            // Set the Apex sharing reason for hiring manager and recruiter
            mktc.RowCause = Schema.Commdev3__Transaction__Share.RowCause.Marketing_Coordinator__c;
            //hmShr.RowCause = Schema.Job__Share.RowCause.Hiring_Manager__c;
            
            // Add objects to list for insert
            ts.add(mktc);
            //jobShrs.add(hmShr);
        }
        
        // Insert sharing records and capture save result 
        // The false parameter allows for partial processing if multiple records are passed 
        // into the operation 
        Database.SaveResult[] lsr = Database.insert(ts,false);
        
        // Create counter
        Integer i=0;
        
        // Process the save results
        for(Database.SaveResult sr : lsr){
            if(!sr.isSuccess()){
                // Get the first save result error
                Database.Error err = sr.getErrors()[0];
                
                // Check if the error is related to a trivial access level
                // Access levels equal or more permissive than the object's default 
                // access level are not allowed. 
                // These sharing records are not required and thus an insert exception is 
                // acceptable. 
                if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
                                               &&  err.getMessage().contains('AccessLevel'))){
                    // Throw an error when the error is not related to trivial access level.
                    trigger.newMap.get(ts[i].ParentId).
                      addError(
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());
                }
            }
            i++;
        }   
    }
    
}

 

 

 

 

 

  • September 29, 2013
  • Like
  • 0

I'd like to implenent a tool that allows a user to build a list of Contact records to relate to Custom_Object__c through Junction_Object__c.  

 

Essentially, the workflow should be:

  1. Create new Custom_Object__c record.
  2. Select list of contacts to relate (much like campaigns or mass email function)
  3. Automatically create Junction_Object__c records for related to Custom_Object__c record and each selected Contact record.

Would love to know if there is an unmanaged package to handle this.

 

I appreciate any input!

 

Is it possible to export Chatter Likes? I know these are stored as FeedLike but I cannout access this object with the dataloader.

 

Thoughts? 

Is it possible to move a chatter feed/chatter data into a new org?

I'm attempting to write a trigger to create a new custom object record, when a Contact record meets specific criteria.  The problem is, the trigger is creating 2 records instead of 1.

 

Code below:

trigger createPreferenceWeb on Contact (before update) {
    
    List <rethink3__Requirements__c> prefToInsert = new List <rethink3__Requirements__c> ();
   
    
    for (Contact c : Trigger.new) {

        if (c.Web_To_Lead__c = true){  
        
        rethink3__Requirements__c p = new rethink3__Requirements__c ();
        
        p.rethink3__Contact__c = c.Name;
        p.rethink3__Contact__c = c.Id;
        p.Down_Payment_Available__c = c.Down_Payment_Available__c;
        p.Maximum_Price__c = c.Maximum_Price__c;

        
        prefToInsert.add(p);
        
        
        }
        
    }
    try {
        insert prefToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
    
}

 

Is it possible to export Chatter Likes? I know these are stored as FeedLike but I cannout access this object with the dataloader.

 

Thoughts? 

I'm attempting to write a trigger to create a new custom object record, when a Contact record meets specific criteria.  The problem is, the trigger is creating 2 records instead of 1.

 

Code below:

trigger createPreferenceWeb on Contact (before update) {
    
    List <rethink3__Requirements__c> prefToInsert = new List <rethink3__Requirements__c> ();
   
    
    for (Contact c : Trigger.new) {

        if (c.Web_To_Lead__c = true){  
        
        rethink3__Requirements__c p = new rethink3__Requirements__c ();
        
        p.rethink3__Contact__c = c.Name;
        p.rethink3__Contact__c = c.Id;
        p.Down_Payment_Available__c = c.Down_Payment_Available__c;
        p.Maximum_Price__c = c.Maximum_Price__c;

        
        prefToInsert.add(p);
        
        
        }
        
    }
    try {
        insert prefToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
    
}