• Rick Haag
  • NEWBIE
  • 30 Points
  • Member since 2014
  • PM/BA/SFDC Admin/Analyst
  • Blue Coat Systems, Inc.


  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 6
    Replies
Hi All, 

I am very new to writing triggers and need some help on this one. On the Opportunity object, there is a custom checkbox field called "Invoice Attached". When an Invoice is uploaded to that Opp, I would like the checkbox will be marked. I have attached my Trigger and Test Class. Any help would be greatly appreciated. User-added imageUser-added image
Hello,
I am attempting to write a test class for a trigger where I am leveraging Custom Metadata to Activate/Deactivate the trigger.  I am not able to generate code coverage for both scenarios as I cannot insert test data for Custom Metadata.

In the past I used Custom Settings for trigger activation/deactivation and that it was simple enough to insert to test the "if" and the "other".  I understand Custom Metadata is the future, so here I am with incomplete code coverage.

Any recommendations on how to do this is appreciated. I have posted the test class below and added the bit for Custom Metadata, but as mentioned it isn't valid, but added to provide a sense of what I am attempting to do.

Trigger
trigger OpportunityContactRole on OpportunityContactRole (after insert, after update, after delete) {

    
    //Checks if Custom Metadata on for Trigger is Active
    List<Trigger__mdt> metadataList =  new List<Trigger__mdt>();
    metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c != NULL AND Label = 'OpportunityContactRole'];
    
    if(metadataList.size()==0) 
    {
        return;
    }
    
    for(OpportunityContactRole ocr : Trigger.new==null ? Trigger.old : Trigger.new)
        
    {
            ------Run Stuff Here----
    }
    
}

Test Class
@isTest
private class OpportunityContactRoleTest
{

    @isTest
    static void triggerOn()
    {              
        Account acc = new Account(name='test acc');
        insert acc;
        Contact con = new Contact(lastname='test con',accountid=acc.id);
        insert con;
        Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today());
        insert opp;

        //This is will work for Custom Settings but not for Custom Metadata
        Trigger_mdt  setting = new TriggerManager__mdt();
	setting.Label = 'OpportunityContactRole';
        setting.isActive__c = TRUE;
	insert setting;
        
        //Insert OpportunityContactRole
	OpportunityContactRole ocr = new OpportunityContactRole();
	ocr.ContactId = con.Id;
	ocr.OpportunityId = opp.Id;
	ocr.IsPrimary = TRUE;
	ocr.Role = 'Decision Maker';
        test.starttest();
        insert ocr;
        
        test.stoptest();
        
    }


    @isTest
    static void triggerOff()
    {             
        Account acc = new Account(name='test acc');
        insert acc;
        Contact con = new Contact(lastname='test con',accountid=acc.id);
        insert con;
        Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today());
        insert opp;
     
        //Insert OpportunityContactRole
	OpportunityContactRole ocr = new OpportunityContactRole();
	ocr.ContactId = con.Id;
	ocr.OpportunityId = opp.Id;
	ocr.IsPrimary = TRUE;
	ocr.Role = 'Decision Maker';
        test.starttest();
        insert ocr;
        
        test.stoptest();
        
    }
    
    
}


 
Hello,
I am attempting to write a test class for a trigger where I am leveraging Custom Metadata to Activate/Deactivate the trigger.  I am not able to generate code coverage for both scenarios as I cannot insert test data for Custom Metadata.

In the past I used Custom Settings for trigger activation/deactivation and that it was simple enough to insert to test the "if" and the "other".  I understand Custom Metadata is the future, so here I am with incomplete code coverage.

Any recommendations on how to do this is appreciated. I have posted the test class below and added the bit for Custom Metadata, but as mentioned it isn't valid, but added to provide a sense of what I am attempting to do.

Trigger
trigger OpportunityContactRole on OpportunityContactRole (after insert, after update, after delete) {

    
    //Checks if Custom Metadata on for Trigger is Active
    List<Trigger__mdt> metadataList =  new List<Trigger__mdt>();
    metadataList = [SELECT Id FROM Trigger__mdt WHERE isActive__c != NULL AND Label = 'OpportunityContactRole'];
    
    if(metadataList.size()==0) 
    {
        return;
    }
    
    for(OpportunityContactRole ocr : Trigger.new==null ? Trigger.old : Trigger.new)
        
    {
            ------Run Stuff Here----
    }
    
}

Test Class
@isTest
private class OpportunityContactRoleTest
{

    @isTest
    static void triggerOn()
    {              
        Account acc = new Account(name='test acc');
        insert acc;
        Contact con = new Contact(lastname='test con',accountid=acc.id);
        insert con;
        Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today());
        insert opp;

        //This is will work for Custom Settings but not for Custom Metadata
        Trigger_mdt  setting = new TriggerManager__mdt();
	setting.Label = 'OpportunityContactRole';
        setting.isActive__c = TRUE;
	insert setting;
        
        //Insert OpportunityContactRole
	OpportunityContactRole ocr = new OpportunityContactRole();
	ocr.ContactId = con.Id;
	ocr.OpportunityId = opp.Id;
	ocr.IsPrimary = TRUE;
	ocr.Role = 'Decision Maker';
        test.starttest();
        insert ocr;
        
        test.stoptest();
        
    }


    @isTest
    static void triggerOff()
    {             
        Account acc = new Account(name='test acc');
        insert acc;
        Contact con = new Contact(lastname='test con',accountid=acc.id);
        insert con;
        Opportunity opp = new Opportunity(AccountId=acc.id,Amount=1,Name = 'test opp',StageName = 'Closed Lost',CloseDate=Date.today());
        insert opp;
     
        //Insert OpportunityContactRole
	OpportunityContactRole ocr = new OpportunityContactRole();
	ocr.ContactId = con.Id;
	ocr.OpportunityId = opp.Id;
	ocr.IsPrimary = TRUE;
	ocr.Role = 'Decision Maker';
        test.starttest();
        insert ocr;
        
        test.stoptest();
        
    }
    
    
}


 
Hi All, 

I am very new to writing triggers and need some help on this one. On the Opportunity object, there is a custom checkbox field called "Invoice Attached". When an Invoice is uploaded to that Opp, I would like the checkbox will be marked. I have attached my Trigger and Test Class. Any help would be greatly appreciated. User-added imageUser-added image
Hi everyone,

Here is my business requirement:

On the standard Quote object, I have a custom checkbox field called PO_Attached__c. If a File is uploaded to a Quote record, PO_Attached__c should be updated to true. I currently achieve this functionality using the Attachments functionality by use of an Apex trigger.

However, we are now migrating to Files. I have updated my code block as below. It saves fine, but the checkbox is never updated. Any ideas why? Thanks in advance.
 
trigger quoteAttachment2 on ContentDocumentLink (after insert) {

                        List<Quote> listOfQuote = [select id from Quote where id =: Trigger.New[0].LinkedEntityId];
                        if(listOfQuote.size()>0)
                        {
                                    listOfQuote[0].PO_Attached__c = true;
                                    update listOfQuote;
                        }                                                                   
            }

 
Hi,

I have portal users, chatter users and salesforce users.

When a user is created, I want a trigger that will check if the user is a portal user and then update the associated contact to show the contact has a portal account.  The contact object has a checkbox called "Portal_User__c" and I have put together the code below but I'm missing the section that updates the Contact field (shown below with the comment).  Can anyone suggest how I would update the contact?



trigger UpdateContactAsPortalUser on User (after insert, after update){

User uid =[select Contact from User where id=:UserInfo.getUserID()];

for (User newUser:Trigger.new){

if(uid.Contact !== null)

// Steps to set "Portal_User__c" field on Contact object to true

update uid;

}


Any assistance is appreciated.