• Electron
  • NEWBIE
  • 30 Points
  • Member since 2013

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 14
    Questions
  • 24
    Replies

Hi I am testing offline for our org. 

 

I set everything up, I found that offline couldn't support customized object fully. 

 

  • After finished the briefcase, I can see the customized object tab, and related Account and Contact. 
  • If I create records offline, it could sync to Cloud.
  • But I can't download any record for the customized object, if there is any change on cloud, we couldn't get that change.
  • And Offline doesn't support Help Text and Visualforce Page.

 

Are these default setting, or we could do something to improve it?

Hi I am writing an APEX Trigger to send MassEmail, I learned I could use SetTargetObjectId for MassEmailMessage, from this link:

http://www.forcetree.com/2009/07/sending-email-from-your-apex-class.html

 

I want to use setTargetObjectId, this method works very well in SingleEmailSeassage.

When I use it for MassEmailMessage,

 

It keeps showing the error

 

Method does not exist or incorrect signature

 

I thought I may give the wrong value for this method, I tried the List, Set<Id>, etc.

None of them works.

 

Does MassEmailMeassage supports this method, or it only supports it in APEX Class, not trigger?

 

the error came for SetWhatId, too. 

 

Update: 

Thanks Puja,

 

I forget to add things I did try the "setTargetObjectIds" and  "setTargetObjectId", but both of them shows same error for me. 

Hi Everyone, when I faced the too many queries error, then I realize how important this article is 

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

And I update all my code, use List for sObject, and put all queries out of loop. \

But it doesn't tell how to Where the update

 

Now I put update into the loop, because I have conditional statement.

 

Is there some method could check if List is changed or not? then update it? 

Hi I don't post code before, and thought that's lazy and waste other one's time.

But I really don't get it, it works very well in Sandbox, but testClass doesn't give green light.

 

1. What I have made. 

 

  • There is one look up field link to User on Contact page, the contact is staff of our Org.
  • There have 2 trigger which update the Phone number for both sides

       Such as when Admin update the phone number on Staff page, trigger update it to User's profile, and opposite way.

 

2. I finished the 2 trigger, and then write the testClass.

    I tried a lot, but it doesn't work, then I try to test it with real data in sandbox, nothing wrong, even Chatter Free User could work with these triggers. 

 

3. Then I try to comment the System.Assert part, I could still get 100 % cover, but I don't feel that's the correct way I should do.

 

     I also reviewed my code a lot, I still can't find how to make the testClass correct. 

 

Trigger 1

trigger ChattterToStaff on User (after update) {
	
	for(User SFUser: trigger.new)
	{
		User oldSFUser = Trigger.oldMap.get(SFUser.Id);
		List<Contact> staff = new List<Contact>();
		staff = [SELECT Id from Contact Where User__c=:SFUser.Id];
		
		If(staff.size()==1)
		{
			If(oldSFUser.Phone != SFUser.Phone)
			{
				staff[0].Phone = SFUser.Phone;
				update staff;
			}
			If(oldSFUser.MobilePhone != SFUser.MobilePhone)
			{
				staff[0].MobilePhone = SFUser.MobilePhone;
				update staff;
			}		
		}
	}
}

 

Trigger 2

 

trigger StaffToChatter on Contact (after update) {
	
	for(Contact Staff : trigger.new)
	{
		If(Staff.User__c !=null)
		{
			Contact oldStaff = Trigger.oldMap.get(Staff.Id);
			User SFUser = new User();
			SFUser.Id = Staff.User__c;
			If(oldStaff.Phone != Staff.Phone)
			{
				SFUser.Phone = Staff.Phone;
				update SFUser;
			}
			If(oldStaff.MobilePhone != Staff.MobilePhone)
			{
				SFUser.MobilePhone = Staff.MobilePhone;
				update SFUser;
			}
		}
	}
}

 

TestClass

 

@isTest
private class testChatterWithStaff {

    static testMethod void myUnitTest() {
        Profile pf = [select Id from Profile where Name='Standard User'];
        User testUser1 = new User(
        alias = 'test1', 
        email='test1@noemail.com',
        emailencodingkey='UTF-8', 
        lastname='Testing', 
        languagelocalekey='en_US',
        localesidkey='en_US', 
        profileid = pf.Id, 
        country='United States',
        timezonesidkey='America/Los_Angeles', 
        username='test1@noemail.com');
        insert testUser1;
        
        Id StaffRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Staff').getRecordTypeId();
        //initial test Contact
        Contact testContact = new contact(FirstName='test', LastName='Test', RecordTypeId = StaffRecordTypeId);
        insert testContact;
        testContact.User__c = testUser1.Id;
        update testContact;
        System.Debug('User is '+testContact.User__c);//correct
        
        testContact.Phone = '212-975-4321';
        update testContact;
        System.Debug('testContact Phone is'+testContact.Phone);
        System.Debug('testUser1 Phone is ' +testUser1.Phone); //User is not updated.
        System.assertEquals(testContact.Phone , testUser1.Phone);
        testContact.MobilePhone = ' 604-684-7221';
        update testContact;
        System.assertEquals(testContact.MobilePHone , testUser1.MobilePhone);  
        
        testUser1.Phone = '202-456-1121';
        update testUser1;
        System.assertEquals(testUser1.Phone , testContact.Phone);
        testUser1.MobilePhone = '202-456-9713';
        update testUser1;
        System.assertEquals(testUser1.MobilePhone , testContact.MobilePhone);   

    }
}

 

    

I am writing one Visual force Email template.

 

There is one field (Let's call it Field A) on the contact page, which is look-up field that link to account

I want to get the account name by field A, but I could only get the account's ID, right now. 

 

If this field link to another customed object, we can use

obj1__c.obj2__r

But in this scenario it's account, how could I get the Account Name? 

 

Also I think about to use SOQL, but is that right choice for Email Template?

 

I don't know if I explain it clearly, if not, I can write more, thank you for checking my question. 

 

Update 2013-05-14

 

Sorry I didn't explain it very well, in my template.

  • Recieve is User
  • Related object is Contact.

The field A is an look up field to account, I want to get the Account Name by field A. 

Hi Bob and other contributor,

 

I faced one very weird question, that I can't understand. 

 

1. I know when we finished APEX Class, we must grant it in profile, then user could use it.

    But what about trigger? should we do same thing?

 

    Then here is my question.

 

2. I use one line code to help me get the Record Type

    it works fine in my side, of course I am Administrator. 

Id ID = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Customer').getRecordTypeId();   

 

    But when I tested with other standard user, question came.     

    No matter what kind contact editing, salesforce keep throwing the error 

 Attempt to de-reference a null object

   the error occurred right in previous line. 

 

3. My first understand 

  

    I just feel when trigger is triggered, the user will execute  the action( is this right?)

    

    Then normal user doesn't have the enough permission to execute the piece of Code. 

    So I got the error. 

 

4. Then I tested another trigger, who use same code to get Record Type ID.

     but nothing wrong, that make me very confused.

 

update 2013-05-15

      

     I was wrong, this code still doesn't work for normal user.

 

     Also I have other limitation, such like field checking to limite the trigger action, but why this error stop every editing, it doesn't make sense!

 

5. To fix it, I take replace of the previous code to another Query, like below

Id Id = [SELECT id from RecordType where Name ='Customer'].Id;

    Then nothing error message came up.

 

    So Salesforce, are you kidding me? what's the point, I really don't get it.

 

 

Sorry for my dramatic tone, it took me so long time, I really feel bad for it, is there someone could answer my question?

I have a trigger works for changing the owner for related look-up object, and it works fine.

But I am confused by writint its test, I know test class shouldn't access a real data. 

 

So I try let test class insert a user, but user needs profile, that's still real data, if I do so, I still feel strange for this kind test. 

 

What I should do to test it? 

I searched one related topic

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-create-a-popup-window-using-Apex-code/td-p/197272

 

After read it, it seems no possible to achieve it in my scenario, that topic is from 3 years ago, does Salesforce update some functions?

 

Below is what I want:

 

  • A popup window comes out after Contact owner is changed.
  • Works on default page layout(without visualforce page)
  • Check the contact’s record type

 

Question:

  1. Could I write the response.write into a trigger, then it execute the pieces of JavaScript code
  2. If I use it in Visualforce, is there possible to check the record type.

 

I feel it’s so hard to achieve, will someone tell me it’s impossible T_T?

After finished my trigger and my unit test, I am starting to write my next one.

I know I should write the test class first.

 

When I run the test, my new trigger already has 56% test cover. 

The new trigger is related to same SObject, but I still think the number is not correct. 

 

So I had this idea, if these trigger works on same Object, I feel write the test method in one class seems more efficient. 

I want to use record Type to limit trigger action. 

 

Now I use querey to get it my code liks below

string Id = [SELECT id from RecordType where Name ='Someone'].Id;

 

I also found this page, we could use method to get the Record Type Id to avoid the query.

I still can't understand it.

 

Is there some simple and easy wayt to get the ID only by Record Type's Name?

Thanks to this community provides all the topics and great contributor , especial for Bob, now I write a very good APEX Trigger, and Unit test with 100% Test cover. 

 

But I am still very curious why most people use for loop to initial a trigger.

Such as for(sObject a:tirgger.new)

My trigger only works once update, why we should write it in a for loop?

 

And I think APEX trigger could do a lot work, I want to move to my next milestone, where I could learn more advanced APEX usage? 

Search the method by this guide? 

http://www.salesforce.com/us/developer/docs/apexcode/index.htm 

We can use APEX trigger to achive some function. Could we just use  APEX class withour trigger? 
I know using visualforce to callout APEX class, but could we do it in default interface and defualt object.
 
For example, if we change something on contact, then use APEX class to send E-mail and then do a series update, not the trigger.
I checked most example triggers, they works on the scenario, such as before insert, after update or something else.
Does it means, no matter what kind action we do, Salesforce will check all triggers that related to the object?
 
Is it low efficient, and will it slow the system running?
If yes, could we specific the trigger to specific field change?
 
I know we can detect the filed change by 
If(field change)

 

Could we make same thing to limit trigger? or we needn't to do it.

Hi everyone,

 

I made one APEX trigger, and I  read this article to learn how to write unit test http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests
It said we need Set Up All Conditions For Testing, what it means?
 
There are several conditional statements in my trigger.
Let me make one example that we use record type to limit the trigger.
Should I 
 
1.
I initialize a test contact, test one record type, then change its record type then test it and use system.assert.
 
Or 
 
2.
It's based on how many update action to the database, like there are 4 update in my trigger, I should cover these 4.
 
I am new to APEX, and thank you for answering my question.

Hi I am testing offline for our org. 

 

I set everything up, I found that offline couldn't support customized object fully. 

 

  • After finished the briefcase, I can see the customized object tab, and related Account and Contact. 
  • If I create records offline, it could sync to Cloud.
  • But I can't download any record for the customized object, if there is any change on cloud, we couldn't get that change.
  • And Offline doesn't support Help Text and Visualforce Page.

 

Are these default setting, or we could do something to improve it?

Hi I am writing an APEX Trigger to send MassEmail, I learned I could use SetTargetObjectId for MassEmailMessage, from this link:

http://www.forcetree.com/2009/07/sending-email-from-your-apex-class.html

 

I want to use setTargetObjectId, this method works very well in SingleEmailSeassage.

When I use it for MassEmailMessage,

 

It keeps showing the error

 

Method does not exist or incorrect signature

 

I thought I may give the wrong value for this method, I tried the List, Set<Id>, etc.

None of them works.

 

Does MassEmailMeassage supports this method, or it only supports it in APEX Class, not trigger?

 

the error came for SetWhatId, too. 

 

Update: 

Thanks Puja,

 

I forget to add things I did try the "setTargetObjectIds" and  "setTargetObjectId", but both of them shows same error for me. 

Hi Everyone, when I faced the too many queries error, then I realize how important this article is 

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

And I update all my code, use List for sObject, and put all queries out of loop. \

But it doesn't tell how to Where the update

 

Now I put update into the loop, because I have conditional statement.

 

Is there some method could check if List is changed or not? then update it? 

Hi I don't post code before, and thought that's lazy and waste other one's time.

But I really don't get it, it works very well in Sandbox, but testClass doesn't give green light.

 

1. What I have made. 

 

  • There is one look up field link to User on Contact page, the contact is staff of our Org.
  • There have 2 trigger which update the Phone number for both sides

       Such as when Admin update the phone number on Staff page, trigger update it to User's profile, and opposite way.

 

2. I finished the 2 trigger, and then write the testClass.

    I tried a lot, but it doesn't work, then I try to test it with real data in sandbox, nothing wrong, even Chatter Free User could work with these triggers. 

 

3. Then I try to comment the System.Assert part, I could still get 100 % cover, but I don't feel that's the correct way I should do.

 

     I also reviewed my code a lot, I still can't find how to make the testClass correct. 

 

Trigger 1

trigger ChattterToStaff on User (after update) {
	
	for(User SFUser: trigger.new)
	{
		User oldSFUser = Trigger.oldMap.get(SFUser.Id);
		List<Contact> staff = new List<Contact>();
		staff = [SELECT Id from Contact Where User__c=:SFUser.Id];
		
		If(staff.size()==1)
		{
			If(oldSFUser.Phone != SFUser.Phone)
			{
				staff[0].Phone = SFUser.Phone;
				update staff;
			}
			If(oldSFUser.MobilePhone != SFUser.MobilePhone)
			{
				staff[0].MobilePhone = SFUser.MobilePhone;
				update staff;
			}		
		}
	}
}

 

Trigger 2

 

trigger StaffToChatter on Contact (after update) {
	
	for(Contact Staff : trigger.new)
	{
		If(Staff.User__c !=null)
		{
			Contact oldStaff = Trigger.oldMap.get(Staff.Id);
			User SFUser = new User();
			SFUser.Id = Staff.User__c;
			If(oldStaff.Phone != Staff.Phone)
			{
				SFUser.Phone = Staff.Phone;
				update SFUser;
			}
			If(oldStaff.MobilePhone != Staff.MobilePhone)
			{
				SFUser.MobilePhone = Staff.MobilePhone;
				update SFUser;
			}
		}
	}
}

 

TestClass

 

@isTest
private class testChatterWithStaff {

    static testMethod void myUnitTest() {
        Profile pf = [select Id from Profile where Name='Standard User'];
        User testUser1 = new User(
        alias = 'test1', 
        email='test1@noemail.com',
        emailencodingkey='UTF-8', 
        lastname='Testing', 
        languagelocalekey='en_US',
        localesidkey='en_US', 
        profileid = pf.Id, 
        country='United States',
        timezonesidkey='America/Los_Angeles', 
        username='test1@noemail.com');
        insert testUser1;
        
        Id StaffRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Staff').getRecordTypeId();
        //initial test Contact
        Contact testContact = new contact(FirstName='test', LastName='Test', RecordTypeId = StaffRecordTypeId);
        insert testContact;
        testContact.User__c = testUser1.Id;
        update testContact;
        System.Debug('User is '+testContact.User__c);//correct
        
        testContact.Phone = '212-975-4321';
        update testContact;
        System.Debug('testContact Phone is'+testContact.Phone);
        System.Debug('testUser1 Phone is ' +testUser1.Phone); //User is not updated.
        System.assertEquals(testContact.Phone , testUser1.Phone);
        testContact.MobilePhone = ' 604-684-7221';
        update testContact;
        System.assertEquals(testContact.MobilePHone , testUser1.MobilePhone);  
        
        testUser1.Phone = '202-456-1121';
        update testUser1;
        System.assertEquals(testUser1.Phone , testContact.Phone);
        testUser1.MobilePhone = '202-456-9713';
        update testUser1;
        System.assertEquals(testUser1.MobilePhone , testContact.MobilePhone);   

    }
}

 

    

I am writing one Visual force Email template.

 

There is one field (Let's call it Field A) on the contact page, which is look-up field that link to account

I want to get the account name by field A, but I could only get the account's ID, right now. 

 

If this field link to another customed object, we can use

obj1__c.obj2__r

But in this scenario it's account, how could I get the Account Name? 

 

Also I think about to use SOQL, but is that right choice for Email Template?

 

I don't know if I explain it clearly, if not, I can write more, thank you for checking my question. 

 

Update 2013-05-14

 

Sorry I didn't explain it very well, in my template.

  • Recieve is User
  • Related object is Contact.

The field A is an look up field to account, I want to get the Account Name by field A. 

Hi Bob and other contributor,

 

I faced one very weird question, that I can't understand. 

 

1. I know when we finished APEX Class, we must grant it in profile, then user could use it.

    But what about trigger? should we do same thing?

 

    Then here is my question.

 

2. I use one line code to help me get the Record Type

    it works fine in my side, of course I am Administrator. 

Id ID = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Customer').getRecordTypeId();   

 

    But when I tested with other standard user, question came.     

    No matter what kind contact editing, salesforce keep throwing the error 

 Attempt to de-reference a null object

   the error occurred right in previous line. 

 

3. My first understand 

  

    I just feel when trigger is triggered, the user will execute  the action( is this right?)

    

    Then normal user doesn't have the enough permission to execute the piece of Code. 

    So I got the error. 

 

4. Then I tested another trigger, who use same code to get Record Type ID.

     but nothing wrong, that make me very confused.

 

update 2013-05-15

      

     I was wrong, this code still doesn't work for normal user.

 

     Also I have other limitation, such like field checking to limite the trigger action, but why this error stop every editing, it doesn't make sense!

 

5. To fix it, I take replace of the previous code to another Query, like below

Id Id = [SELECT id from RecordType where Name ='Customer'].Id;

    Then nothing error message came up.

 

    So Salesforce, are you kidding me? what's the point, I really don't get it.

 

 

Sorry for my dramatic tone, it took me so long time, I really feel bad for it, is there someone could answer my question?

I have a trigger works for changing the owner for related look-up object, and it works fine.

But I am confused by writint its test, I know test class shouldn't access a real data. 

 

So I try let test class insert a user, but user needs profile, that's still real data, if I do so, I still feel strange for this kind test. 

 

What I should do to test it? 

I searched one related topic

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-create-a-popup-window-using-Apex-code/td-p/197272

 

After read it, it seems no possible to achieve it in my scenario, that topic is from 3 years ago, does Salesforce update some functions?

 

Below is what I want:

 

  • A popup window comes out after Contact owner is changed.
  • Works on default page layout(without visualforce page)
  • Check the contact’s record type

 

Question:

  1. Could I write the response.write into a trigger, then it execute the pieces of JavaScript code
  2. If I use it in Visualforce, is there possible to check the record type.

 

I feel it’s so hard to achieve, will someone tell me it’s impossible T_T?

After finished my trigger and my unit test, I am starting to write my next one.

I know I should write the test class first.

 

When I run the test, my new trigger already has 56% test cover. 

The new trigger is related to same SObject, but I still think the number is not correct. 

 

So I had this idea, if these trigger works on same Object, I feel write the test method in one class seems more efficient. 

I want to use record Type to limit trigger action. 

 

Now I use querey to get it my code liks below

string Id = [SELECT id from RecordType where Name ='Someone'].Id;

 

I also found this page, we could use method to get the Record Type Id to avoid the query.

I still can't understand it.

 

Is there some simple and easy wayt to get the ID only by Record Type's Name?