• Coco_Sydney
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies

I'm stuck on this test class, I don't think I am consrtucting it correctly as I am incorrectly refrecing the object. This is my first time writing a test class for an extension class.

System.NullPointerException: Attempt to de-reference a null object - Class.testTotalGPbyDiv.testTotalGPbyDiv: line 4, column 1 External entry point
 

 

 

public class testTotalGPbyDiv {
static testMethod void testTotalGPbyDiv(){
Division__c div;
div.name = 'Hampton Test';
insert div;

ApexPages.currentPage().getParameters().put('id', div.Id);
ApexPages.StandardController std=new ApexPages.StandardController(div);

TotalGPbyDiv controller=new TotalGPbyDiv(std);

}
}

 

 


I'm really lost right now on how to get this to complie. Any thoughts?

 

_chris

  • March 17, 2011
  • Like
  • 0

Please help I am not a developer but am trying to learn.  After taking code samples from the board and manuals I was able to get this apex class do what I wanted but then I ran into the issue of unit test.  I had a hard enough time figuring out how to get the class to work.  I feel like I am banging my head against a wall with the unit test process it just does not make sense to me.  But I have figured out how to get 80% coverage but it is failing and I am not sure how to fix it.  Any help would be appreciated.

Thanks

Eric

 

Message: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target object ids (contact, lead or user): []

stack trace: Class.MNEmail2.send: line 56, column 1 Class.MNEmail2.testMNEmail2: line 89, column 32 External entry point

 

 

public class MNEmail2 {
public String Template = [select MN_Email_Template__c from Case where id = :ApexPages.currentPage().getParameters().get('id') limit 1].MN_Email_Template__c;
public list <Contact> Test;
Contact[] contactList = new Contact[]{};
Id[] targetObjectIds = new Id[] {};
Id[] whatids = new Id[] {}; 
public id mn = [select ID from Case where id = :ApexPages.currentPage().getParameters().get('id')].id; 
public String custaff = [select MN_Customers_Affected__c from Case where id = :ApexPages.currentPage().getParameters().get('id') limit 1].MN_Customers_Affected__c;
String[] actnames = new List<String>();

public MNEmail2() {
actnames = custaff.split(';');
//System.debug ('***** List: ' + actnames + ', size: ' + actnames.size ());
         //Loop thrugh the whole ist of contacts an their emails
          for (Contact c : [Select account.name, name, email from Contact where Contact.Account.name = :actnames and Receive_support_notifications__c = True]) {
            targetObjectIds.add(c.Id);
            contactlist.add(c);
            whatIds.add(mn);
           }
}

//public Account getAccount() {
//return account;
//}
public string getTemplate() {
    return Template;
}
public id getmn() {
    return mn;
} 
 public list<Contact> getcontacts(){ 
        return ContactList;  
}


public PageReference cancel() {
    PageReference pageRef = new PageReference('/'+ mn);
    pageRef.setRedirect(true);
    return pageRef;
}

public PageReference send() {
// Define the email
Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();
// Sets the paramaters of the email
email.setTargetObjectIds (targetObjectIds);
email.setWhatIds (whatids);
email.setTemplateId ([select id from EmailTemplate where Name = :template].id);
email.setreplyto ('noreply@demandtec.com');
email.setSenderDisplayName ('DemandTec Support No-Reply');
//email.setSubject( subject );
//email.setToAddresses( toAddresses );
//email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.MassEmailMessage[] {email});
PageReference pageRef = new PageReference('/'+ mn);
    pageRef.setRedirect(true);
    return pageRef;
return null;
}
static testMethod void testMNEmail2(){
   
        //make test data
        Account a = new Account(Name = 'Testing');
        Insert a;
        Contact con = new Contact(Firstname = 'testing',lastname = 'test',email = 'eric.wc@gmail.com',Receive_support_notifications__c = True,Account = a);
        Insert con;
        Case c = new Case(Subject = 'Test case',MN_Email_Template__c = 'Scheduled Maintenance',MN_Customers_Affected__c = 'Testing');
        Insert c;
       
        CaseComment cmt1 = new CaseComment(
            ParentId = c.id,
            IsPublished = false,
            CommentBody = 'A private comment from a user.'
        );
        CaseComment cmt2 = new CaseComment(
            ParentId = c.id,
            IsPublished = true,
            CommentBody = 'A public comment from a client.'
        );
        Insert cmt1;
        Insert cmt2;
       
         // Initiate class createOpsTicketClass with reference to created Support Case
             System.currentPageReference().getParameters().put('id', c.id);

             MNemail2 ee = new MNemail2();
             String nextPage = ee.send().getUrl();
             nextPage = ee.cancel().getUrl();
             System.assert(ee.mn == c.id);
}

}

 

 

 

Hello,

 

I have developed a trigger and tested successfully in the sandbox environment. Now I would like to deploy into production however, I ran into some issues providing the sufficient trigger code coverage needed in order to promote. I tried creating a public class where a static testmethod for the trigger is defined, but although the class saves without errors, my trigger is still unable to be promoted. Any help in writing the correct testmethod for my trigger would be greatly appreciated. Thanks in advance.

 

--Here is my trigger:

Trigger ClientHandOffUpdate on Client_Hand_Off__c(after insert, after update)
{
Client_Hand_Off__c[] CHOs = Trigger.new;
 
     for(Client_Hand_Off__c CHO:CHOs){

        Contract_Order_Line_Item__c[] updateCOLI = [SELECT Name FROM Contract_Order_Line_Item__c LIMIT 100];
            if (updateCOLI.size()>0)
            {
                for(Integer i = 0; i < updateCOLI.size(); i++)
                {
                    updateCOLI[i].Client_Hand_Off__c = null;
                    update updateCOLI;
                }
            } 
    }
 
    for(Client_Hand_Off__c CHO:CHOs){
 
        if (CHO.Contract_Order__c!= null){
 
        string ContactOrder = CHO.Contract_Order__c;

        Contract_Order_Line_Item__c[] updateCOLI = [SELECT Name FROM Contract_Order_Line_Item__c WHERE Contract_Order__r.Id = :ContactOrder LIMIT 100];
            if (updateCOLI.size()>0)
            {
                for(Integer i = 0; i < updateCOLI.size(); i++)
                {
                    updateCOLI[i].Client_Hand_Off__c = CHO.Id;
                    update updateCOLI;
                }
            }
        }   
    }
}

 

 

--Here is my Attempted Testmethod:

 

public with sharing class CHOtriggerUnitTest {
  
  public static testmethod void testClientHandOffTrigger()
    {
    
          Account acct = new Account(name='test account', Type='Prospect');
          insert acct;
          
          Contract_Order__c CO = new Contract_Order__c(Account__c=acct.Id,State_Fips__c = '01 AL');
          insert CO;
           
          Client_Hand_Off__c[] CHOstoCreate = new Client_Hand_Off__c[]{};
          for(Integer x=0; x<200;x++){
              Client_Hand_Off__c CHOS = new Client_Hand_Off__c(Client_Name__c=acct.Id,Contract_Order__c=CO.Id,Client_Personality__C ='Test Client Hand Off Form');
              insert CHOstoCreate;
          }
           
   }   

}

 

Note : Following is a small development being carried out to demystify salesforce apex webservice and callout features, please correct me if you find my understanding to be incorrect.

 

I am trying to integrate two Salesforce Orgs using Apex. Apex supports both callouts and webservices, then why not leverage both to achieve a 360 connectivity between two Salesforce instances.

 

Org1 : Create a Apex Web Service, Generate its WSDL

global class AccountPlan {

webservice String area;
webservice String region;

//Define an object in apex that is exposed in apex web service
global class Plan {
webservice String name;
webservice Integer planNumber;
webservice Date planningPeriod;
webservice Id planId;
}

webservice static Plan createAccountPlan(Plan vPlan) {

//A plan maps to the Account object in salesforce.com.
//So need to map the Plan class object to Account standard object
Account acct = new Account();
acct.Name = vPlan.name;
acct.AccountNumber = String.valueOf(vPlan.planNumber);
insert acct;

vPlan.planId=acct.Id;
return vPlan;
}

}

 

Org2:  Consume above generated WSDL to generate following Apex Class viz.,AccountPlanClasses

public class AccountPlanClasses {
public class LogInfo {
public String category;
public String level;
private String[] category_type_info = new String[]{'category','http://soap.sforce.com/schemas/class/TCSSAAS/AccountPlan','LogCategory','1','1','false'};
private String[] level_type_info = new String[]{'level','http://soap.sforce.com/schemas/class/TCSSAAS/AccountPlan','LogCategoryLevel','1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/TCSSAAS/AccountPlan','true','false'};
private String[] field_order_type_info = new String[]{'category','level'};
}
public class AllowFieldTruncationHeader_element {
............................
............................

 

Now lets try to connect to Org1 from Org2

AccountPlanClasses.Plan plan = new AccountPlanClasses.Plan();
plan.name = 'Chirag';
plan.planNumber = 111;
AccountPlanClasses.AccountPlan a = new AccountPlanClasses.AccountPlan();
a.createAccountPlan(plan);

 

Execution of above code runs into following error

WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor= 

 

I to feel that somewhere I am missing User Credentials to be supplied, but I dont know exactly where. Please guide me to complete this 360 connectivity between two orgs using Apex.

Message Edited by Chirag Mehta on 05-05-2009 06:13 AM
Message Edited by Chirag Mehta on 05-12-2009 07:52 PM