• Mona Weso
  • NEWBIE
  • 10 Points
  • Member since 2016
  • Salesforce Administrator
  • Jewelers Mutual Insurance Group

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

I am trying to pass an opportunity record collection from a flow into an Apex Action that will loop through the opportunity record collection (LIST), then create an opportunity team member record for each opportunity in that list, then return the new list as a new opportunity team member collection that will be an output variable accessable via the flow.  However, I keep getting the following errors that I cannot resolve.  I am new to Apex so I am guessing that my MAP setup is wrong.

 

Illegal assignment from List<Opportunity> to List<OpportunityService1.ReturnVariables>

Illegal assignment from OpportunityTeamMember to OpportunityService1.ReturnVariables

//invocable method to create OpportunityTeamMember records from a list of Opportunity Ids
    @InvocableMethod(label='Create OTM Records for OOO' description='Given a list of Opportunity IDs, create OTM records from these IDs.')
        public static List<ListReturnVariables> createOTMRecords(List<InputVariables> inputVariables) {

            //inputs
            List<Opportunity> opps = inputVariables.get(0).opportunities;
            String varUserId;
            

            //outputs       
            
            Map<Id, Opportunity> opp = new Map<Id, Opportunity>(opps);
            List<ReturnVariables> OTMOutputs =[SELECT Id FROM Opportunity WHERE Id IN: opp.keySet()];
            for(Opportunity o : opp.values()){
            ReturnVariables otm = new OpportunityTeamMember(OpportunityId = o.Id, UserId = varUserId, OpportunityAccessLevel = 'Edit',TeamMemberRole = 'Mortgage Banker');
                OTMOutputs.add(otm);
            }
            return OTMOutputs;
        }
        public class InputVariables {
            @InvocableVariable
            public List <Opportunity> opportunities;

            @InvocableVariable
            public String varUserId;
        }
        public class ReturnVariables {
            @InvocableVariable
            public List <OpportunityTeamMember> OTMOutputs;
        }

I created this trigger to update a related opportunity when an OpportunityContactRole not marked as Primary is created.  I am updating two lookup fields with OpportunityContactRole.ContactId on the opportunity -- if one field (secondaryclient) already contains a value, then the next field (tertiaryclient) should be updated if null.  If both fields on the opportunity already contain a value, then no updates should be made.  It currently functions as expected, but it needs some work to bulkify. How can I do this?

trigger NonPrimaryClientTrigger on OpportunityContactRole (before insert) {

    if (ByPassSettingTriggers__c.getInstance().OpportunityContactRole__c) {        
        return;
    }

    //Adding it to track the start of trigger execution
    System.debug('Start of OpportunityContactRole Trigger, operation type : '+trigger.operationType+' , Start Time: '+System.now()+' , CPU Time Start: '+Limits.getCpuTime());
     
    //Allows the trigger to only execute if the OpportunityContactRole added is nonPrimary
    Set<Id> ids = new Set<Id>();
        for (OpportunityContactRole ocr : Trigger.new){
            if (ocr.IsPrimary == False){
                ids.add(ocr.Id);
                }            
        
    System.debug('OCR Id list size = ' + ids.size());
    if (ids.size() > 0) {
     
    //Get Opportunity Ids of all Opportunities related to nonPrimary OpportunityContactRoles
        List<Id> oppIds = new List<Id>();
            for (OpportunityContactRole ocrs:Trigger.new){                
                    oppIds.add(ocrs.OpportunityId);         
                }

    System.debug('OpportunityID list after OpportunityContactRole Loop' + oppIds);
    
    List<Opportunity> oppList = [SELECT Id, SecondaryClient__c, TertiaryClient__c FROM Opportunity WHERE TertiaryClient__c = Null AND Id IN :oppIds];
       
        for(Opportunity opp :oppList){
    
    //If there is no SecondaryClient, then add the Contact Id to this field
                if (opp.SecondaryClient__c == Null) {
                    opp.SecondaryClient__c = ocr.ContactId;
                }   
    //If there is already a SecondaryClient, then add the Contact Id to the TertiaryClient field     
                    else
                    {
                        if(opp.SecondaryCLient__c != Null && opp.TertiaryClient__c == Null){
                        opp.TertiaryClient__c = ocr.ContactId;
                        }
                    }
                }     
        
        update oppList;    
    }
}
I am receiving a Cover Coverage Failure error (0%) when attempting to deploy my Apex Class, Test Class, and VisualForce Page to production.  My code tested 100% coverage in Sandbox.  The error is on testPageRef method.  The error I am receiving is "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Source__c]: [Source__c] 
Stack Trace: Class.expressControllerTest.testPageRef: line 9, column 1."  Any help would be appreciated!

Here is my Test Class:
@isTest
public class expressControllerTest {

public static testMethod void testPageRef() {

    Account acc = new Account(Name='Abce');
    insert acc;
    Program__c prg = new Program__c(Account__c=acc.Id);
    insert prg;
    Payment_Schedule__c pa = new Payment_Schedule__c();
    // TODO: Populate required Opportunity fields here
    pa.Name='test sfdc';
    pa.Program_Master__c = prg.Id;
    insert pa;
    
    
    PageReference pref = Page.Payment_Schedules; 
    pref.getParameters().put('id', prg.id);
    ApexPages.StandardController sc= new ApexPages.StandardController(prg);
    expressController exp = new expressController(sc);
    System.assert(null == exp.newPayment_Schedule());    
    System.assertNotEquals(null, exp.getheaders());
    
    System.assertNotEquals(null, exp.getpaymentschedules());
    
    System.assertEquals(null, exp.saveChanges());
}
}
Here is my Controller:
public class expressController {
  
  // Constructor
 public expressController(ApexPages.StandardController controller) {
  this.prog = (Program__c)controller.getSubject();
     this.schedules = [ SELECT 
      ps.Date_Due__c, 
      ps.Amount_Due__c, ps.Descriptions__c, ps.Date_From__c, 
      ps.Date_To__c, ps.Paid__c, ps.Id, ps.CreatedById,
      ps.Program_Master__c FROM Payment_Schedule__c ps where ps.program_Master__c = :prog.id ];
 }
 
 // Action Method called from page button
 public pagereference saveChanges() { 
  upsert this.schedules;
  return null;
 }
 
 // Action Method called from page link
 public pagereference newPayment_Schedule() { 
  payment_schedule__c ps = new payment_schedule__c();
  ps.program_master__c =this.prog.id; 
  schedules.add(ps);
  return null;
 }
 
 // public Getter to provide table headers 
 public string[] getheaders() { return new string [] 
  {'Date Due','Amount Due','Descriptions', 'Date From',
   'Date To','Paid'} ; }
 
 // public Getter to list payment schedules
 public payment_schedule__c[] getpaymentschedules() { 
  return this.schedules; 
 } 
 
 // class variables
 Program__c prog;
 payment_schedule__c[] schedules; 
}


 
I am receiving a Cover Coverage Failure error (0%) when attempting to deploy my Apex Class, Test Class, and VisualForce Page to production.  My code tested 100% coverage in Sandbox.  The error is on testPageRef method.  The error I am receiving is "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Source__c]: [Source__c] 
Stack Trace: Class.expressControllerTest.testPageRef: line 9, column 1."  Any help would be appreciated!

Here is my Test Class:
@isTest
public class expressControllerTest {

public static testMethod void testPageRef() {

    Account acc = new Account(Name='Abce');
    insert acc;
    Program__c prg = new Program__c(Account__c=acc.Id);
    insert prg;
    Payment_Schedule__c pa = new Payment_Schedule__c();
    // TODO: Populate required Opportunity fields here
    pa.Name='test sfdc';
    pa.Program_Master__c = prg.Id;
    insert pa;
    
    
    PageReference pref = Page.Payment_Schedules; 
    pref.getParameters().put('id', prg.id);
    ApexPages.StandardController sc= new ApexPages.StandardController(prg);
    expressController exp = new expressController(sc);
    System.assert(null == exp.newPayment_Schedule());    
    System.assertNotEquals(null, exp.getheaders());
    
    System.assertNotEquals(null, exp.getpaymentschedules());
    
    System.assertEquals(null, exp.saveChanges());
}
}
Here is my Controller:
public class expressController {
  
  // Constructor
 public expressController(ApexPages.StandardController controller) {
  this.prog = (Program__c)controller.getSubject();
     this.schedules = [ SELECT 
      ps.Date_Due__c, 
      ps.Amount_Due__c, ps.Descriptions__c, ps.Date_From__c, 
      ps.Date_To__c, ps.Paid__c, ps.Id, ps.CreatedById,
      ps.Program_Master__c FROM Payment_Schedule__c ps where ps.program_Master__c = :prog.id ];
 }
 
 // Action Method called from page button
 public pagereference saveChanges() { 
  upsert this.schedules;
  return null;
 }
 
 // Action Method called from page link
 public pagereference newPayment_Schedule() { 
  payment_schedule__c ps = new payment_schedule__c();
  ps.program_master__c =this.prog.id; 
  schedules.add(ps);
  return null;
 }
 
 // public Getter to provide table headers 
 public string[] getheaders() { return new string [] 
  {'Date Due','Amount Due','Descriptions', 'Date From',
   'Date To','Paid'} ; }
 
 // public Getter to list payment schedules
 public payment_schedule__c[] getpaymentschedules() { 
  return this.schedules; 
 } 
 
 // class variables
 Program__c prog;
 payment_schedule__c[] schedules; 
}