Salesforce.com Apex Test Class issue: Need to hard code Opportunity Custom Field Value - salesforce

I have a VF page form that has a text field. The value of this text field is compared to an existing opportunity custom field. If the value of this text field exists in an opportunity, I go ahead and create a new record.
Everything works out fine except when I create a Apex Test Class.
I need to create an opportunity and hard code the value for the custom field. Unfortuneatly, this field is not writeable.
Has anyone ran into this issue before?
This is the error that I'm getting:
Error: Compile Error: Field is not writeable: Opportunity.Deal_Registration_ID__c at line 9 column 103
Below is my Apex Class.
My test class is at the very bottom.
Any help is greatly appreciated.
Cheers!
Rommel
public with sharing class VaultVF {
public Vault__c vault {get; set;}
public List<String> errorMsgs {get; set;}
public String saveResult {get; set;}
public String dealReg {get; set;}
public String oppId {get; set;}
public String email {get; set;}
public Boolean scenario1{get; set;}
public Boolean scenario2{get; set;}
public Boolean scenario3{get; set;}
// Generates country dropdown from country settings
public List<SelectOption> getCountriesSelectList() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('', '-- Select One --'));
// Find all the countries in the custom setting
Map<String, Country__c> countries = Country__c.getAll();
// Sort them by name
List<String> countryNames = new List<String>();
countryNames.addAll(countries.keySet());
countryNames.sort();
// Create the Select Options.
for (String countryName : countryNames) {
Country__c country = countries.get(countryName);
options.add(new SelectOption(country.Two_Letter_Code__c, country.Name));
}
return options;
}
//Constructor, set defaults
public VaultVF(ApexPages.StandardController controller) {
vault = (vault__c)controller.getRecord();
//Populate list of random characters for CAPTCHA verification
characters = new List<String>{'a','b','c','d','e','f','g','h',
'i','j','k','m','n','p','q','r','s','t','u','v','w',
'x','y','z','1','2','3','4','5','6','7','8','9'
};
errorMsgs = new List<String>();
}
//Determine if page has error so errors message can be displayed
public boolean getHasErrors(){
if(ApexPages.hasMessages() == true){
if(errorMsgs == null){
errorMsgs = new List<String>();
}
//Loop through errors and add to a list
for(ApexPages.Message m : ApexPages.getMessages()){
if(m.getSummary().startsWith('Uh oh.')){
errorMsgs.add(String.valueOf(m.getSummary()));
}
}
}
return ApexPages.hasMessages();
}
public void save(){
errorMsgs = new List<String>();
//Make sure captcha was correct
if(validateCAPTCHA() == false){
errorMsgs.add('Verification code was incorrect.');
captchaInput = '';
}
//Make sure the Scenario is selected
if(scenario1 == true){
vault.Scenario__c = 'BIG-IP as a Firewall';
}else if(scenario2 == true){
vault.Scenario__c = 'BIG-IP providing firewall service in conjunction with other vendors';
}else if(scenario3 == true){
vault.Scenario__c = 'BIG-IP front ending Firewalls';
if(vault.Load_Balancer_is_a_Firewall__c == null){
errorMsgs.add('Please specify if the load balancer is a firewall.');
}
if(vault.Providing_DDos_Connection__c == null){
errorMsgs.add('Please specify if this is providing DDos connection level protection for firewalls or both.');
}
if(vault.Providing_DDos_protection_except_ASM__c == null){
errorMsgs.add('Please specify if we are providing DDos protection (except ASM).');
}
if(vault.No_Traffic_Traverse__c == null){
errorMsgs.add('Please specify if there is Traffic that does not traverse the Firewalls.');
}
if(vault.Firewall_Vendor_Name__c == null){
errorMsgs.add('Please specify a Firewall Vendor.');
}
} else {
errorMsgs.add('Please select one of three Scenarios that is similar to your setup');
}
//-----Need to make sure deal registration number on opportuntiy is valid.-----
//Set some sort of is valid deal reg flag to false. We will assume the deal reg Id entered is invalid until we determine it is valid
vault.isValidDealReg__c = false;
//Query Account Id, and Deal_Registration_Id__c from the opportunity object where the deal registration Id equals the value entered in the form
List<Opportunity> opps = [select Id, Deal_Registration_ID__c from Opportunity where Deal_Registration_ID__c = :vault.Deal_Registration__c];
//check to see if registration id on the opp match the entered value
if(opps.size() > 0 && opps[0].Deal_Registration_ID__c == vault.Deal_Registration__c){
//If they do match set the valid deal reg Id flat to true
vault.isValidDealReg__c = true;
vault.Related_Opp__c = opps[0].Id;
//If they don't match then query all contacts on the account related to the opportunity
}
//If is valid deal reg flag is false add a message to the errorMSgs list indicated the entered
//deal registration is not valid with details on who they should contact
if(errorMsgs.size()>0){
//stop here
} else if(vault.isValidDealReg__c == false){
errorMsgs.add('Deal Registration # was incorrect. Please contact your representative.');
} else {
try{
vault.Status__c = 'Application Received';
insert vault;
saveResult = 'success';
}catch(exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Uh oh. Something didn\'t work quite right. Please contact partners#f5.com for assistance. \n\n' + e.getMessage()));
saveResult = 'fail';
}
}
}
//------------------------Code for CAPTCHA verification----------------------------
public String captchaInput {get; set;}
List<String> characters;
String char1;
String char3;
String char5;
//This methods simply returns a random number between 0 and the size of the character list
public Integer randomNumber(){
Integer random = Math.Round(Math.Random() * characters.Size());
if(random == characters.size()){
random--;
}
return random;
}
/*Here we have 6 get methods that return 6 random characters to the page.
For chars 1,3, and 5 (the black characters) we are saving the the values so
that we can compare them with the user's input */
public String getChar1(){
char1 = characters[randomNumber()];
return char1;
}
public String getChar2(){
return characters[randomNumber()];
}
public String getChar3(){
char3 = characters[randomNumber()];
return char3;
}
public String getChar4(){
return characters[randomNumber()];
}
public String getChar5(){
char5 = characters[randomNumber()];
return char5;
}
public String getChar6(){
return characters[randomNumber()];
}
public String correctAnswer(){
return char1 + char3 + char5;
}
public Boolean validateCAPTCHA(){
if(captchaInput.length() == 3 && captchaInput.subString(0,1) == char1 && captchaInput.subString(1,2) == char3 && captchaInput.subString(2,3) == char5){
return true;
}else{
return false;
}
}
}
Below is Test Class
#isTest
public class VaultVFTEST {
/*This tests the VaultVF class on the Vault__c object*/
static testMethod void VaultVFTEST() {
//Create opp, hardcode the Deal_Registration_Id__c field with 'x111111111' and insert opp
Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Deal_Registration_Id__c='x111111111', Probability = 95, CloseDate=system.today());
insert opp;
//Create and insert Vault record
Vault__c vault = new Vault__c(
Deal_Registration__c = 'x111111111',
Scenario__c = 'BIG-IP front ending Firewalls',
isValidDealReg__c = true,
Status__c = 'Application Received',
Load_Balancer_is_a_Firewall__c = 'Yes',
Providing_DDos_Connection__c = 'Firewalls',
Providing_DDos_protection_except_ASM__c = 'Yes',
No_Traffic_Traverse__c = 'Yes',
Firewall_Vendor_Name__c = 'Firewall Vendor Company',
First_Name__c = 'Test First Name',
Last_Name__c = 'Test Last Name',
Company_Name__c = 'Test Company Name',
Phone__c = '(206) 946-3126',
Email__c = 'test#email.com',
Country__c = 'United States',
Additional_Info__c = 'Test Additional Info',
Related_Opp__c = opp.Id
);
//Now insert the vault record to invoke the VaultVF class
Test.startTest();
insert vault;
Test.stopTest();
//Assertion Testing - Check to make sure the Deal_Registration__c value
//matches the Deal_Registration_Id__c value of the related opportunity
for(Vault__c v : [select Id, Deal_Registration__c from Vault__c where Deal_Registration__c IN :vault]){
system.assertEquals(v.Deal_Registration__c, opp.Deal_Registration_Id__c);
}
}
}

What's the Opportunity.Deal_Registration_ID__c field type? If it's a formula field or autonumber it will really really be not writeable from Apex.
Generally speaking in such scenarios you'd have to insert the Opportunity (without this field set), then read it back from database after a successful insert. If it's autonumber - magic will happen on it's own (although I wouldn't say it's a best idea to have some kind of license numbers consecutive & easy to fake ;)), if it's a formula field - make sure you've set all fields that are used in calculation prior to the insert.
So something like this should do the trick:
Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());
insert opp;
opp = [SELECT Id, Name, Deal_Registration_ID__c FROM Opportunity WHERE Id = :opp.Id];
// Optionally - make sure value was set
System.assertNotEquals(null, opp.Deal_Registration_ID__c);
System.assert(opp.Deal_Registration_ID__c.length() > 0); // if it's a String
Vault__c vault = new Vault__c(
Deal_Registration__c = opp.Deal_Registration_ID__c ,
Scenario__c = 'BIG-IP front ending Firewalls',
isValidDealReg__c = true,
Status__c = 'Application Received',
Load_Balancer_is_a_Firewall__c = 'Yes',
Providing_DDos_Connection__c = 'Firewalls',
Providing_DDos_protection_except_ASM__c = 'Yes',
No_Traffic_Traverse__c = 'Yes',
Firewall_Vendor_Name__c = 'Firewall Vendor Company',
First_Name__c = 'Test First Name',
Last_Name__c = 'Test Last Name',
Company_Name__c = 'Test Company Name',
Phone__c = '(206) 946-3126',
Email__c = 'test#email.com',
Country__c = 'United States',
Additional_Info__c = 'Test Additional Info',
Related_Opp__c = opp.Id
);

Related

System.TypeException: Invalid integer salesforce test class error in controller

i have a visual force page which is used as a view to send a custom sms to the leads generated in salesforce.The year field is a number field on salesforce. Posting the controller and test class error. Also mentioning the error line.
Controller :-
//Class to send Message to Lead or Account
public class nmSendSMS
{
//Name of Lead or Account
public string strName{get;set;}
public Lead objLead {get;set;}
public String defaultNumbersToBeAdded;
public String leadYear{get;set;}
//Mobile number of lead of account
public string strMobile{get;set;}
public String messageToBeSent{get;set;}
public List<SelectOption> getYear{get;set;}
public string strSMSBody{get;set;}
//To disable fields if data is not available
String session,statusOfLead,stringOfMobileNumbers;
public nmSendSMS()
{
objLead = new Lead();
leadYear ='';
defaultNumbersToBeAdded = '9820834921,9920726538';
messageToBeSent = '';
stringOfMobileNumbers= '';
}
//Method to send SMS
public PageReference SendSMS()
{
if(leadYear!='' || messageToBeSent!='')
{
session = objLead.nm_Session__c;
integer lengthOfCommaSeperatedNumbers;
String finalString ='';
statusOfLead = objLead.Status;
list<lead> leadNumbersList = [select MobilePhone from Lead where Status=:statusOfLead and nm_Session__c=:session and nm_Year__c=:integer.valueOf(leadYear)];
for(Lead obj :leadNumbersList)
{
stringOfMobileNumbers = obj.MobilePhone+','+stringOfMobileNumbers;
}
System.debug('stringOfMobileNumbers -->'+stringOfMobileNumbers);
lengthOfCommaSeperatedNumbers = stringOfMobileNumbers.length();
finalString = stringOfMobileNumbers.substring(0,lengthOfCommaSeperatedNumbers-1);
finalString = finalString + defaultNumbersToBeAdded;
System.debug('Final String--->'+finalString+'Message To Be Sent-->'+messageToBeSent);
String response = SMSSenderWebService.sendSMSForNotContactedLead(finalString,messageToBeSent);
System.debug('Response-->'+response);
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please Mention all the fields For Lead Search'));
return null;
}
return null;
}
public List<SelectOption> getYear()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('2016','2016'));
options.add(new SelectOption('2017','2017'));
options.add(new SelectOption('2018','2018'));
options.add(new SelectOption('2019','2019'));
return options;
}
}
Test Class:-
#isTest
public class nmSendSMSTracker
{
public static Lead obj;
public static nm_Centers__c objLearningCenter;
public static nm_Program__c program;
public static nm_EligiblityCriteria__c eligibility ;
static testMethod void tesMethod()
{
LoadData();
PageReference pg = new PageReference('/apex/nmSendSMS');
Test.setCurrentPage(pg);
Test.StartTest();
nmSendSMS smsSend = new nmSendSMS();
smsSend.getYear();
smsSend.objLead = obj;
smsSend.messageToBeSent ='Hello Vikas';
smsSend.SendSMS();
Test.StopTest();
}
static void LoadData()
{
nm_EscalationMatrix__c objCustomSeetings3 = new nm_EscalationMatrix__c();
objCustomSeetings3.name='0-1 Week';
objCustomSeetings3.nm_LCEscalationTime__c='22:45';
objCustomSeetings3.nm_RemidertoIC__c='22:45';
objCustomSeetings3.nm_HOEscalationTime__c='20:56';
objCustomSeetings3.nm_RemidertoHO__c='22:45';
insert objCustomSeetings3;
nm_EscalationMatrix__c objCustomSeetings = new nm_EscalationMatrix__c();
objCustomSeetings.name='2-4 Months';
objCustomSeetings.nm_LCEscalationTime__c='20:45';
objCustomSeetings.nm_RemidertoIC__c='21:45';
objCustomSeetings.nm_HOEscalationTime__c='20:56';
objCustomSeetings.nm_RemidertoHO__c='21:45';
insert objCustomSeetings;
nm_EscalationMatrix__c objCustomSeetings2 = new nm_EscalationMatrix__c();
objCustomSeetings2.name='3-6 Week';
objCustomSeetings2.nm_LCEscalationTime__c='20:34';
objCustomSeetings2.nm_RemidertoIC__c='21:45';
objCustomSeetings2.nm_HOEscalationTime__c='20:56';
objCustomSeetings2.nm_RemidertoHO__c='21:45';
insert objCustomSeetings2;
nm_Holidays__c objHoliday = new nm_Holidays__c();
objHoliday.Name='Holi';
objHoliday.nm_Date__c=system.today();
insert objHoliday;
// profile objprofile =[SELECT Id FROM Profile WHERE Name='System Administrator'];
user usr = [Select id from user limit 1];
SystemConfiguration__c objSystemConfiguration=new SystemConfiguration__c();
objSystemConfiguration.name='test';
objSystemConfiguration.nm_BusinessHoursStartTime__c='012213';
objSystemConfiguration.nm_BusinessHoursEndTime__c='0234533';
insert objSystemConfiguration;
Recordtype rt=[select id from Recordtype where sobjectType='nm_Centers__c' AND name ='Learning Center'];
objLearningCenter = new nm_Centers__c();
objLearningCenter.RecordTypeID =rt.id;
objLearningCenter.nm_CenterCode__c ='002';
objLearningCenter.nm_CenterCity__c='Delhi';
objLearningCenter.nm_City__c='Delhi';
objLearningCenter.nm_StateProvince__c='Delhi';
objLearningCenter.nm_Street__c='Laxmi Ngar';
objLearningCenter.nm_PostalCode__c='110091';
insert objLearningCenter;
program = new nm_Program__c();
program.nmIsActive__c = true;
program.nm_ProgramCode__c = 'test';
program.nm_ProgramDuration__c= 2.0;
program.nm_ProgramName__c = 'Post grad diploma finance';
program.nm_ProgramValidity__c = 4;
program.nm_TotalSemesters__c = 4;
program.nm_Type__c = 'Post Graduate Diploma Program';
insert program;
eligibility = new nm_EligiblityCriteria__c();
eligibility.Name = 'Bachelors degree';
eligibility.nm_EligiblityCriteria__c = 'bjhwbghbjgw';
eligibility.Experience_Required_In_Year__c= 2;
eligibility.Graduation_Percentage__c = 6;
eligibility.Graduation_Required__c = true;
insert eligibility;
obj = new Lead();
obj.Email='amit.kumar#saasfocus.com';
obj.MobilePhone='8377985721';
obj.FirstName='sandy';
obj.LastName='babar';
obj.nm_BloodGroup__c='B+';
obj.nm_Gender__c='male';
obj.nm_FathersName__c='subhash';
obj.nm_MothersName__c='kalpana';
obj.nm_StateProvince_P__c='maharashtra';
obj.nm_Nationality__c='Indian';
obj.nm_Street_P__c='xyz';
obj.nm_LocalityName__c='mohitep';
obj.nm_SelfLearningMaterial__c='Send to my shipping address';
obj.Status='Cold';
obj.nm_Session__c = 'January';
obj.nm_NameofBoard__c='CBSE';
obj.nm_EligiblityCriteria__c = eligibility.id;
obj.nm_Program__c = program.id;
obj.nm_InformationCenter__c =objLearningCenter.id;
obj.nm_10thPercentage__c=77.00;
obj.nm_NameofBoard__c='ICSC';
obj.nm_YearofCompletion__c='2000';
obj.nm_NameofSchool__c='nutan';
obj.nm_Class12OrDiploma__c='HSC';
obj.nm_NameofBoard12__c='LCSC';
obj.nm_YearofCompletion12__c='2002';
obj.nm_NameofSchool12__c='dfg';
obj.nm_Stream__c='adw';
obj.nm_BachelorsDegreeName__c='gfc';
obj.nm_Specialization__c='gf';
obj.nm_NameofUniversity__c='G K university';
obj.nm_BachelorsDegreePercentage__c=55.00;
obj.nm_GraduationDegreeMode__c='fgc';
obj.nm_YearofCollegeCompletion__c='2006';
obj.LeadSource='Web';
obj.OwnerId=usr.id;
insert obj;
}
}
Error Message:
System.TypeException: Invalid integer:
Class.nmSendSMS.SendSMS: line 37, column 1
Class.nmSendSMSTracker.tesMethod: line 19, column 1
List<Lead> leadNumbersList = [select MobilePhone from Lead where nm_Year__c=:leadYear];
Works for me and I get the correct Lead(s) in the list

No Viable Alternative at character ' '

Hi i am getting the following error in this code
/*
Class : CreateMobileChatterCntrl
Description : Post Chatter on Contact.
Developed by : Harish Khatri(Appirio Offshore)
Created Date : June 2, 2012
*/
public without sharing class CreateMobileChatterCntrl {
public final Id ContactID{get;set;}
public String message{get;set;}
public boolean isSuccess{get;set;}
public boolean throwError{get;set;}
public String deviceType{get;set;}
//----------------------------------------------------------------------------
//constructor
//----------------------------------------------------------------------------
public CreateMobileChatterCntrl() {
throwError = false;
isSuccess = false;
if( ApexPages.CurrentPage().getParameters().get('id') != null){
ContactID = ApexPages.CurrentPage().getParameters().get('id');
}
String userAgent = ApexPages.currentPage().getHeaders().get('USER-AGENT');
if(userAgent.contains('iPhone'))
deviceType = 'iPhone';
//else if(userAgent.contains('Android')) deviceType = 'Android';
}
//----------------------------------------------------------------------------
// Post the chatter on contact
//----------------------------------------------------------------------------
public Pagereference save() {
if(message == null || message ==''){
throwError = true;
return null;
}
FeedItem feedItem = new FeedItem();
feedItem.ParentId = ContactID;
feedItem.Body = message;
try {
insert feedItem;
isSuccess = true;
} catch(Exception e){}
return null;//new PageReference('/' + ContactID);
}
public Pagereference cancel() {
return new PageReference('/' + ContactID);
}
}
public final Id ContactID{get;set;} at this line i am getting the error No Viable Alternative at character ' '.can any one please help why i am getting this error??
Some of the single quote characters in your class file are invalid --- perhaps because you copied and pasted the code from somewhere else. I've had this happen many times before when I've copied code from elsewhere. Starting with the quotes in: message == '' , i'd delete the single quotes, retype them, and resave your file. Repeat for all single quotes (or do a find and replace).

automatically insert in a master relationship field

I have a VF page with a master relationship field, can I insert value in this field automatically
<apex:inputField value="{!a.Parent__c}"/>
Question is a bit vague but if you want to set a default value for the Parent__c you can just apply the value in your controller class.
EXAMPLE
public class VFController {
private Account controllerAccount;
//Basic Constructor
public VFController() {
controllerAccount = new Account();
controllerAccount.Parent__c = getDefaultParentAccount();
}
//Standard Controller Constructor
public VFController(ApexPages.Controller standardController) {
controllerAccount = (Account)standardController.getRecord();
controllerAccount.Parent__c = getDefaultParentAccount();
}
private Id getDefaultParentAccount() {
Id parentAccountId = null;
//Lookup default parent Account
List<Account> parentAccounts = [SELECT Id FROM Account WHERE /*Your Criteria here*/ LIMIT 1];
if (parentAccounts.isEmpty() == false) {
parentAccountId = parentAccounts[0].Id;
}
return parentAccountId;
}
}

Apex Test Class - How to set a rollup count field in a test class

Trying to set a value for a roll up summary field in the test class to improve code coverage. How do I do it?
public class clsPreferredIASetExt {
List<PreferredIA__c> preferredias;
public static PreferredIA__c[] tobeClosed = new PreferredIA__c[0];
public static PreferredIA__c[] newPreIAs = new PreferredIA__c[0];
public static PreferredIA__c loopadd;
public static PreferredContact__c[] contactlists = new PreferredContact__c[0];
public static Account[] InvoicedAccounts = new Account[0];
public static PreferredIA__c[] monkey;
public clspreferrediaSetExt(ApexPages.StandardSetController controller) {
preferredias = (List<PreferredIA__c>) controller.getSelected();
}
public void getInitCloseInv() {
tobeclosed = [select id, Account__c, Account__r.id, Account__r.Name,
Account__r.AccountNumber, Specialist__c,
PreferredInvoice__c, Status__c
from PreferredIA__c where Status__c = 'Invoiced' limit 150];
list<string> testme = new list<string>{};
for(PreferredIA__c a:tobeclosed) {
testme.add(a.Account__r.id);
}
InvoicedAccounts = [select id, EligibleIAs__c, PreferredOverride__c,
Preferred_Territory__r.rep__c, LastSurveyDate__c,
InitialInspectionComplete__c, Program_level__c,
PreferredExempt__c, Account_Status__c,
Active_IAs__c, Last_Training__c
from Account where id IN :testme];
Contactlists = [select id, Account__c
from PreferredContact__c where Account__c IN :testme];
for(PreferredIA__c q:tobeclosed) {
q.Status__c = 'Closed';
}
for(Account z:invoicedaccounts) {
/****************************************************************
The following condition is where I am trying to set the z.EligibleIAs__c
which is a roll up count field of PreferredIA__c objects associated with
the account.
****************************************************************/
if(z.EligibleIAs__c == 0
&& z.Program_Level__c == 'Preferred'
&& !z.PreferredExempt__c
&& (z.Account_Status__c == 'Active'
|| z.Account_Status__c == 'Product Only')) {
loopadd = new PreferredIA__c();
system.debug(z.id);
system.debug(z.Account_Status__c);
loopadd.Account__c = z.id;
if(z.PreferredOverride__c != null) {
loopadd.Specialist__c = z.PreferredOverride__c;
}
else {
loopadd.Specialist__c= z.Preferred_territory__r.Rep__c;
}
for(PreferredContact__c q:contactlists) {
if(q.Account__c == z.id) {
loopadd.PreferredContact__c = q.id;
}
}
loopadd.CreatedDate__c = Date.Today();
if(z.Last_training__c != null) {
loopadd.DueDate__c = z.Last_Training__c.AddDays(365);
}
else {
loopadd.DueDate__c = Date.Today().AddDays(365);
}
loopadd.initial__c = false;
loopadd.Status__c = 'Unacknowledged';
newPreIAs.add(loopadd);
}
z.InitialInspectionComplete__c = true;
}
try {
update tobeclosed;
update invoicedaccounts;
insert newPreIAs;
}
catch(system.dmlexception q) {
system.debug(q);
system.debug(invoicedaccounts);
system.debug(newPreIAs);
}
}
public void ReceivePPW() {
monkey = [select id, Status__c from PreferredIA__c
where id in :preferredias and status__c = 'Training Completed'];
for (PreferredIA__c m:monkey) {
m.status__c = 'Awaiting Invoice';
}
update monkey;
}
}
I can't actually see where you're trying to write to the field — or did you remove it because it wasn't working?
That aside, the answer is that you can not write to a roll-up summary field. If you require a value in that field you should insert child records to your parent test records, with appropriate field values such that your summary field calculates a value.
Also, I can see that you're querying PerferredIA__c at the start, your test methods should never depend on data being in the system already, you should insert your records yourself in your test code. The reason for this is that if you try to deploy to an org which has no relevant data, your tests will fail and so, subsequently, will your deployment.
For situations like these, consider mock objects (or just variables simulating expected values), similar to inserting test values as Lacey suggests. This technique is required to achieve 100% coverage when doing callouts, for example, since they terminate tests at the moment of the call.

Updating properties for multiple users

How do I update a list of different Telephone, IPPhone using this
static void Main(string[] args)
{
Console.Write("Enter userid : "); // I would pass this in from the first
//Field in the .csv file 2439009
String username = Console.ReadLine();
try
{
DirectoryEntry myLdapConnection = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(cn=" + uid + ")";
search.PropertiesToLoad.Add("Telephone","IPPhone");
SearchResult result = search.FindOne();
if (result != null)
{
// create new object from search result
DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
// show existing title
Console.WriteLine("Current title : " + entryToUpdate.Properties["Telephone][0].ToString());
Console.Write("\n\nEnter new title : ");
// get new title and write to AD
String newTitle = Console.ReadLine();
entryToUpdate.Properties["Telephone"].Value = newTelePhone;
entryToUpdate.Properties["IPPhone"].Value = newIPPhone;
entryToUpdate.CommitChanges();
Console.WriteLine("\n\n...new title saved");
}
else Console.WriteLine("User not found!");
}
catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
}
}
static DirectoryEntry createDirectoryEntry()
{
// create and return new LDAP connection with desired settings
DirectoryEntry ldapConnection = new DirectoryEntry("mydomain.dm.com");
ldapConnection.Path = "LDAP://OU=myusers,DC=sales,DC=US,DC=US";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
I'm guessing you've grabbed someone else's code and don't know how to use it?
You should understand that this code can (will?) cause serious server problems as the DirectoryEntry resources are not closed correctly.
Every DirectoryEntry variable in your Main method should be wrapped in a using(){} statement.
Try something like this:
You define a class CSVRecord which holds your data from the CSV - read that in using FileHelpers. The class looks like this:
public class CSVRecord
{
public string EmployeeNumber { get; set; }
public string TelephoneNumber { get; set; }
public string IPPhoneNumber { get; set; }
}
Once you've read that class in, you need to iterate over its elements, and do the update for each of them.
CSVRecord[] listOfEmployees = (read in via FileHelpers)
// define root for searching your user accounts
using (DirectoryEntry root = new DirectoryEntry("LDAP://dc=yourcompany,dc=com"))
{
// set up directory searcher to find users by employeeId
using (DirectorySearcher searcher = new DirectorySearcher(root))
{
searcher.SearchScope = SearchScope.Subtree;
// iterate over all entries in your list of employees
foreach (CSVRecord csvEntry in listOfEmployees)
{
searcher.Filter = string.Format("(&(objectCategory=user)(employeeId={0}))", csvEntry.EmployeeNumber);
// search for that employee
SearchResult result = searcher.FindOne();
// if found - access the DirectoryEntry
if (result != null)
{
DirectoryEntry foundUser = result.GetDirectoryEntry();
// update properties from values in CSV
foundUser.Properties["telephoneNumber"].Value = csvEntry.TelephoneNumber;
foundUser.Properties["ipPhone"].Value = csvEntry.IPPhoneNumber;
// save changes back to directory
foundUser.CommitChanges();
}
}
}
}
Does that work for you??

Resources