• jule robinson
  • NEWBIE
  • 30 Points
  • Member since 2019

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

I try to create test class for convert lead class, but i have an error that is . "System.DmlException: ConvertLead failed. First exception on row 0; first error: INVALID_STATUS, invalid convertedStatus: Creation account PA: [Status]".
Can you help me please ?

Class:
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Lead> LeadtoConvert)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel, ApiName FROM LeadStatus WHERE IsConverted=true AND ApiName='Creation account PA'];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        
        for(Lead currentlead: LeadtoConvert){
            	system.debug(currentlead.id);
            	system.debug(currentlead.status);
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead.id);
                Leadconvert.setConvertedStatus(CLeadStatus.ApiName);
                Leadconvert.setDoNotCreateOpportunity(TRUE);
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

Test class:
@isTest(seealldata = true)
public class TestAutoConvertLeads{
    public static testMethod void convertLead(){
        test.startTest();
        Lead leadtoConvert = new Lead();
        leadtoConvert.FirstName='Trigger1';
        leadtoConvert.LastName='Test1';
        leadtoConvert.Company='Trigger Test1';
        leadtoConvert.Exists_In_PartsAdvisor__c=true;
        insert leadtoConvert;
        List<Lead> listToConvert = new List<Lead>();
        listToConvert.add(leadToConvert);
       
        AutoConvertLeads.LeadAssign(listToConvert);
        
        test.stopTest();   
        
    } 
}

Thanks​​​​​​​
Hello,

I would like to create a custom button that create automatically an account in an external system.
I know how to create the button, I need you to know how communicate with the external system, maybe I need just to connect with the external database. How can I do it please ?

Thanks
Hi,
I need help to create test class for a TriggerHandler Class.
I will show you my code.
Class Trigger Handler:
public class ContactTriggerHandler {
    
    Public static void newTasks_VisitPlanner(List<Contact> newContacts, Map<Id,Contact> mapOldContacts) {
        
        
        List<Task> NewTasks = new List<Task>();
        for (Contact con: newContacts){
            If(!checkRecursive.SetOfIDs.contains(con.Id)){
                Contact oldContact = mapOldContacts.get(con.Id);
                if(con.Frequency__c != oldContact.Frequency__c){
                    
                    if (con.Frequency__c == '3 Months'){
                        for(Integer i = 1; i < 5; i++){
                            Task newTask = new Task(
                                WhoId = con.Id,
                                WhatId = con.AccountId,
                                OwnerId = con.Assigned_to__c,
                                Subject = 'Rappel : Visite périodique',
                                ActivityDate = Date.today().addMonths(i*3),
                                Priority = 'Normal',
                                Status = 'Not Started',
                                Created_from_Visit_planner__c = true
                            );
                            NewTasks.add(newTask);
                        }
                    }
                    
                    if (con.Frequency__c == '6 Months'){
                        for(Integer i = 1; i < 3; i++){
                            Task newTask = new Task(
                                WhoId = con.Id,
                                WhatId = con.AccountId,
                                OwnerId = con.Assigned_to__c,
                                Subject = 'Rappel : Visite périodique',
                                ActivityDate = Date.today().addMonths(i*6),
                                Priority = 'Normal',
                                Status = 'Not Started',
                                Created_from_Visit_planner__c = true
                            );
                            NewTasks.add(newTask);
                        }
                    }
                }
                checkRecursive.SetOfIDs.add(con.Id);
            }
        }
        
        
        if(NewTasks != null && NewTasks.size()>0){
            insert NewTasks;
        }    
    }
}

Trigger:
trigger NewTasks_VisitPlanner_Trigger on Contact (after update) {
    
    ContactTriggerHandler.newTasks_VisitPlanner(Trigger.new, Trigger.oldMap);
}

Test Class:
@isTest
private class Test_NewTasksVisit_Trigger {
    
    @isTest
    public static void Test_newsTask3Months(){
        Account acc = new Account(Name = 'OPALE', Type = 'Public sector', Main_Role__c = 'Contracting authority' );
        insert acc;
        
        Contact con_A = new Contact(FirstName='Albert',LastName='Roche', AccountId = acc.id);
        insert con_A;

        con_A.Frequency__c='3 Months';
        con_A.Assigned_to__c=con_A.Name;
        update con_A;
        
        con_A.Frequency__c='6 Months';
        con_A.Assigned_to__c=con_A.Name;
        update con_A;
    }
    
    @isTest
    public static void Test_newsTask6Months(){
        Account acc = new Account(Name = 'SOGEA', Type = 'Private sector', Main_Role__c = 'Contracting authority support' );
        insert acc;
        
        Contact con_B = new Contact(FirstName='Jean',LastName='Rabi', AccountId = acc.id);
        insert con_B;
        
        con_B.Frequency__c='6 Months';
        con_B.Assigned_to__c=con_B.Name;
        update con_B;
        
        con_B.Frequency__c='3 Months';
        con_B.Assigned_to__c=con_B.Name;
        update con_B;
    }
}

​​​​​​​​​​​​​​

Hello,
the code sent two time post chatter for the sames WhoId.
If condition found two Task
The code sent post chatter in first for the first WhoId, in second for the first and the second WhoId.
What can I do please ?
Thanks

Code: 
public ConnectApi.FeedElement CreatePostChatterRDV(){
        ConnectApi.FeedElement AlertPostChatter;
        
        
        List<Task> TaskAlert = new List<Task>();
        List<Task> listTask = [select OwnerId, WhoId, ActivityDate, Created_from_Visit_planner__c from task];
        for(Task ta: listTask){
            if(ta.ActivityDate == Date.today() && ta.Created_from_Visit_planner__c == true){
                //system.debug(ta);
                TaskAlert.add(ta);
                System.debug(TaskAlert);
                for(Task tb: TaskAlert){
                    List<User> utilisateur = [select Name from user where id = :tb.OwnerId];
                    for(User us: utilisateur){
                        
                        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
                        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
                        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
                        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
                        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
                        
                        mentionSegmentInput.id = tb.OwnerId;
                        messageBodyInput.messageSegments.add(mentionSegmentInput);
                        
                        textSegmentInput.text = ' ' + MessagePostChatter(us.Name);
                        messageBodyInput.messageSegments.add(textSegmentInput);
                        
                        feedItemInput.body = messageBodyInput;
                        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
                        feedItemInput.subjectId = ta.WhoId;
                        
                        AlertPostChatter = ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput);
                    }
                }
                
            }
        }
        return AlertPostChatter;
    }

 
Hi,
I need help to create test class for a TriggerHandler Class.
I will show you my code.
Class Trigger Handler:
public class ContactTriggerHandler {
    
    Public static void newTasks_VisitPlanner(List<Contact> newContacts, Map<Id,Contact> mapOldContacts) {
        
        
        List<Task> NewTasks = new List<Task>();
        for (Contact con: newContacts){
            If(!checkRecursive.SetOfIDs.contains(con.Id)){
                Contact oldContact = mapOldContacts.get(con.Id);
                if(con.Frequency__c != oldContact.Frequency__c){
                    
                    if (con.Frequency__c == '3 Months'){
                        for(Integer i = 1; i < 5; i++){
                            Task newTask = new Task(
                                WhoId = con.Id,
                                WhatId = con.AccountId,
                                OwnerId = con.Assigned_to__c,
                                Subject = 'Rappel : Visite périodique',
                                ActivityDate = Date.today().addMonths(i*3),
                                Priority = 'Normal',
                                Status = 'Not Started',
                                Created_from_Visit_planner__c = true
                            );
                            NewTasks.add(newTask);
                        }
                    }
                    
                    if (con.Frequency__c == '6 Months'){
                        for(Integer i = 1; i < 3; i++){
                            Task newTask = new Task(
                                WhoId = con.Id,
                                WhatId = con.AccountId,
                                OwnerId = con.Assigned_to__c,
                                Subject = 'Rappel : Visite périodique',
                                ActivityDate = Date.today().addMonths(i*6),
                                Priority = 'Normal',
                                Status = 'Not Started',
                                Created_from_Visit_planner__c = true
                            );
                            NewTasks.add(newTask);
                        }
                    }
                }
                checkRecursive.SetOfIDs.add(con.Id);
            }
        }
        
        
        if(NewTasks != null && NewTasks.size()>0){
            insert NewTasks;
        }    
    }
}

Trigger:
trigger NewTasks_VisitPlanner_Trigger on Contact (after update) {
    
    ContactTriggerHandler.newTasks_VisitPlanner(Trigger.new, Trigger.oldMap);
}

Test Class:
@isTest
private class Test_NewTasksVisit_Trigger {
    
    @isTest
    public static void Test_newsTask3Months(){
        Account acc = new Account(Name = 'OPALE', Type = 'Public sector', Main_Role__c = 'Contracting authority' );
        insert acc;
        
        Contact con_A = new Contact(FirstName='Albert',LastName='Roche', AccountId = acc.id);
        insert con_A;

        con_A.Frequency__c='3 Months';
        con_A.Assigned_to__c=con_A.Name;
        update con_A;
        
        con_A.Frequency__c='6 Months';
        con_A.Assigned_to__c=con_A.Name;
        update con_A;
    }
    
    @isTest
    public static void Test_newsTask6Months(){
        Account acc = new Account(Name = 'SOGEA', Type = 'Private sector', Main_Role__c = 'Contracting authority support' );
        insert acc;
        
        Contact con_B = new Contact(FirstName='Jean',LastName='Rabi', AccountId = acc.id);
        insert con_B;
        
        con_B.Frequency__c='6 Months';
        con_B.Assigned_to__c=con_B.Name;
        update con_B;
        
        con_B.Frequency__c='3 Months';
        con_B.Assigned_to__c=con_B.Name;
        update con_B;
    }
}

​​​​​​​​​​​​​​