• Tristan P
  • NEWBIE
  • 35 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
Hi guys,

I'm relatively new to Apex and am struggling a bit with a Test Class - I hope you can help!

Here is my Apex Trigger to create new Community users when Contacts are inserted:
 
trigger NewCommUser on Contact (After insert) {
     
     if(Trigger.isInsert){ 
     
         for(Contact co : trigger.new){
            Contact con = [select id,email,firstName,lastname,accountId,Do_Not_Grant_Community_Access__c,Account_Status_Text__c from Contact where Id =:co.Id];         
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.EmailHeader.triggerUserEmail = false;       
            dmo.EmailHeader.triggerOtherEmail = false;
            dmo.EmailHeader.triggerAutoResponseEmail = false;       
            dmo.optAllOrNone = false;

            // Create Community Hub user
            List<User> unames = [select username from user where email =:con.email LIMIT 1];
            List<Contact> accountstatus = [select id from contact where Account_Status_Text__c = 'Signed-Up' and Id=:co.Id LIMIT 1];
            
            string userAlias;
            if (con.firstName.length() > 8)
            userAlias = con.firstName.substring(0,8); else userAlias = con.firstName;    
            
            if(unames.isEmpty() && !con.Do_Not_Grant_Community_Access__c && accountstatus.Size()>0){
                string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; 
                nick += Datetime.now().getTime();
                User newUser1 = new User(alias=userAlias, email = con.email, emailencodingkey = 'UTF-8', firstname = con.firstName, lastname = con.lastname, languagelocalekey = 'en_US',localesidkey = 'en_GB',contactId = con.Id,timezonesidkey = 'Europe/London',username = con.email,CommunityNickname = nick,ProfileId ='00e58000000NkUd', IsActive = true);
                newUser1.setOptions(dmo); insert newUser1;
            }
         }
     }
}

And here is my Test Class:
 
@isTest
public class NewCommUserTest {
    
    @Testsetup
    static void dataSetup() {
        
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
        
        
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName=uniqueName + '@test' + orgId + '.org');
        
        u.UserRoleId=[select Id from UserRole where Name='CEO'].Id;
        
        //System.debug([select Id from UserRole where Name='CEO']);
        insert u;
       
        System.runAs(u) {
            Account a = new Account();
            a.Name = 'Test Account';
            insert a;                 
            
            Contact c = new Contact();
            c.LastName = ' Contact1';
            c.FirstName = 'test';
            
            c.Account_Status_Text__c = 'Signed-Up';
            c.Email = 'standarduser1@testorg.com';
            c.Do_Not_Grant_Community_Access__c = true;
            c.AccountId = a.Id;
            insert c;
            
            c.Do_Not_Grant_Community_Access__c = FALSE;
            update c;
        }
        
        
    }    
    
    @isTest static void testUpdateOrgForNewBusiness2() { 
        
        Account a = [select Id from Account limit 1];
        Contact c = new Contact();
        c.LastName = 'Contact1234';
        c.FirstName = 'test';
        
        c.Account_Status_Text__c = 'Signed-Up';
        c.Email = 'standarduser1@testorg.com';
        c.Do_Not_Grant_Community_Access__c = true;
        c.Account = a;
        insert c;
        
        c.Do_Not_Grant_Community_Access__c = FALSE;
        c.LastName = 'Contact1';
        update c;
        
    }
    
}

I'm currently at 73% coverage but so far am having trouble with this part of my Trigger:
 
string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; 
nick += Datetime.now().getTime();
User newUser1 = new User(alias=userAlias, email = con.email, emailencodingkey = 'UTF-8', firstname = con.firstName, lastname = con.lastname, languagelocalekey = 'en_US',localesidkey = 'en_GB',contactId = con.Id,timezonesidkey = 'Europe/London',username = con.email,CommunityNickname = nick,ProfileId ='00e58000000NkUd', IsActive = true);
newUser1.setOptions(dmo); insert newUser1;

Can anyone please help me with how I can add this in my Test Class?

Thanks,

Tristan

 
Hi all,

I'm attempting to deploy the CustomSearchController class as written in the official developer documentation here:

https://developer.salesforce.com/docs/atlas.en-us.communities_dev.meta/communities_dev/communities_dev_example_search.htm
 
public class CustomSearchController {
	@AuraEnabled
	public static List<String> searchForIds(String searchText) {
		List<List<SObject>> results = [FIND :searchText IN ALL FIELDS  RETURNING Account(Id), Campaign(Id), 		Contact(Id), Lead(Id)];
		List<String> ids = new List<String>();
		for (List<SObject> sobjs : results) {
			for (SObject sobj : sobjs) {
				ids.add(sobj.Id);
			}
		}
		 return ids;
	}
}

However I can't see any instruction for writing the test class required. Can anyone please help with this?

Thanks,

Tristan
Hi,

I'm attempting to build a Lightning Component that will invoke a Send Email quick action on cases. Ideally this should be a button on each Case page that opens the email popup in Lightning Experience and fills in the To and CC fields automatically so that the user can write the email.

This is what I have so far:

Component code:
 
<aura:component implements="lightning:actionOverride,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome">
	
    <lightning:quickActionAPI aura:id="quickActionAPI" />

    <lightning:button label="Send Email" onclick="{!c.SendEmail }" />

</aura:component>

Controller code:
 
({
    SendEmail : function(component, event, helper) {
        
		var actionAPI = component.find("quickActionAPI");
        var args = { actionName :"Case.Send_Email" }; // UpdateContact is the action on the contact object
        actionAPI.selectAction(args).then(function(result) {
            // Action selected; show data and set field values
        })
        
        .catch(function(e) {
            if (e.errors) {
                // If the specified action isn't found on the page, 
                // show an error message in the my component 
            }
    	});
    }
})

At the moment I can click on the button, however nothing happens.

Does anyone know what I am doing wrong? I would rather not use the quick action itself within the chatter publisher as it is not very user friendly, so I'm trying to replicate what the old send email javascript button did in Classic.

Thanks

Hi all,

I'm trying to find out how I can automatically create a Community user when a Contact is created. I've had a look around and I think this could be done using an Apex class (?) as a part of a workflow but this is something I'm completely new to, so I haven't got any idea where to start. Is anyone able to give me any guidance here?

Thanks,

Tristan

Hi guys,

I'm relatively new to Apex and am struggling a bit with a Test Class - I hope you can help!

Here is my Apex Trigger to create new Community users when Contacts are inserted:
 
trigger NewCommUser on Contact (After insert) {
     
     if(Trigger.isInsert){ 
     
         for(Contact co : trigger.new){
            Contact con = [select id,email,firstName,lastname,accountId,Do_Not_Grant_Community_Access__c,Account_Status_Text__c from Contact where Id =:co.Id];         
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.EmailHeader.triggerUserEmail = false;       
            dmo.EmailHeader.triggerOtherEmail = false;
            dmo.EmailHeader.triggerAutoResponseEmail = false;       
            dmo.optAllOrNone = false;

            // Create Community Hub user
            List<User> unames = [select username from user where email =:con.email LIMIT 1];
            List<Contact> accountstatus = [select id from contact where Account_Status_Text__c = 'Signed-Up' and Id=:co.Id LIMIT 1];
            
            string userAlias;
            if (con.firstName.length() > 8)
            userAlias = con.firstName.substring(0,8); else userAlias = con.firstName;    
            
            if(unames.isEmpty() && !con.Do_Not_Grant_Community_Access__c && accountstatus.Size()>0){
                string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; 
                nick += Datetime.now().getTime();
                User newUser1 = new User(alias=userAlias, email = con.email, emailencodingkey = 'UTF-8', firstname = con.firstName, lastname = con.lastname, languagelocalekey = 'en_US',localesidkey = 'en_GB',contactId = con.Id,timezonesidkey = 'Europe/London',username = con.email,CommunityNickname = nick,ProfileId ='00e58000000NkUd', IsActive = true);
                newUser1.setOptions(dmo); insert newUser1;
            }
         }
     }
}

And here is my Test Class:
 
@isTest
public class NewCommUserTest {
    
    @Testsetup
    static void dataSetup() {
        
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
        
        
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName=uniqueName + '@test' + orgId + '.org');
        
        u.UserRoleId=[select Id from UserRole where Name='CEO'].Id;
        
        //System.debug([select Id from UserRole where Name='CEO']);
        insert u;
       
        System.runAs(u) {
            Account a = new Account();
            a.Name = 'Test Account';
            insert a;                 
            
            Contact c = new Contact();
            c.LastName = ' Contact1';
            c.FirstName = 'test';
            
            c.Account_Status_Text__c = 'Signed-Up';
            c.Email = 'standarduser1@testorg.com';
            c.Do_Not_Grant_Community_Access__c = true;
            c.AccountId = a.Id;
            insert c;
            
            c.Do_Not_Grant_Community_Access__c = FALSE;
            update c;
        }
        
        
    }    
    
    @isTest static void testUpdateOrgForNewBusiness2() { 
        
        Account a = [select Id from Account limit 1];
        Contact c = new Contact();
        c.LastName = 'Contact1234';
        c.FirstName = 'test';
        
        c.Account_Status_Text__c = 'Signed-Up';
        c.Email = 'standarduser1@testorg.com';
        c.Do_Not_Grant_Community_Access__c = true;
        c.Account = a;
        insert c;
        
        c.Do_Not_Grant_Community_Access__c = FALSE;
        c.LastName = 'Contact1';
        update c;
        
    }
    
}

I'm currently at 73% coverage but so far am having trouble with this part of my Trigger:
 
string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; 
nick += Datetime.now().getTime();
User newUser1 = new User(alias=userAlias, email = con.email, emailencodingkey = 'UTF-8', firstname = con.firstName, lastname = con.lastname, languagelocalekey = 'en_US',localesidkey = 'en_GB',contactId = con.Id,timezonesidkey = 'Europe/London',username = con.email,CommunityNickname = nick,ProfileId ='00e58000000NkUd', IsActive = true);
newUser1.setOptions(dmo); insert newUser1;

Can anyone please help me with how I can add this in my Test Class?

Thanks,

Tristan

 
Hi all,

I'm attempting to deploy the CustomSearchController class as written in the official developer documentation here:

https://developer.salesforce.com/docs/atlas.en-us.communities_dev.meta/communities_dev/communities_dev_example_search.htm
 
public class CustomSearchController {
	@AuraEnabled
	public static List<String> searchForIds(String searchText) {
		List<List<SObject>> results = [FIND :searchText IN ALL FIELDS  RETURNING Account(Id), Campaign(Id), 		Contact(Id), Lead(Id)];
		List<String> ids = new List<String>();
		for (List<SObject> sobjs : results) {
			for (SObject sobj : sobjs) {
				ids.add(sobj.Id);
			}
		}
		 return ids;
	}
}

However I can't see any instruction for writing the test class required. Can anyone please help with this?

Thanks,

Tristan