• Sandeepbv
  • NEWBIE
  • 0 Points
  • Member since 2017
  • Admin/Developer

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 8
    Replies
Hi Team!

I'm working on apex code to auto-update 5 fields when a picklist value is selected. I had the code with no errors and wrote a test class with no errors. But the test class is not having 75% code coverage.

The test class only covers the first if statment and the last else statment. Even though the test code is written for other criteria it won't cover that. Need help. Refer the below code for more info.
 
trigger Addressupdate on Sales_Summary__c (before insert, before update) {
    
    List<Sales_Summary__c> address = trigger.new;
    for(Sales_Summary__c sls: address){
        if(sls.Location__c=='Brooks'){
            sls.Street__c = '31 1st St W';
            sls.Country__c = 'canada';
            sls.City__c = 'Brooks';
            sls.Postal_Code__c ='T1R 2B6';
            sls.State__c = 'AB';
                      
        }
         if(sls.Location__c=='Camrose'){
            sls.Street__c = '6800 48th Ave';
            sls.Country__c = 'canada';
            sls.City__c = 'Camrose';
            sls.Postal_Code__c ='T4V 4T1';
            sls.State__c = 'AB';
                      
        }
        else if(sls.Location__c=='Canmore'){
            sls.Street__c = '802 Bow Valley Tr';
            sls.Country__c = 'canada';
            sls.City__c = 'Canmore';
            sls.Postal_Code__c ='T1W 1N6';
            sls.State__c = 'AB';
                      
        }
        else if(sls.Location__c=='Chestermere'){
            sls.Street__c = '#303 - 100 Marina Dr.';
            sls.Country__c = 'canada';
            sls.City__c = 'Chestermere';
            sls.Postal_Code__c ='T1X 0A9';
            sls.State__c = 'AB';
                      
        }
        else if(sls.Location__c=='Dawson Creek'){
            sls.Street__c = '11000 8th St';
            sls.Country__c = 'canada';
            sls.City__c = 'Dawson Creek';
            sls.Postal_Code__c ='V1G 3L8';
            sls.State__c = 'BC';
                      
        }
         else if(sls.Location__c=='Cranbrook'){
            sls.Street__c = '1500 Cranbrook St N';
            sls.Country__c = 'canada';
            sls.City__c = 'Cranbrook';
            sls.Postal_Code__c ='V1C 358';
            sls.State__c = 'BC ';
                      
        }
         else if(sls.Location__c=='High River'){
            sls.Street__c = '1225 1 St SE Unit E';
            sls.Country__c = 'canada';
            sls.City__c = 'High River';
            sls.Postal_Code__c ='T1V 0J5';
            sls.State__c = 'AB';
                      
        }
         else if(sls.Location__c=='Prince Rupert'){
            sls.Street__c = '500 2nd Ave W';
            sls.Country__c = 'canada';
            sls.City__c = 'Prince Rupert';
            sls.Postal_Code__c ='V8J 3T6';
            sls.State__c = 'BC ';
                      
        }
         else if(sls.Location__c=='Terrace'){
            sls.Street__c = '4761 Lakelse Ave Ste 104';
            sls.Country__c = 'canada';
            sls.City__c = 'Terrace';
            sls.Postal_Code__c ='V8G 4R9';
            sls.State__c = 'BC';
                      
        }
         else if(sls.Location__c=='Strathmore'){
            sls.Street__c = '500 Ranch Market';
            sls.Country__c = 'canada';
            sls.City__c = 'Strathmore';
            sls.Postal_Code__c ='T1P 0A8';
            sls.State__c = 'AB';
                      
        }
        else {
            sls.Street__c = '44 Maple Valley Rd';
            sls.Country__c = 'canada';
            sls.City__c = ' Corner Brook';
            sls.Postal_Code__c ='A2H 6J3';
            sls.State__c = 'NL';
        }
    }

}

---------------------------------------------------------------

@isTest
public class Addressupdate_test {
@isTest
    static void testAddressupdate(){
        Sales_Summary__c ss = new Sales_Summary__c();
        ss.Location__c = 'Brooks';
        ss.Street__c = '31 1st St W';
        ss.Country__c = 'canada';
        ss.City__c = 'Brooks';
        ss.Postal_Code__c ='T1R 2B6';
        ss.State__c = 'AB';  
        ss.Date__c = date.today();
        ss.New_add__c = 1;
        ss.Online_Sales__c = 1;
        ss.Green_members__c = 941;
        ss.Yellow_members__c = 93;
        ss.Frozen_Members__c = 68;
        ss.Total_Walk_ins__c = 3;
        ss.Total_Revenue_End_of_the_day__c = 416.84;
        ss.Ask_family_member_before_signing_up__c = 1;
        ss.Total_number_of_missed_walk_ins__c = 1;
        ss.Asking_for_drop_in__c = 0;
        ss.going_to_checkout_other_gyms_in_town_fir__c = 0;
        ss.Lack_of_classes__c = 0;
        ss.too_expensive__c = 0;
        ss.bi_weekly__c = 3;
             insert ss;
         Sales_Summary__c s1 =[select id, Location__c, Street__c, City__c, Postal_Code__c, State__c, Country__c from Sales_Summary__c where id=:ss.Id];
       system.assertEquals(s1.Location__c, 'Brooks');
        
        Sales_Summary__c sA = new Sales_Summary__c();
        sA.Location__c = 'Camrose';
        sA.Street__c = '6800 48th Ave';
        sA.Country__c = 'canada';
        sA.City__c = 'Camrose';
        sA.Postal_Code__c ='T4V 4T1';
        sA.State__c = 'AB';  
        sA.Date__c = date.today();
        sA.New_add__c = 1;
        sA.Online_Sales__c = 1;
        sA.Green_members__c = 941;
        sA.Yellow_members__c = 93;
        sA.Frozen_Members__c = 68;
        sA.Total_Walk_ins__c = 3;
        sA.Total_Revenue_End_of_the_day__c = 416.84;
        sA.Ask_family_member_before_signing_up__c = 1;
        sA.Total_number_of_missed_walk_ins__c = 1;
        sA.Asking_for_drop_in__c = 0;
        sA.going_to_checkout_other_gyms_in_town_fir__c = 0;
        sA.Lack_of_classes__c = 0;
        sA.too_expensive__c = 0;
        sA.bi_weekly__c = 3;
            insert sA;
                Sales_Summary__c s2 =[select id, Location__c, Street__c, City__c, Postal_Code__c, State__c, Country__c from Sales_Summary__c where id=:sA.Id];
       system.assertEquals(s2.Location__c, 'Camrose');
        
        Sales_Summary__c sB = new Sales_Summary__c();
        sB.Location__c = 'Canmore';
        sB.Street__c = '802 Bow Valley Tr';
        sB.Country__c = 'canada';
        sB.City__c = 'Canmore';
        sB.Postal_Code__c ='T1W 1N6';
        sB.State__c = 'AB';  
        sB.Date__c = date.today();
        sB.New_add__c = 1;
        sB.Online_Sales__c = 1;
        sB.Green_members__c = 941;
        sB.Yellow_members__c = 93;
        sB.Frozen_Members__c = 68;
        sB.Total_Walk_ins__c = 3;
        sB.Total_Revenue_End_of_the_day__c = 416.84;
        sB.Ask_family_member_before_signing_up__c = 1;
        sB.Total_number_of_missed_walk_ins__c = 1;
        sB.Asking_for_drop_in__c = 0;
        sB.going_to_checkout_other_gyms_in_town_fir__c = 0;
        sB.Lack_of_classes__c = 0;
        sB.too_expensive__c = 0;
        sB.bi_weekly__c = 3;
            insert sB;
                Sales_Summary__c s3 =[select id, Location__c, Street__c, City__c, Postal_Code__c, State__c, Country__c from Sales_Summary__c where id=:sB.Id];
       system.assertEquals(s3.Location__c, 'Canmore');
        
         Sales_Summary__c sC = new Sales_Summary__c();
        sC.Location__c = 'Chestermere';
        sC.Street__c = '#303 - 100 Marina Dr.';
        sC.Country__c = 'canada';
        sC.City__c = 'Chestermere';
        sC.Postal_Code__c ='T1X 0A9';
        sC.State__c = 'AB';  
        sC.Date__c = date.today();
        sC.New_add__c = 1;
        sC.Online_Sales__c = 1;
        sC.Green_members__c = 941;
        sC.Yellow_members__c = 93;
        sC.Frozen_Members__c = 68;
        sC.Total_Walk_ins__c = 3;
        sC.Total_Revenue_End_of_the_day__c = 416.84;
        sC.Ask_family_member_before_signing_up__c = 1;
        sC.Total_number_of_missed_walk_ins__c = 1;
        sC.Asking_for_drop_in__c = 0;
        sC.going_to_checkout_other_gyms_in_town_fir__c = 0;
        sC.Lack_of_classes__c = 0;
        sC.too_expensive__c = 0;
        sC.bi_weekly__c = 3;
            insert sC;
                Sales_Summary__c s4 =[select id, Location__c, Street__c, City__c, Postal_Code__c, State__c, Country__c from Sales_Summary__c where id=:sC.Id];
       system.assertEquals(s4.Location__c, 'Chestermere');
    }
Hello,

I created a before insert trigger which uses sosl queries to search for field value 'phone number' of call_log__c object in salesforce.
  • If phone is matched with Account phone number, it links call_log__c object with Account.
  • If phone is matched with phone_book__c phone, it links call_log__c object with phone_book__c.
  • If no record is found, then new phone_book__c record is created, and is linked with call_log__c object.
I have created a test class for trigger, but I could only test about 64%(11 of 17 Lines) of the trigger code. I have tried everything I can think of. Please, suggest me what I need to do in order to get 100% coverage.

Here's my trigger and test class
 
trigger MatchPhoneNumber on Call_Log__c (before insert) {
    
	List<List<sObject>> searchList = [FIND :Trigger.New[0].Caller_Id__c IN ALL FIELDS 
                   RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
    
	Account[] searchAccounts = (Account[])searchList[0];
	Phone_Book__c[] searchPhones =(Phone_Book__c[])searchList[1];
    
    if(!searchAccounts.isEmpty()){
		for (Account a : searchAccounts) {
        	for(Call_Log__c c : Trigger.New) {
        		c.member__c = a.ID;
    		}
    	}
    }
    
    if(!searchPhones.isEmpty()){
    	for (Phone_Book__c p : searchPhones) {
            for(Call_Log__c c : Trigger.New) {
                c.Phone_Number__c = p.ID;
            }
    	}
    }
    else{
        Phone_Book__c p = new Phone_Book__c();
        p.name = Trigger.New[0].Caller_Id__c;
        p.caller_id__c = Trigger.New[0].Caller_Id__c;
        insert p;
        for(Call_Log__c c : Trigger.New) {
                c.Phone_Number__c = p.ID;
            }
    }

}
 
@isTest 
public class TestMatchPhoneNumber {
    static testMethod void insertcalllog(){
        Account acc = new Account();
        acc.LastName = 'Jas';
        acc.Phone = '1234567890';
		acc.Security_Key__c = '1as4s22';
        insert acc;
        System.assertEquals(1,[select count() from Account  where LastName = 'Jas' ]);
        
        Phone_Book__c p = New Phone_book__c();
        p.name = '1234567890';
        insert p;
        System.assertEquals(1,[select count() from Phone_Book__c  where Name = '1234567890' ]);
        
        Call_Log__c c1 = New Call_Log__c();
        	Call_Log__c c2 = New Call_Log__c();

            c1.Caller_ID__c = '1234567890';
            c2.Caller_ID__c = '1111111111';
            insert c1;
            insert c2;
        Test.startTest();
            Id [] fixedSearchResults= new Id[2];
            fixedSearchResults[0] = acc.Id;
            fixedSearchResults[1] = p.Id;
            Test.setFixedSearchResults(fixedSearchResults);
            List<List<sObject>> searchList = [FIND '1234567890' IN ALL FIELDS 
                       RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
        
        	List<List<sObject>> searchList2 = [FIND '1111111111' IN ALL FIELDS 
                       RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
        
            Account[] searchAccounts = (Account[])searchList[0];
			Phone_Book__c[] searchPhones =(Phone_Book__c[])searchList[1];
        
            Account[] searchAccounts2 = (Account[])searchList2[0];
			Phone_Book__c[] searchPhones2 =(Phone_Book__c[])searchList2[1];
        
            
        Test.stopTest();
        
        System.assert(!searchAccounts.isEmpty());
        System.assert(!searchPhones.isEmpty());  
        System.assert(!searchAccounts2.isEmpty());
        System.assert(!searchPhones2.isEmpty()); 
        
        
            
            c1.member__c = acc.ID;
      	
        
         
                c1.Phone_Number__c = p.ID;     
    	
    
        Phone_Book__c ph = New Phone_book__c();
        ph.Name = c2.Caller_ID__c;
        ph.Caller_ID__c = c2.Caller_ID__c;
        insert ph;
        Call_log__c call = [Select id, member__c, phone_number__c from call_log__c where caller_id__c = '1234567890'];
        List<Phone_book__c> pl = [Select id from Phone_book__c where id = :ph.Id];
        System.assertEquals( pl.size() , 1 );
		//System.assertEquals(fixedSearchResults[0], call.member__c);  
		//System.assertEquals(fixedSearchResults[1], call.phone_number__c);  
     
    }
}

Thanks in advance
Jas
Hello,

I want to add Validation on account object when upserting new records. Please suggest how it can be done.

First, I want to check whether that record exists using "key" field(unique).If it exists, and the record has different email than what we have in our record; the system should throw an error with some message.

Second, I want to do same validation as in step 1, but this time with email. When upserting, if the record has same email and different key than what we have in our existing record; the system should not allow the upsert.
Hello guys,

I am a beginner and stuck at a simple test class for before delete trigger. Please help me what I am doing wrong.

Trigger code:
trigger NewRandomNumber on Random_Number__c (before delete) {
	Random_Number__c num = New Random_Number__c();

    Integer n = Math.round((100000 + Math.random() * 900000));
	num.Random_number__c = n; 
    
	insert num;
    
}

Test class:
@isTest 

public class TestRandomNumber {
	static testMethod void insertNewRandomNumber() {
        Random_Number__c num = New Random_Number__c();
        Integer n = 624575;
        num.Random_number__c = n;
         test.startTest();

        //insert random number
        insert num;
        
    	test.stopTest();
        
        
        //retrieve the new random number
        num = [SELECT Random_number__c FROM Random_number__c WHERE Id =:num.Id];
       System.debug('New number after delete trigger : ' + num.Random_number__c);
 
       // Test that the trigger correctly updated the number
       System.assertEquals(624575, num.Random_number__c);
   }
}

I tried to cover test for every line of code, but it seems I am doing something wrong as Itis showing 0% of test coverage.
Hello,

I am trying to get value from custom object using AJAX Toolkit inside custom button javascript action. I have written following code, but not able to get any value in a variable. 

Please, have a look at a code and let me know what could be the reason.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var keyfob = new sforce.SObject("Key_Fob_Dispenser__c");
keyfob.Id = '{!Key_Fob_Dispenser__c.Id}';


var key = sforce.connection.query("select random_number__c, id, name from random_number__c  limit 1");
var records = key.getArray("records"); 
keyfob.Lock_Code__c = records[0].random_number__c;
alert(records[0].random_number__c);

keyfob.Lock_Code__c = records[0].random_number__c;

var result = sforce.connection.update([keyfob]);

I see that records[0].random_number__c is not returning any value.

Thanks in Advance!
Hello,

I have a field 'Location' with picklist data type on user object. I have the same field on another object object2.
I want to populate 'Location' field in object2 with a value from user object.

Both fields are picklist.

Any suggestions how it can be done?
 
Hello,

I created a before insert trigger which uses sosl queries to search for field value 'phone number' of call_log__c object in salesforce.
  • If phone is matched with Account phone number, it links call_log__c object with Account.
  • If phone is matched with phone_book__c phone, it links call_log__c object with phone_book__c.
  • If no record is found, then new phone_book__c record is created, and is linked with call_log__c object.
I have created a test class for trigger, but I could only test about 64%(11 of 17 Lines) of the trigger code. I have tried everything I can think of. Please, suggest me what I need to do in order to get 100% coverage.

Here's my trigger and test class
 
trigger MatchPhoneNumber on Call_Log__c (before insert) {
    
	List<List<sObject>> searchList = [FIND :Trigger.New[0].Caller_Id__c IN ALL FIELDS 
                   RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
    
	Account[] searchAccounts = (Account[])searchList[0];
	Phone_Book__c[] searchPhones =(Phone_Book__c[])searchList[1];
    
    if(!searchAccounts.isEmpty()){
		for (Account a : searchAccounts) {
        	for(Call_Log__c c : Trigger.New) {
        		c.member__c = a.ID;
    		}
    	}
    }
    
    if(!searchPhones.isEmpty()){
    	for (Phone_Book__c p : searchPhones) {
            for(Call_Log__c c : Trigger.New) {
                c.Phone_Number__c = p.ID;
            }
    	}
    }
    else{
        Phone_Book__c p = new Phone_Book__c();
        p.name = Trigger.New[0].Caller_Id__c;
        p.caller_id__c = Trigger.New[0].Caller_Id__c;
        insert p;
        for(Call_Log__c c : Trigger.New) {
                c.Phone_Number__c = p.ID;
            }
    }

}
 
@isTest 
public class TestMatchPhoneNumber {
    static testMethod void insertcalllog(){
        Account acc = new Account();
        acc.LastName = 'Jas';
        acc.Phone = '1234567890';
		acc.Security_Key__c = '1as4s22';
        insert acc;
        System.assertEquals(1,[select count() from Account  where LastName = 'Jas' ]);
        
        Phone_Book__c p = New Phone_book__c();
        p.name = '1234567890';
        insert p;
        System.assertEquals(1,[select count() from Phone_Book__c  where Name = '1234567890' ]);
        
        Call_Log__c c1 = New Call_Log__c();
        	Call_Log__c c2 = New Call_Log__c();

            c1.Caller_ID__c = '1234567890';
            c2.Caller_ID__c = '1111111111';
            insert c1;
            insert c2;
        Test.startTest();
            Id [] fixedSearchResults= new Id[2];
            fixedSearchResults[0] = acc.Id;
            fixedSearchResults[1] = p.Id;
            Test.setFixedSearchResults(fixedSearchResults);
            List<List<sObject>> searchList = [FIND '1234567890' IN ALL FIELDS 
                       RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
        
        	List<List<sObject>> searchList2 = [FIND '1111111111' IN ALL FIELDS 
                       RETURNING Account(Name),Phone_Book__c(Name, Caller_Name__c)];
        
            Account[] searchAccounts = (Account[])searchList[0];
			Phone_Book__c[] searchPhones =(Phone_Book__c[])searchList[1];
        
            Account[] searchAccounts2 = (Account[])searchList2[0];
			Phone_Book__c[] searchPhones2 =(Phone_Book__c[])searchList2[1];
        
            
        Test.stopTest();
        
        System.assert(!searchAccounts.isEmpty());
        System.assert(!searchPhones.isEmpty());  
        System.assert(!searchAccounts2.isEmpty());
        System.assert(!searchPhones2.isEmpty()); 
        
        
            
            c1.member__c = acc.ID;
      	
        
         
                c1.Phone_Number__c = p.ID;     
    	
    
        Phone_Book__c ph = New Phone_book__c();
        ph.Name = c2.Caller_ID__c;
        ph.Caller_ID__c = c2.Caller_ID__c;
        insert ph;
        Call_log__c call = [Select id, member__c, phone_number__c from call_log__c where caller_id__c = '1234567890'];
        List<Phone_book__c> pl = [Select id from Phone_book__c where id = :ph.Id];
        System.assertEquals( pl.size() , 1 );
		//System.assertEquals(fixedSearchResults[0], call.member__c);  
		//System.assertEquals(fixedSearchResults[1], call.phone_number__c);  
     
    }
}

Thanks in advance
Jas
Hello guys,

I am a beginner and stuck at a simple test class for before delete trigger. Please help me what I am doing wrong.

Trigger code:
trigger NewRandomNumber on Random_Number__c (before delete) {
	Random_Number__c num = New Random_Number__c();

    Integer n = Math.round((100000 + Math.random() * 900000));
	num.Random_number__c = n; 
    
	insert num;
    
}

Test class:
@isTest 

public class TestRandomNumber {
	static testMethod void insertNewRandomNumber() {
        Random_Number__c num = New Random_Number__c();
        Integer n = 624575;
        num.Random_number__c = n;
         test.startTest();

        //insert random number
        insert num;
        
    	test.stopTest();
        
        
        //retrieve the new random number
        num = [SELECT Random_number__c FROM Random_number__c WHERE Id =:num.Id];
       System.debug('New number after delete trigger : ' + num.Random_number__c);
 
       // Test that the trigger correctly updated the number
       System.assertEquals(624575, num.Random_number__c);
   }
}

I tried to cover test for every line of code, but it seems I am doing something wrong as Itis showing 0% of test coverage.
Hello,

I am trying to get value from custom object using AJAX Toolkit inside custom button javascript action. I have written following code, but not able to get any value in a variable. 

Please, have a look at a code and let me know what could be the reason.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var keyfob = new sforce.SObject("Key_Fob_Dispenser__c");
keyfob.Id = '{!Key_Fob_Dispenser__c.Id}';


var key = sforce.connection.query("select random_number__c, id, name from random_number__c  limit 1");
var records = key.getArray("records"); 
keyfob.Lock_Code__c = records[0].random_number__c;
alert(records[0].random_number__c);

keyfob.Lock_Code__c = records[0].random_number__c;

var result = sforce.connection.update([keyfob]);

I see that records[0].random_number__c is not returning any value.

Thanks in Advance!
Hello,

I have a field 'Location' with picklist data type on user object. I have the same field on another object object2.
I want to populate 'Location' field in object2 with a value from user object.

Both fields are picklist.

Any suggestions how it can be done?
 
Hi,

I am trying to create a custom button on a custom object which can only be clicked if a date on the parent oject is filled in.  I am new to SOQL and javascript but have managed to created custom buttons in the past with validation rules which related to the object they are on however I am struggling with cross object rules.

In this instance an Assessment__c can only be submitted if it's parent Referral__c has an accepted date that is not blank.

Any help would be very much apprecitated.

Here is the code so far:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var ReferraAccepted = "SELECT Accepted_date__c FROM Referral__c WHERE Name = '{!Assessment__c.Referral__c}' LIMIT 1";

if (ReferraAccepted == null){alert ("Please submit the completed referral form to the Clearing House as this form cannot be submitted on it's own.")} 

else {

var newRecords = []; 
var c = new sforce.SObject("Assessment__c"); 
c.id ="{! Assessment__c.Id }"; 
c.Submit_for_assessment__c = true; 
newRecords.push(c); 
result = sforce.connection.update(newRecords); 
window.location.reload();
}