• Jas Grewal
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 8
    Replies
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();
}