• Marc Gauthier
  • NEWBIE
  • 30 Points
  • Member since 2011

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 9
    Replies

HI All,

 

Anyone please help me with the trigger,  basically it fires on insert and works if a user with username 'abc@abc.com'' assigns a new task to anyone other than the owner of the task it will be automatically assigned to the owner of the task.

THe issue is i don't have any unit test failure but when i validate the code it throws following error."Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.Task_AssignOwnerTest.test: line 51, column 1"

 

i could put a if(User.size()==0) then return;

but to do this i need to do below change: 


User[] user = [
select Id, ProfileID
from User
where IsActive = true
and username = 'abc@abc.com'
limit 1
];

 

and if i do above changes then i am not able to runAs (User[0].id) .

as the error is "runAs requires single argument of type 'user'"

 

Below is the test class

         


@IsTest
private class Task_AssignOwnerTest {
public static Program__c program1 = [select Id from Program__c limit 1];

@IsTest
public static void test() {
User[] userList = [
select UserRoleId
from User
where Profile.Name = 'System Administrator'
and UserRoleId != null
and IsActive = true
limit 3
];

Account account = new Account();
account.name = 'Testing account';
account.ownerId = userList[0].Id;
insert account;
System.debug(' ::Task_AssignOwnerTest:: step 1' +account.Id);

Contact contact = new Contact();
contact.FirstName = 'FirstName';
contact.Start_Term__c = 'Jan 2011';
contact.LastName = 'LastName';
contact.Program_Primary__c = program1.Id;
contact.OwnerId = userList[0].Id;
contact.AccountId = account.Id;
insert contact;
System.debug(' ::Task_AssignOwnerTest:: step 2' +contact.Id);

Opportunity opportunity = new Opportunity();
opportunity.Contact__c = contact.Id;
opportunity.Name = 'OpportunityName';
opportunity.StageName = 'StageName';
opportunity.CloseDate = Date.today();
opportunity.Program__c = program1.Id;
opportunity.OwnerId = userList[0].Id;
insert opportunity;
System.debug(' ::Task_AssignOwnerTest:: step 3' +opportunity.Id);

(Line 51 is here)User user = [
select Id, ProfileID
from User
where IsActive = true
and username = 'abc@abc.com'
limit 1
];

 System.debug(' ::Task_AssignOwnerTest:: step 4' +user[0].id);

try {
System.RunAs(user){
Task[] taskList = new Task[] {
new Task (WhoId = contact.Id, OwnerId = userList[1].Id),
new Task (WhoId = null),
new Task (WhoId = opportunity.Id, OwnerId = userList[1].Id)
};
insert taskList;
}
} catch (System.dmlException e) {

}

}
}

Hello,

 

I wrote a trigger which prevents duplicates. The trigger is on Lead object and check if the email (Email) entered is already in use in Contact's email (Applicant_Email__c). The code is perfectly working , the problem is with test class, My test class is stuck at 72%. The code covereage is not happening  after the for loop. I tried multiple ways by inserting debug statements. The records are inserted by not modified by code. In the below code lines  highlighted in organge color is not being covered by test class.

Can anyone help me pass this test case. 

 

Trigger:

 

public without sharing class Lead_EmailDuplicatepreventer extends LeadTrigger {
public Lead_EmailDuplicatepreventer(Lead[] leadOldList, Lead[] leadNewList) {
super(leadOldList, leadNewList);
}

public override void execute() {

for(Lead LeadNew : LeadNewList){

lead leadOld = leadOld(leadNew.Id);

if ((Trigger.isInsert == true && leadNew.Email == null) || (Trigger.isUpdate == true && leadNew.Email == null) ){
return;
}


for ( Contact contacts : [select Applicant_Email__c FROM Contact where email = :LeadNew.Email]){

if (Trigger.isInsert == true && leadNew.Email == contacts.Applicant_Email__c){
System.debug ('::Lead_EmailDuplicatepreventer::==>1');
LeadNew.Email.addError('Email already in use');
}
else if (Trigger.isUpdate == true && ((leadNew.Email != leadOld.Email && leadNew.Email == contacts.Applicant_Email__c) || (leadOld.Email == null && leadNew.Email == contacts.Applicant_Email__c))){
System.debug ('::Lead_EmailDuplicatepreventer::==>2');
LeadNew.Email.addError('Email already in use');
}

}
}
}

}

 

Test Class:


@IsTest
private class Lead_EmailDuplicatePreventerTest {
public static final Country__c country = [select Id, Name from Country__c limit 1];

@IsTest
public static void test() {

Contact contacts = new Contact();
contacts.LastName = 'TestContact';
contacts.Applicant_Email__c = 'TestContact@test.com';
insert contacts;
System.debug('::Lead_EmailDuplicatePreventerTest::contact==>'+contacts.Applicant_Email__c);

Lead lead = new Lead();
lead.FirstName = 'FirstName1';
lead.LastName = 'LastName1';
lead.Company = 'Company1';
lead.Email = 'LeadContact@test.com';
lead.LeadSource = 'Alumni';
lead.Status = 'Contacted';
lead.Country_Of_Citizenship__c = country.id;
lead.Country_Of_Residence__c = country.id;
insert lead;
System.debug('::Lead_EmailDuplicatePreventerTest::contact==>'+lead.Applicant_Email__c);

lead.Email = 'TestContact@test.com';
update lead;

lead.Email = '';
update lead;

lead.Email = 'lead1@test.com';
update lead;

lead.Email = 'TestContact@test.com';
update lead;


}
}

I also tried using assertions which are failing.

 

 

 

error

 

Hi,

 

I am  new to development and need some help.

I have a cutom object  "Payment__c" , this has all payment information related fields like card name, expire_date__c, etc .    I need to pull the fields values and add a month to the expire_date__c field. Below is my code,

 

CODE:

Payment__c[] payment = [Select Name, expire_date__c FROM Payment__c];

String updatedexpire; 

for (Payment__c  temp : payment)

{

 updatedexpire=  temp.expire_date__c.Months(1);

System.debug ('the current value of updatedexpire==> '+updatedexpire );

}

 

The datetype of expire_date__c is Text(4)

When I execute  this piece of code in Anonymous  , it throws an error

"Method does not exist  or incorrect signature:[String].Months(Integer)"

Can anyone please help me.


Hi,

 

I have 2 objects A and B which i am trying to synchronize.i.e updating or inserting owner field in object A will sync owner record in B. So i have 1 trigger each on each object which updates the records. The issue is, these triggers will keep running recursively as everytime an appointmet is updated the event is also updated and the triggers keep firing. I used the following solution which made the sync between A  &  B  object work .

 

Trigger on A:

 

trigger klm on A (after insert, after update)
{
if (klm.stateA == false) //static boolean variable initialised to false
{
klm.stateA= true;
new klm(trigger.old, trigger.new).execute();
klm.stateA=false;
}
}

 

Initialized static boolean variables in class lmn and pqr to false

 

Trigger on B:

trigger pqr on B (after insert, after update) 
{
if (pqr .stateB == false) //static boolean variable initialised to false
{
pqr .stateB= true; 
new pqr (trigger.old, trigger.new).execute();
pqr .stateB=false;
}
}

 

The code works !!!, but the problem is that when I try to update the record , only sometimes it throws the following error:

" The record you were editing was modified by XXX user during the edit sesssion" 

At first I found the problem is only when i try to update or insert using "Inline" editing but later found that the problem was existing even when I tried to click edit and save the record. I am not able to uderstand why this kind of error occurs.

Please help me if you are familiar with this kind of error. 

 

Thanks,

Marc

Hello,

 

Please help me on this trigger !!!

The trigger is on Lead and needs to fire only when

1. A checkbox field (Convert__c ) in Lead in selected.

2. The custom object (Obj2__c) has only 3 similar fields of Leads i.e Compnany__c, Date__c and Location__c  which needs to  be inserted or updated.

 

I wrote the below trigger, it is working good but it creates duplicates whenver a record is updated. Can anyone please help me in showing how to avoid duplicates. I am also not sure if I have to use before insert or after insert.

 

CODE:


trigger abc  on Lead (after insert, after update) // Not sure if I can use before or after 
{

    List<Obj2__c>  list  = new List <Obj2__c> {};
         
        for (Lead lead : trigger.new)
        {
        
            if (lead.Convert_c == true) 
            {
            
            list .add(new Obj2__c 

        (Company__c = lead.Company,

         Date__c = lead.Date__c,  

         Location__c = lead.Location__c) );
            
            }
        }
    insert list;
}

 

Thanks for your help.

HI All,

 

Anyone please help me with the trigger,  basically it fires on insert and works if a user with username 'abc@abc.com'' assigns a new task to anyone other than the owner of the task it will be automatically assigned to the owner of the task.

THe issue is i don't have any unit test failure but when i validate the code it throws following error."Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.Task_AssignOwnerTest.test: line 51, column 1"

 

i could put a if(User.size()==0) then return;

but to do this i need to do below change: 


User[] user = [
select Id, ProfileID
from User
where IsActive = true
and username = 'abc@abc.com'
limit 1
];

 

and if i do above changes then i am not able to runAs (User[0].id) .

as the error is "runAs requires single argument of type 'user'"

 

Below is the test class

         


@IsTest
private class Task_AssignOwnerTest {
public static Program__c program1 = [select Id from Program__c limit 1];

@IsTest
public static void test() {
User[] userList = [
select UserRoleId
from User
where Profile.Name = 'System Administrator'
and UserRoleId != null
and IsActive = true
limit 3
];

Account account = new Account();
account.name = 'Testing account';
account.ownerId = userList[0].Id;
insert account;
System.debug(' ::Task_AssignOwnerTest:: step 1' +account.Id);

Contact contact = new Contact();
contact.FirstName = 'FirstName';
contact.Start_Term__c = 'Jan 2011';
contact.LastName = 'LastName';
contact.Program_Primary__c = program1.Id;
contact.OwnerId = userList[0].Id;
contact.AccountId = account.Id;
insert contact;
System.debug(' ::Task_AssignOwnerTest:: step 2' +contact.Id);

Opportunity opportunity = new Opportunity();
opportunity.Contact__c = contact.Id;
opportunity.Name = 'OpportunityName';
opportunity.StageName = 'StageName';
opportunity.CloseDate = Date.today();
opportunity.Program__c = program1.Id;
opportunity.OwnerId = userList[0].Id;
insert opportunity;
System.debug(' ::Task_AssignOwnerTest:: step 3' +opportunity.Id);

(Line 51 is here)User user = [
select Id, ProfileID
from User
where IsActive = true
and username = 'abc@abc.com'
limit 1
];

 System.debug(' ::Task_AssignOwnerTest:: step 4' +user[0].id);

try {
System.RunAs(user){
Task[] taskList = new Task[] {
new Task (WhoId = contact.Id, OwnerId = userList[1].Id),
new Task (WhoId = null),
new Task (WhoId = opportunity.Id, OwnerId = userList[1].Id)
};
insert taskList;
}
} catch (System.dmlException e) {

}

}
}

Hello,

 

I wrote a trigger which prevents duplicates. The trigger is on Lead object and check if the email (Email) entered is already in use in Contact's email (Applicant_Email__c). The code is perfectly working , the problem is with test class, My test class is stuck at 72%. The code covereage is not happening  after the for loop. I tried multiple ways by inserting debug statements. The records are inserted by not modified by code. In the below code lines  highlighted in organge color is not being covered by test class.

Can anyone help me pass this test case. 

 

Trigger:

 

public without sharing class Lead_EmailDuplicatepreventer extends LeadTrigger {
public Lead_EmailDuplicatepreventer(Lead[] leadOldList, Lead[] leadNewList) {
super(leadOldList, leadNewList);
}

public override void execute() {

for(Lead LeadNew : LeadNewList){

lead leadOld = leadOld(leadNew.Id);

if ((Trigger.isInsert == true && leadNew.Email == null) || (Trigger.isUpdate == true && leadNew.Email == null) ){
return;
}


for ( Contact contacts : [select Applicant_Email__c FROM Contact where email = :LeadNew.Email]){

if (Trigger.isInsert == true && leadNew.Email == contacts.Applicant_Email__c){
System.debug ('::Lead_EmailDuplicatepreventer::==>1');
LeadNew.Email.addError('Email already in use');
}
else if (Trigger.isUpdate == true && ((leadNew.Email != leadOld.Email && leadNew.Email == contacts.Applicant_Email__c) || (leadOld.Email == null && leadNew.Email == contacts.Applicant_Email__c))){
System.debug ('::Lead_EmailDuplicatepreventer::==>2');
LeadNew.Email.addError('Email already in use');
}

}
}
}

}

 

Test Class:


@IsTest
private class Lead_EmailDuplicatePreventerTest {
public static final Country__c country = [select Id, Name from Country__c limit 1];

@IsTest
public static void test() {

Contact contacts = new Contact();
contacts.LastName = 'TestContact';
contacts.Applicant_Email__c = 'TestContact@test.com';
insert contacts;
System.debug('::Lead_EmailDuplicatePreventerTest::contact==>'+contacts.Applicant_Email__c);

Lead lead = new Lead();
lead.FirstName = 'FirstName1';
lead.LastName = 'LastName1';
lead.Company = 'Company1';
lead.Email = 'LeadContact@test.com';
lead.LeadSource = 'Alumni';
lead.Status = 'Contacted';
lead.Country_Of_Citizenship__c = country.id;
lead.Country_Of_Residence__c = country.id;
insert lead;
System.debug('::Lead_EmailDuplicatePreventerTest::contact==>'+lead.Applicant_Email__c);

lead.Email = 'TestContact@test.com';
update lead;

lead.Email = '';
update lead;

lead.Email = 'lead1@test.com';
update lead;

lead.Email = 'TestContact@test.com';
update lead;


}
}

I also tried using assertions which are failing.

 

 

 

error

 

Hello,

 

Please help me on this trigger !!!

The trigger is on Lead and needs to fire only when

1. A checkbox field (Convert__c ) in Lead in selected.

2. The custom object (Obj2__c) has only 3 similar fields of Leads i.e Compnany__c, Date__c and Location__c  which needs to  be inserted or updated.

 

I wrote the below trigger, it is working good but it creates duplicates whenver a record is updated. Can anyone please help me in showing how to avoid duplicates. I am also not sure if I have to use before insert or after insert.

 

CODE:


trigger abc  on Lead (after insert, after update) // Not sure if I can use before or after 
{

    List<Obj2__c>  list  = new List <Obj2__c> {};
         
        for (Lead lead : trigger.new)
        {
        
            if (lead.Convert_c == true) 
            {
            
            list .add(new Obj2__c 

        (Company__c = lead.Company,

         Date__c = lead.Date__c,  

         Location__c = lead.Location__c) );
            
            }
        }
    insert list;
}

 

Thanks for your help.

Hi,
 
I have ten picklist fields and based on whatever is chosen here, I want the formula field to represent the sum of these ten fields.
 
Relationship score = 1,2,3,4 based on the option they chose
Geographic Alignment score = 1,2,3,4 based on the option they chose
Outsourcing savvy =1,2,3,4 based on the option they chose
 
Bid score = Relationship score + Geographic Alignment score + Outsourcing Savvy score
 
There are aseven other fields.can someone give me the syntax for these three above fields?
 
Thanks
John
 
  • October 30, 2008
  • Like
  • 0