• furamag
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 20
    Replies
I am trying to create a testClass for my trigger.
Trigger:

Code:
trigger pd_check on SFDC_PTO_Request__c (before insert, before update) {

SFDC_PTO_Request__c[] opp = trigger.new;

String picklist_v = opp[0].Request_Type__c;

Double count_d_n = opp[0].Days_Off__c;

if (picklist_v == 'Personal days') {

String str = opp[0].Employee__c;
str = str.substring(0,15);

date myDate = opp[0].Request_Start_Date__c;
Integer month = myDate.month();
Integer year = myDate.year();

Double count_d;
Double count_d_b = 0;

Double del;
del = month / 3;
Date from_d;
Date to_d;


if(del <= 1) {
from_d = date.newInstance(year, 1, 1);
to_d = date.newInstance(year, 3, 31);
}
else if((del <= 2) && (del > 1)) {
from_d = date.newInstance(year, 4, 1);
to_d = date.newInstance(year, 6, 31);

}
else if((del <= 3) && (del > 2)) {
from_d = date.newInstance(year, 7, 1);
to_d = date.newInstance(year, 9, 31);
}
else if((del <= 4) && (del > 3)) {
from_d = date.newInstance(year, 10, 1);
to_d = date.newInstance(year, 12, 31);
}



for(SFDC_PTO_Request__c[] s : [SELECT Request_Start_Date__c, Request_End_Date__c, Days_Off__c FROM SFDC_PTO_Request__c WHERE Employee__c = :str AND Request_Type__c = 'Personal days' AND Request_Start_Date__c > :from_d AND Request_Start_Date__c < :to_d]) {


for(SFDC_PTO_Request__c c:s) {
Integer month_s = c.Request_Start_Date__c.month();
Integer month_e = c.Request_End_Date__c.month();
if ((month_s != month_e) && ((month_s / 3 == 1) || (month_s / 3 == 2) || (month_s / 3 == 3) || (month_s / 3 == 4)))
{
count_d_b += 1;
}
else
{
count_d_b += c.Days_Off__c;
}

}

}

count_d = count_d_b + count_d_n;

String count_d_b_t = ''+count_d_b+'';

String[] days = count_d_b_t.split('.');

if(count_d > 2) {

Trigger.new[0].OwnerId.addError('This employee already have 2 or more personal days! Every employee can have only 2 personal day\'s per quarter.');

}

}

else if(picklist_v == 'Vacation'){
//---//
String str = opp[0].Employee__c;
str = str.substring(0,15);

date myDate = opp[0].Request_Start_Date__c;
Integer month = myDate.month();
Integer year = myDate.year();

Double count_d;
Double count_d_b = 0;

Double del;
del = month / 3;
Date from_d;
Date to_d;


if(del <= 1) {
from_d = date.newInstance(year, 1, 1);
to_d = date.newInstance(year, 3, 31);
}
else if((del <= 2) && (del > 1)) {
from_d = date.newInstance(year, 4, 1);
to_d = date.newInstance(year, 6, 31);

}
else if((del <= 3) && (del > 2)) {
from_d = date.newInstance(year, 7, 1);
to_d = date.newInstance(year, 9, 31);
}
else if((del <= 4) && (del > 3)) {
from_d = date.newInstance(year, 10, 1);
to_d = date.newInstance(year, 12, 31);
}



for(SFDC_PTO_Request__c[] s : [SELECT Request_Start_Date__c, Request_End_Date__c, Days_Off__c FROM SFDC_PTO_Request__c WHERE Employee__c = :str AND Request_Type__c = 'Personal days' AND Request_Start_Date__c > :from_d AND Request_Start_Date__c < :to_d]) {


for(SFDC_PTO_Request__c c:s) {
Integer month_s = c.Request_Start_Date__c.month();
Integer month_e = c.Request_End_Date__c.month();
if ((month_s != month_e) && ((month_s / 3 == 1) || (month_s / 3 == 2) || (month_s / 3 == 3) || (month_s / 3 == 4)))
{
count_d_b += 1;
}
else
{
count_d_b += c.Days_Off__c;
}

}

}

count_d = count_d_b + count_d_n;

String count_d_b_t = ''+count_d_b+'';

String[] days = count_d_b_t.split('.');

if(count_d > 2) {

Trigger.new[0].OwnerId.addError('This employee already have 2 or more personal days! Every employee can have only 2 personal day\'s per quarter.');

}
//---//
}


}

 
Test class:
Code:
public class pd_check_test {

    public static testMethod void myUnitTest() {

    SFDC_PTO_Request__c opp = new SFDC_PTO_Request__c(Employee__c = 'a0A70000002lu6N', Request_End_Date__c = Date.newInstance(2009,01,08), Request_Start_Date__c = Date.newInstance(2009,01,01), Request_Type__c = 'Personal days', Details__c = '123456789', Date_of_Request__c = Date.newInstance(2008,12,01));

    insert opp;

    String id = opp.Employee__c;

    SFDC_PTO_Request__c[] s = [SELECT Details__c, Date_of_Request__c FROM SFDC_PTO_Request__c WHERE Employee__c = :id AND Request_Type__c = :opp.Request_Type__c AND Request_Start_Date__c = 2009-01-01 AND Request_End_Date__c = 2009-01-08 AND Details__c = '123456789' LIMIT 1];


Double count_d = 0.0;
Double count_d_b = 0.0;

Double del = 0.0;

   System.assertEquals('123456789', s[0].Details__c); 

    }
}

 My trigger only check how many days off have employee and write error if employee have 2 or more daysoff. It doesn't insert or update something. But when I try to deploy to server I can't do it, because I have only 38% coverage and I have error: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, This employee already have 2 or more personal days! Every employee can have only 2 personal day's per quarter.: [OwnerId]". Can somebody help me to write correct test class?

I have a trigger. My trigger update one field in custom object. This is code of trigger:
Code:
trigger Update on Promotions__c (after insert, after update) {

Promotions__c[] opp = trigger.new;
String title = opp[0].Title__c;



String id = opp[0].Employee__c;

SFDC_Employee__c s = [SELECT Title__c FROM SFDC_Employee__c WHERE ID = :id];

s.Title__c = title;

update s;

}

 I tested my trigger in development version of salesforce. It wirked correct. Now I want to install this trigger to real version of salesforce.
To check my trigger and transfer it to real version I use Eclipse. But I have two errors when I check my trigger:
Code:
cvc-complex-type.2.4.b: The content of element 'ApexTrigger' is not complete. One of '{"http://soap.sforce.com/2006/04/metadata":status}' is expected.
Save error: unexpected token: Update

Why I have this errors and why I can't save this trigger to server (trigger_name -> Force.com -> Save to Server).

I have two custom objects (SFDC_Employee__c Promotion__c). I need to update field Unit__c (text) in object SFDC_Employee__c when sombody add or change field Unit__c (Picklist) in object Promotions__c.

I made trigger in object Promotions__c with this code:

Code:
trigger Unit_update on Promotions__c (after insert, after update) {

sObject s = [SELECT Unit__c FROM SFDC_Employee__c LIMIT 1];

s.Unit__c = 'sdfsfsdf';

update s;

}


But this trigger doesn't work. Can you please explane what is the better way to fix my problem.
I am trying to create a testClass for my trigger.
Trigger:

Code:
trigger pd_check on SFDC_PTO_Request__c (before insert, before update) {

SFDC_PTO_Request__c[] opp = trigger.new;

String picklist_v = opp[0].Request_Type__c;

Double count_d_n = opp[0].Days_Off__c;

if (picklist_v == 'Personal days') {

String str = opp[0].Employee__c;
str = str.substring(0,15);

date myDate = opp[0].Request_Start_Date__c;
Integer month = myDate.month();
Integer year = myDate.year();

Double count_d;
Double count_d_b = 0;

Double del;
del = month / 3;
Date from_d;
Date to_d;


if(del <= 1) {
from_d = date.newInstance(year, 1, 1);
to_d = date.newInstance(year, 3, 31);
}
else if((del <= 2) && (del > 1)) {
from_d = date.newInstance(year, 4, 1);
to_d = date.newInstance(year, 6, 31);

}
else if((del <= 3) && (del > 2)) {
from_d = date.newInstance(year, 7, 1);
to_d = date.newInstance(year, 9, 31);
}
else if((del <= 4) && (del > 3)) {
from_d = date.newInstance(year, 10, 1);
to_d = date.newInstance(year, 12, 31);
}



for(SFDC_PTO_Request__c[] s : [SELECT Request_Start_Date__c, Request_End_Date__c, Days_Off__c FROM SFDC_PTO_Request__c WHERE Employee__c = :str AND Request_Type__c = 'Personal days' AND Request_Start_Date__c > :from_d AND Request_Start_Date__c < :to_d]) {


for(SFDC_PTO_Request__c c:s) {
Integer month_s = c.Request_Start_Date__c.month();
Integer month_e = c.Request_End_Date__c.month();
if ((month_s != month_e) && ((month_s / 3 == 1) || (month_s / 3 == 2) || (month_s / 3 == 3) || (month_s / 3 == 4)))
{
count_d_b += 1;
}
else
{
count_d_b += c.Days_Off__c;
}

}

}

count_d = count_d_b + count_d_n;

String count_d_b_t = ''+count_d_b+'';

String[] days = count_d_b_t.split('.');

if(count_d > 2) {

Trigger.new[0].OwnerId.addError('This employee already have 2 or more personal days! Every employee can have only 2 personal day\'s per quarter.');

}

}

else if(picklist_v == 'Vacation'){
//---//
String str = opp[0].Employee__c;
str = str.substring(0,15);

date myDate = opp[0].Request_Start_Date__c;
Integer month = myDate.month();
Integer year = myDate.year();

Double count_d;
Double count_d_b = 0;

Double del;
del = month / 3;
Date from_d;
Date to_d;


if(del <= 1) {
from_d = date.newInstance(year, 1, 1);
to_d = date.newInstance(year, 3, 31);
}
else if((del <= 2) && (del > 1)) {
from_d = date.newInstance(year, 4, 1);
to_d = date.newInstance(year, 6, 31);

}
else if((del <= 3) && (del > 2)) {
from_d = date.newInstance(year, 7, 1);
to_d = date.newInstance(year, 9, 31);
}
else if((del <= 4) && (del > 3)) {
from_d = date.newInstance(year, 10, 1);
to_d = date.newInstance(year, 12, 31);
}



for(SFDC_PTO_Request__c[] s : [SELECT Request_Start_Date__c, Request_End_Date__c, Days_Off__c FROM SFDC_PTO_Request__c WHERE Employee__c = :str AND Request_Type__c = 'Personal days' AND Request_Start_Date__c > :from_d AND Request_Start_Date__c < :to_d]) {


for(SFDC_PTO_Request__c c:s) {
Integer month_s = c.Request_Start_Date__c.month();
Integer month_e = c.Request_End_Date__c.month();
if ((month_s != month_e) && ((month_s / 3 == 1) || (month_s / 3 == 2) || (month_s / 3 == 3) || (month_s / 3 == 4)))
{
count_d_b += 1;
}
else
{
count_d_b += c.Days_Off__c;
}

}

}

count_d = count_d_b + count_d_n;

String count_d_b_t = ''+count_d_b+'';

String[] days = count_d_b_t.split('.');

if(count_d > 2) {

Trigger.new[0].OwnerId.addError('This employee already have 2 or more personal days! Every employee can have only 2 personal day\'s per quarter.');

}
//---//
}


}

 
Test class:
Code:
public class pd_check_test {

    public static testMethod void myUnitTest() {

    SFDC_PTO_Request__c opp = new SFDC_PTO_Request__c(Employee__c = 'a0A70000002lu6N', Request_End_Date__c = Date.newInstance(2009,01,08), Request_Start_Date__c = Date.newInstance(2009,01,01), Request_Type__c = 'Personal days', Details__c = '123456789', Date_of_Request__c = Date.newInstance(2008,12,01));

    insert opp;

    String id = opp.Employee__c;

    SFDC_PTO_Request__c[] s = [SELECT Details__c, Date_of_Request__c FROM SFDC_PTO_Request__c WHERE Employee__c = :id AND Request_Type__c = :opp.Request_Type__c AND Request_Start_Date__c = 2009-01-01 AND Request_End_Date__c = 2009-01-08 AND Details__c = '123456789' LIMIT 1];


Double count_d = 0.0;
Double count_d_b = 0.0;

Double del = 0.0;

   System.assertEquals('123456789', s[0].Details__c); 

    }
}

 My trigger only check how many days off have employee and write error if employee have 2 or more daysoff. It doesn't insert or update something. But when I try to deploy to server I can't do it, because I have only 38% coverage and I have error: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, This employee already have 2 or more personal days! Every employee can have only 2 personal day's per quarter.: [OwnerId]". Can somebody help me to write correct test class?

I have a trigger. My trigger update one field in custom object. This is code of trigger:
Code:
trigger Update on Promotions__c (after insert, after update) {

Promotions__c[] opp = trigger.new;
String title = opp[0].Title__c;



String id = opp[0].Employee__c;

SFDC_Employee__c s = [SELECT Title__c FROM SFDC_Employee__c WHERE ID = :id];

s.Title__c = title;

update s;

}

 I tested my trigger in development version of salesforce. It wirked correct. Now I want to install this trigger to real version of salesforce.
To check my trigger and transfer it to real version I use Eclipse. But I have two errors when I check my trigger:
Code:
cvc-complex-type.2.4.b: The content of element 'ApexTrigger' is not complete. One of '{"http://soap.sforce.com/2006/04/metadata":status}' is expected.
Save error: unexpected token: Update

Why I have this errors and why I can't save this trigger to server (trigger_name -> Force.com -> Save to Server).

I have two custom objects (SFDC_Employee__c Promotion__c). I need to update field Unit__c (text) in object SFDC_Employee__c when sombody add or change field Unit__c (Picklist) in object Promotions__c.

I made trigger in object Promotions__c with this code:

Code:
trigger Unit_update on Promotions__c (after insert, after update) {

sObject s = [SELECT Unit__c FROM SFDC_Employee__c LIMIT 1];

s.Unit__c = 'sdfsfsdf';

update s;

}


But this trigger doesn't work. Can you please explane what is the better way to fix my problem.