• Adam Lee 60
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi all.

I am very new to code so can someone please give me some advice?

I am trying to create trigger for when a case is created.

This is what I want to do:

if email address is on a lead, then populate the 'Lead' field. otherwise just leave the Lead field blank and save.
trigger UpdateLeadOnCase on Case (before update)
 {
    public id leadID;
    public id caseId;
    public string caseEmail;
    
    Id caseRT1 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound MTB Emails').getRecordTypeId(); 
    Id caseRT2 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound Sponsors').getRecordTypeId(); 
    Id caseRT3 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound Summits Del EU').getRecordTypeId(); 
    Id caseRT4 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound Summits Del US').getRecordTypeId(); 
    
    for(Case ca: Trigger.New)
    { 
        try
        {
          if(ca.SuppliedEmail!=null)
          {
            Lead L = [Select ID,Name from Lead where Email =: ca.SuppliedEmail limit 1];
            ca.Lead__c=l.Id;
          }
         else
         {
          //ca.Lead__c='';
         }
        }
        catch(Exception ex)
        {
          ca.addError('No email address is found');

        }
       
    }
    

}

I've put an exception in at the moment so I can save the trigger but this needs replacing,

Hope you can help.

Thanks in advance
HI All.

I'm only getting 70% coverage and I'm not sure how to cover the variables.

These are the lines not covered.

     Line 16 qty = o.Total_Roundtables__c;
            17 orderId = o.Id;
            18 eventId = o.Event__c;
            19 eventName = o.Event__r.Name;

            31 r=new Roundtables__c (Order__c=orderId, Status__c ='Unallocated', Event__c = eventId);
            32 rList.add(r);

Can anyone help?

Here's my trigger
trigger CreateRoundtables on Order (after insert, after update) {

    public double qty;
    public id orderId;
    public id eventId;
    public string eventName;


    
    Id orderRecType = Schema.SObjectType.Order.getRecordTypeInfosByName().get('Order').getRecordTypeId(); 
    
    List <OrderItem> oi = [Select id, Roundtables__c from OrderItem where Roundtables__c>0 ];
    system.debug(oi);
    for(order o:trigger.new){
        if(o.BU__c=='MTB' && o.Total_Roundtables__c >= 1 && O.Attendee_Type__c =='Sponsor' && o.Status == 'Approved'){
         	qty = o.Total_Roundtables__c;
            orderId = o.Id;
            eventId = o.Event__c;
            eventName = o.Event__r.Name;
            system.debug('Order ID '+ orderId);
            system.debug('Event Name '+ eventName);
        }
    }
    
    List <Roundtables__c> curRT = [Select id, Order__c from Roundtables__c where Order__c =: orderId];
    List <Roundtables__c> rList = new list <Roundtables__c>();
    Roundtables__c r;
    
    if(curRT.size() <> qty){
    for(Integer m=1;m<=qty;m++){
        r=new Roundtables__c (Order__c=orderId, Status__c ='Unallocated', Event__c = eventId);
        rList.add(r);
    }
    }
	insert rList;
}

Here's the test class
@istest (SeeAllData = true) 
public class CreateRoundTableTEST {
	
    static testMethod void TESTcreateRoundTables(){
        
        //Insert Test Accounts
		Account acc = new Account();
        acc.Type='Other';
        acc.name='test ';
        acc.NumberOfEmployees=10;
        acc.BillingStreet='BillingStreet'; 
        acc.BillingCity = 'BillingCity';
        acc.BillingState = 'BillingState';
        acc.BillingCountry = 'BillingCountry'; 
        acc.BillingPostalCode = 'BillingPostalCode';
        acc.NumberOfEmployees = 50; 
        insert acc;
        
        Account acc2 = new Account();
        acc2.Type='AX Company';
        acc2.name='test 1';
        acc2.BillingStreet='BillingStreet1'; 
        acc2.BillingCity = 'BillingCity';
        acc2.BillingState = 'BillingState';
        acc2.BillingCountry = 'BillingCountry'; 
        acc2.BillingPostalCode = 'BillingPostalCode';
        acc2.NumberOfEmployees = 50; 
        insert acc2;
   
        Project__c testProject = new Project__c();
        testProject.name = 'test Project';
        testProject.Project_Code__c = 'SUM123';
        insert testProject;
   
        //Insert Test Event
        Event__c testEvent = new Event__c();
        testEvent.name='testEvent';
        testEvent.Start_Date__c =system.today();
        testEvent.End_Date__c=system.today().addDays(2);
        testEvent.name='testEvent';
        testEvent.AX_Company__c = acc2.Id;
        testEvent.project__c = testProject.Id;
        Insert testEvent;
        
        //Insert Test Contacts
        Contact con = new contact();
        con.lastname='test';
        con.accountid=acc.id;
        con.FirstName = 'FirstName';
        con.MailingStreet = 'MailingStreet'; 
        con.MailingCity = 'MailingCity';
        con.MailingState = 'MailingState';
        con.MailingCountry = 'MailingCountry';
        con.MailingPostalCode = 'MailingPostalCode';  
        con.IsOpen4All__c=false;      
        insert con;
        
        Contact con2 = new contact();
        con2.lastname='test';
        con2.accountid=acc.id;
        con2.FirstName = 'FirstName';
        con2.MailingStreet = 'MailingStreet'; 
        con2.MailingCity = 'MailingCity';
        con2.MailingState = 'MailingState';
        con2.MailingCountry = 'MailingCountry';
        con2.MailingPostalCode = 'MailingPostalCode';  
        con2.IsOpen4All__c=false;      
        insert con2;
        
        //insert Test Campaign
        Campaign testCampaign1       = new Campaign();
        testCampaign1.name           = 'TestCampaign';
        testCampaign1.Member_Type__c = 'Delegate';
        testCampaign1.Type = 'Call List';
        insert testCampaign1;

       test.startTest();
         Id standardPriceBookId = Test.getStandardPricebookId();
        order o1 = new order();
        o1.accountid=acc.id;
        o1.EffectiveDate= system.today();
        o1.Attendee_Type__c='Sponsor';
        o1.pricebook2id=standardPriceBookId;
        o1.Primary_Campaign_Source__c=testCampaign1.id;
        o1.Price_List__c='test';
        o1.Status='Draft';
        o1.Sold_On_Date__c= system.today();
        o1.Main_POC__c=con2.id;
        o1.Event__c = testEvent.id;
        insert o1;
        
         Id dealOrderId = [Select Id, DeveloperName FROM RecordType where SobjectType = 'Order' and DeveloperName = 'Deal' limit 1].Id;

        o1.recordtypeid=dealOrderId;
        update o1;

        Product2 pd = new Product2(Name='MTB Prod',isActive=true, Number_of_Roundtables__c =2);
        pd.IsActive = true;
        insert pd;
        
        //Id standardPriceBookId = Test.getStandardPricebookId();
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
        insert pbe;
        
        OrderItem ordPd = new OrderItem(PriceBookEntryId=pbe.Id, OrderId=o1.Id, Quantity=1, UnitPrice=99);
        insert ordPd;
        o1.status='Approved';
        update o1;
        
        
       test.stopTest();
    }
}


 
HI All.

I'm only getting 70% coverage and I'm not sure how to cover the variables.

These are the lines not covered.

     Line 16 qty = o.Total_Roundtables__c;
            17 orderId = o.Id;
            18 eventId = o.Event__c;
            19 eventName = o.Event__r.Name;

            31 r=new Roundtables__c (Order__c=orderId, Status__c ='Unallocated', Event__c = eventId);
            32 rList.add(r);

Can anyone help?

Here's my trigger
trigger CreateRoundtables on Order (after insert, after update) {

    public double qty;
    public id orderId;
    public id eventId;
    public string eventName;


    
    Id orderRecType = Schema.SObjectType.Order.getRecordTypeInfosByName().get('Order').getRecordTypeId(); 
    
    List <OrderItem> oi = [Select id, Roundtables__c from OrderItem where Roundtables__c>0 ];
    system.debug(oi);
    for(order o:trigger.new){
        if(o.BU__c=='MTB' && o.Total_Roundtables__c >= 1 && O.Attendee_Type__c =='Sponsor' && o.Status == 'Approved'){
         	qty = o.Total_Roundtables__c;
            orderId = o.Id;
            eventId = o.Event__c;
            eventName = o.Event__r.Name;
            system.debug('Order ID '+ orderId);
            system.debug('Event Name '+ eventName);
        }
    }
    
    List <Roundtables__c> curRT = [Select id, Order__c from Roundtables__c where Order__c =: orderId];
    List <Roundtables__c> rList = new list <Roundtables__c>();
    Roundtables__c r;
    
    if(curRT.size() <> qty){
    for(Integer m=1;m<=qty;m++){
        r=new Roundtables__c (Order__c=orderId, Status__c ='Unallocated', Event__c = eventId);
        rList.add(r);
    }
    }
	insert rList;
}

Here's the test class
@istest (SeeAllData = true) 
public class CreateRoundTableTEST {
	
    static testMethod void TESTcreateRoundTables(){
        
        //Insert Test Accounts
		Account acc = new Account();
        acc.Type='Other';
        acc.name='test ';
        acc.NumberOfEmployees=10;
        acc.BillingStreet='BillingStreet'; 
        acc.BillingCity = 'BillingCity';
        acc.BillingState = 'BillingState';
        acc.BillingCountry = 'BillingCountry'; 
        acc.BillingPostalCode = 'BillingPostalCode';
        acc.NumberOfEmployees = 50; 
        insert acc;
        
        Account acc2 = new Account();
        acc2.Type='AX Company';
        acc2.name='test 1';
        acc2.BillingStreet='BillingStreet1'; 
        acc2.BillingCity = 'BillingCity';
        acc2.BillingState = 'BillingState';
        acc2.BillingCountry = 'BillingCountry'; 
        acc2.BillingPostalCode = 'BillingPostalCode';
        acc2.NumberOfEmployees = 50; 
        insert acc2;
   
        Project__c testProject = new Project__c();
        testProject.name = 'test Project';
        testProject.Project_Code__c = 'SUM123';
        insert testProject;
   
        //Insert Test Event
        Event__c testEvent = new Event__c();
        testEvent.name='testEvent';
        testEvent.Start_Date__c =system.today();
        testEvent.End_Date__c=system.today().addDays(2);
        testEvent.name='testEvent';
        testEvent.AX_Company__c = acc2.Id;
        testEvent.project__c = testProject.Id;
        Insert testEvent;
        
        //Insert Test Contacts
        Contact con = new contact();
        con.lastname='test';
        con.accountid=acc.id;
        con.FirstName = 'FirstName';
        con.MailingStreet = 'MailingStreet'; 
        con.MailingCity = 'MailingCity';
        con.MailingState = 'MailingState';
        con.MailingCountry = 'MailingCountry';
        con.MailingPostalCode = 'MailingPostalCode';  
        con.IsOpen4All__c=false;      
        insert con;
        
        Contact con2 = new contact();
        con2.lastname='test';
        con2.accountid=acc.id;
        con2.FirstName = 'FirstName';
        con2.MailingStreet = 'MailingStreet'; 
        con2.MailingCity = 'MailingCity';
        con2.MailingState = 'MailingState';
        con2.MailingCountry = 'MailingCountry';
        con2.MailingPostalCode = 'MailingPostalCode';  
        con2.IsOpen4All__c=false;      
        insert con2;
        
        //insert Test Campaign
        Campaign testCampaign1       = new Campaign();
        testCampaign1.name           = 'TestCampaign';
        testCampaign1.Member_Type__c = 'Delegate';
        testCampaign1.Type = 'Call List';
        insert testCampaign1;

       test.startTest();
         Id standardPriceBookId = Test.getStandardPricebookId();
        order o1 = new order();
        o1.accountid=acc.id;
        o1.EffectiveDate= system.today();
        o1.Attendee_Type__c='Sponsor';
        o1.pricebook2id=standardPriceBookId;
        o1.Primary_Campaign_Source__c=testCampaign1.id;
        o1.Price_List__c='test';
        o1.Status='Draft';
        o1.Sold_On_Date__c= system.today();
        o1.Main_POC__c=con2.id;
        o1.Event__c = testEvent.id;
        insert o1;
        
         Id dealOrderId = [Select Id, DeveloperName FROM RecordType where SobjectType = 'Order' and DeveloperName = 'Deal' limit 1].Id;

        o1.recordtypeid=dealOrderId;
        update o1;

        Product2 pd = new Product2(Name='MTB Prod',isActive=true, Number_of_Roundtables__c =2);
        pd.IsActive = true;
        insert pd;
        
        //Id standardPriceBookId = Test.getStandardPricebookId();
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
        insert pbe;
        
        OrderItem ordPd = new OrderItem(PriceBookEntryId=pbe.Id, OrderId=o1.Id, Quantity=1, UnitPrice=99);
        insert ordPd;
        o1.status='Approved';
        update o1;
        
        
       test.stopTest();
    }
}