+ Start a Discussion
cyndie wandia 4cyndie wandia 4 
Please help me write a test class for an apex trigger that sends an email when a contact is created.
 
trigger EmailContact on Contact (after insert) {
List<Messaging.SingleEmailMessage> emailList= new List<Messaging.SingleEmailMessage>();
EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name='Contract Signed Thank you'];
for(Contact conObj:Trigger.new){
if (conObj.Email != null) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(conObj.Id);
mail.setSenderDisplayName('System Administrator');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(true);
mail.setTemplateID(emailTemplate.Id);
mail.toAddresses = new String[]{conObj.Email};
emailList.add(mail);
}
}
if(emailList.size()>0){
Messaging.SendEmailResult[] results = Messaging.sendEmail(emailList);
if (results[0].success)
{
System.debug('The email was sent successfully.');
} else {
System.debug('The email failed to send: '+ results[0].errors[0].message);
}
}
}


 
This is what i have so far. i get an error that lines 9&15 are missing ','
@isTest private class EmailContactTestClass { static testMethod void validateEmailContact() { Contact c = new Contact(LastName='Test',Email='test@test.org'); System.debug(c.LastName); insert c; System.assertEquals(0, Limits.getEmailInvocations(); c = [SELECT id FROM Contact WHERE LastName ='Test' limit 1]; System.assertEquals(1, Limits.getEmailInvocations(); } }

 
Best Answer chosen by cyndie wandia 4
Shubham Jain 338Shubham Jain 338
Hi,

The closing parenthesis is missing in System.assertEquals(1, Limits.getEmailInvocations() at both the places because of that you are getting compile-time error.

Try this 

@isTest private class EmailContactTestClass {
    static testMethod void validateEmailContact() { 
        Contact c = new Contact(LastName='Test',Email='test@test.org'); 
        System.debug(c.LastName); 
        insert c; 
        System.assertEquals(0, Limits.getEmailInvocations()); 
        c = [SELECT id FROM Contact WHERE LastName ='Test' limit 1]; 
        System.assertEquals(1, Limits.getEmailInvocations()); 
    } 
}

Kindly mark as best answer if it helps.


Thanks
Shubham Jain
suji srinivasansuji srinivasan 
i got 76% percentage coverage .i need to achieve 100% coverage. apex trigger
public static void afterupdate(List<opportunity> optylist,map<id,Opportunity> optyoldmap){
         Map<Id,Opportunity>mapopp= new Map<Id,Opportunity>();
         List<Project__c> projlist= new List<Project__c>();

         for(Opportunity opp:optylist){
            if ( opp.StageName != optyoldmap.get( opp.Id ).StageName && (opp.StageName=='closed won' || opp.stageName=='Closed Lost' )){
                mapopp.put(opp.id,opp);
            }
        }
        projlist=[Select Status__c ,Opportunity__c from Project__c where Opportunity__c in :mapopp.keyset()];
        
        if(projlist.size()>0){
            For(Project__c p:projlist ){
                if(mapopp.get( p.Opportunity__c ).StageName =='closed won')
                P.status__c='Active';
                if(mapopp.get( p.Opportunity__c ).StageName =='closed Lost')
                p.Status__c='Cancelled';
            }}
            update projlist;
       
        }
test class:
@isTest
public class update_projectHandlerTest {
public static testmethod void optyprojectTest(){
    list<opportunity> opty = new list<opportunity>();
    list<project__c>  proj  = new list<project__c>();
  
    opportunity op = new opportunity ();
    op.stagename = 'qualification';
    op.Name='test1';
    op.CloseDate = System.today();
    opty.add(op);
    insert opty;
        Project__c p1=new Project__c();
        p1.Status__c='Active';
        p1.opportunity__c=op.id;
        p1.Level_of_Effort__c='medium';
        proj.add(p1);
        insert proj;
    
     
       test.startTest();
     op.stagename = 'closedwon';
     update opty;
     p1.Status__c='Active';
     update proj;
     test.stopTest();
}}

thanks in advance.
Best Answer chosen by suji srinivasan
AnkaiahAnkaiah (Salesforce Developers) 
Hi Suji,

You have entered wrong picklist value in the 1st test method. stage name should be closed won  not closedwon.

try with below code.
 
@isTest
public class update_projectHandlerTest {
public static testmethod void optyprojectTest(){
    list<opportunity> opty = new list<opportunity>();
    list<project__c>  proj  = new list<project__c>();
  
    opportunity op = new opportunity ();
    op.stagename = 'qualification';
    op.Name='test1';
    op.CloseDate = System.today();
    opty.add(op);
    insert opty;
        Project__c p1=new Project__c();
        p1.Status__c='Active';
        p1.opportunity__c=op.id;
        p1.Level_of_Effort__c='medium';
        proj.add(p1);
        insert proj;
    
     
       test.startTest();
     op.stagename = 'closed won';
     update opty;
     p1.Status__c='Active';
     update proj;
     test.stopTest();
}

public static testmethod void optyprojectTest1(){
    list<opportunity> opty = new list<opportunity>();
    list<project__c>  proj  = new list<project__c>();
  
    opportunity op = new opportunity ();
    op.stagename = 'qualification';
    op.Name='test1';
    op.CloseDate = System.today();
    opty.add(op);
    insert opty;
        Project__c p1=new Project__c();
        p1.Status__c='Active';
        p1.opportunity__c=op.id;
        p1.Level_of_Effort__c='medium';
        proj.add(p1);
        insert proj;
    
     
       test.startTest();
     op.stagename = 'closed Lost';
     update opty;
     p1.Status__c='Cancelled';
     update proj;
     test.stopTest();
}

}

Thanks!!
 
JohnDuraiJohnDurai 
Hi - I have this scenario, One Account has multiple contacts and in Contact object we have field called Primary contact checkbox field.

If any one of the related contact  is true in  Primary contact checkbox field then other contacts shoud not have option to check that checkbox field.

How to do this lightning flow? can someone suggest ideas. Thanks
Best Answer chosen by JohnDurai
Sai PraveenSai Praveen (Salesforce Developers) 
Cont...

Adding the current record in loop to the variable created above:

User-added image
Updating the collection:

User-added image
Entire Flow:

User-added image

If this solution helps, please mark it as best answer.
If you have any doubts, Feel free to post the comment.


Thanks,
 
kavya mareedukavya mareedu 
Ans:  Text, Text Area, Text Area Long, Rich Text Area, URL. Is this is the right answer or is there something else????

13. Can we change the data type from Text to Auto Number for the Name when we already have?

Ans: I feel the answer is yes. If yes, please do let me know the explanation . If no, let me know the reason as well. Thanks guys. 

 
Best Answer chosen by kavya mareedu
sairam akella 11sairam akella 11
Yes.. We change the Data field type from name to autonumber . But the changes only apply to the new record not for the existing record.

As name is one of the default field that is created while creating object and also internally it is used for indexing Salesforce has given this permissiong to edit the data type of this field.
 
Demi DevDemi Dev 
We currently dont use record type in our environment and all data is collected on page view.
We like to move forward using record type but want to be sure how the data should be parsed out. Do we need to separate the data (so if we have 2 record types Staff and Student, and we have a contact that is a staff and a student, then there will be a Staff record and a Student record for the same contact).
Or can we have 2 record types applied to the same record?
Best Answer chosen by Demi Dev
PriyaPriya (Salesforce Developers) 

Hi Demi,

Yes You can have multiple record type applied to the same Object but not to same record. 

Let me explain you the concept of record type. 

So on the contact object you have to create two record type. one as Staff and another as Student. 

Also create two different page layout, one is staff pagelayout and another is student pagealout.

Assing the pagelayout i.e., staff record type should have Staff pagalyout and Student will have student pagelayout.

Create Contact.
When you click the new button to create contact, one modal box will pop up which will ask, for which kind of contact you wanna create. First option will be Staff and the Second will Student. 

Choose Staff and then create the Record. It will create the contact record as Staff. Follow same for Student.

How to create the page layout and Record type, follow this article :- 

https://trailhead.salesforce.com/en/content/learn/projects/customize-a-salesforce-object/create-record-types

https://www.sfdcpoint.com/salesforce/record-types-in-salesforce/#:~:text=Record%20types%20in%20Salesforce%20allow,different%20picklist%20values%20for%20each.

 Hope this helps,

Kindly mark it as the best answer so that it can helps others as well.

Regards,

Priya Ranjan

Michael Johnson 149Michael Johnson 149 

Hi All,

I am trying to deploy a flow using changesets and keep running into this error while validating below.
 

"Because the DoesRequireRecordChangedToMeetCriteria field has the value "true", you also need to set the following fields: Filters."

I have included every field that is referenced within the flow in the changeset. The only information I can even find on this "DoesRequireRecordChangedToMeetCriteria" variable is that it is now retrievable through the API. I have tried changing the starting conditions on the flow to even be very basic "everytime a record is created" without any conditional logic and still run into the same error. Does anyone have any idea as to what may be prompting this error? Thanks!

Best Answer chosen by Michael Johnson 149
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Michael,

If changing of API version does not work. Then you can try to remove that tag in Metadata and try to deploy it.

If the solution helps, Please mark it as best answer.

Thanks,
 
SandrineSandrine 
Hello,

I alwaays get error when i try to save something from Dev console

Failed to create createContainerMember for containerId duplicate value found: <unknown> duplicates value on record with id: <unknown>
Best Answer chosen by Sandrine
AshlekhAshlekh
Hi,

Follow these stepsGo to Developer Console -> Work Space -> New Workspace
Create a new workspace
Save classes/triggers

*https://help.salesforce.com/apex/HTViewSolution?id=000204953&language=en_US

Try again, it should be fixedIf my answer helps resolve your query, please mark it as the 'Best Answer' and upvote it to benefit others and improve the overall quality of Discussion Forums.


-Thanks
Ashlekh Gera




 
Nandhini S 3Nandhini S 3 
Hi Guys,

I would like to know to write a test class for a trigger which makes a callout.
Best Answer chosen by Nandhini S 3
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Nandhini,

Can you check the below example for test class .

https://developer.salesforce.com/forums/?id=9062I000000g3emQAA

If this solution helps, Please mark it as best answer.

Thanks,
 
Thomas PhilippouThomas Philippou 
Hi,

I'm trying to access the OAS3 spec for versions older than 54.0 but when I add 53.0 or older, I get a "resource not found" response.

Is there any way to access this or is it not available for anything older than 54.0?

I only recently enabled the OAS3 setting under User Interface so I'm wondering if the versions available are based on when this was done?

https://developer.salesforce.com/blogs/2021/01/learn-moar-with-spring-21-openapi-3-0-spec-for-rest-api

TIA
Best Answer chosen by Thomas Philippou
AnkaiahAnkaiah (Salesforce Developers) 
Hi Thomas,

Answer is no. The API version must be 54.0 or greater to access OAS3.

Refer the below article for more information.
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/openapi_beta.htm

If this helps, Please mark it as best answer.

Thanks!!
Naoki Kitaarashi 4Naoki Kitaarashi 4 
It's a basic question. How to rename Lightning component once created?  I couldn't find any menu on Devloper console and any buttons on Setup|Develop|Lightning Components. Is it necessary to delete and create new one with new name?
Best Answer chosen by Naoki Kitaarashi 4
Himanshu ParasharHimanshu Parashar
Hi Naoki,

Yes that is correct. You need to delete the component and create new one with new name.


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.