• karmagedon
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies

trigger Hours on Hours_Professional__c(after insertafter updateafter delete)

{

Hours_Professional__c newTransaction;

 

if (Trigger.isInsert || Trigger.isUpdate)

 newTransaction =  System.Trigger.new[0];

 

if (Trigger.isDelete)

 newTransaction = System.Trigger.old[0];

 

  Technical_Assistance__c a ;

 a = [select id, Total_Hours_Professional__c from Technical_Assistance__c where id =

 :newTransaction.Technical_Assistance__c limit 1];

 

if (newtransaction.Total_Hours_Professional__c!= 0) {

if (Trigger.isInsert) {

if (a.Total_Hours_Professional__c== null) a.Total_Hours_Professional__c= 0;

 if (Trigger.isInsert && System.Trigger.new[0].Total_Hours_Professional__c!= null) {

  a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;

 }

else {

 Hours_Professional__c oldTransaction =  System.Trigger.old[0];

 if (Trigger.isUpdate) {

   if (System.Trigger.new[0].Total_Hours_Professional__c!= null)

     a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;

   if (System.Trigger.old[0].Total_Hours_Professional__c!= null)

     a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;

 }

 if (Trigger.isDelete && System.Trigger.old[0].Total_Hours_Professional__c!= null) {

  a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;

 }

}

update a;

 

}

}

 

-----------------------------------------------------

31% coverage 

Assertion Failed: Expected: 0, Actual: 10000

 

 

public class Hours {

static testmethod void testHours() {

 

Technical_Assistance__c [] a =new Technical_Assistance__c  [] {

new Technical_Assistance__c (Name= 'Test12', Total_Hours_Professional__c=0, Equipment_Installed__c='02iA0000000LhK3')

};

insert a;

 

Hours_Professional__c[] tr1 =new Hours_Professional__c[]{

new Hours_Professional__c(Technical_Assistance__c =a[0].id, Amount__c =1 )

};

 

insert tr1;

 

Technical_Assistance__c [] acctQuery11 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery11[0].Total_Hours_Professional__c,10000) ;

 

 

tr1[0]. Amount__c = 2;

 

update tr1;

 

Technical_Assistance__c [] acctQuery12 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery12[0].Total_Hours_Professional__c,20000) ;

 

delete tr1;

 

Technical_Assistance__c [] acctQuery13 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery13[0].Total_Hours_Professional__c,0) ;

}

}

trigger Hours on Hours_Professional__c(after insert, after update, after delete)

{

Hours_Professional__c newTransaction;

 

if (Trigger.isInsert || Trigger.isUpdate)

 newTransaction =  System.Trigger.new[0];

 

if (Trigger.isDelete)

 newTransaction = System.Trigger.old[0];

 

  Technical_Assistance__c a ;

 a = [select id, Total_Hours_Professional__c from Technical_Assistance__c where id =

 :newTransaction.Technical_Assistance__c limit 1];

 

if (newtransaction.Total_Hours_Professional__c!= 0) {

if (Trigger.isInsert) {

if (a.Total_Hours_Professional__c== null) a.Total_Hours_Professional__c= 0;

 if (Trigger.isInsert && System.Trigger.new[0].Total_Hours_Professional__c!= null) {

  a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;

 }

} else {

 Hours_Professional__c oldTransaction =  System.Trigger.old[0];

 if (Trigger.isUpdate) {

   if (System.Trigger.new[0].Total_Hours_Professional__c!= null)

     a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;

   if (System.Trigger.old[0].Total_Hours_Professional__c!= null)

     a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;

 }

 if (Trigger.isDelete && System.Trigger.old[0].Total_Hours_Professional__c!= null) {

  a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;

 }

}

update a;

 

}

}

 

________________________________________________

 

How to do the best, which was my mistake in my own creation (My Frankenstein)

 

public class Hours {

static testmethod void testHours() {

 

Technical_Assistance__c [] a =new Technical_Assistance__c  [] {

new Technical_Assistance__c (Name= 'Test12', Total_Hours_Professional__c=0, Equipment_Installed__c='02iA0000000LhK3')

};

insert a;

 

Hours_Professional__c[] tr1 =new Hours_Professional__c[]{

new Hours_Professional__c(Technical_Assistance__c =a[0].id, Amount__c =1 )

};

 

insert tr1;

 

Technical_Assistance__c [] acctQuery11 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery11[0].Total_Hours_Professional__c,10000) ;

 

 

tr1[0]. Amount__c = 2;

 

update tr1;

 

Technical_Assistance__c [] acctQuery12 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery12[0].Total_Hours_Professional__c,20000) ;

 

delete tr1;

 

Technical_Assistance__c [] acctQuery13 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery13[0].Total_Hours_Professional__c,0) ;

}

}

 

----------------------------------------------------------------------------------------------------------

Error

 

Class.Hours.testHours: line 7, column 1 External entry point

 

19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO

12:33:49.549|EXECUTION_STARTED

12:33:49.549|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000009Grn|Hours.testHours

12:33:49.549|LIMIT_USAGE_FOR_NS|[4]|SCRIPT_STATEMENTS|1|200000

12:33:49.549|LIMIT_USAGE_FOR_NS|[7]|SCRIPT_STATEMENTS|2|200000

12:33:49.549|DML_BEGIN|[7]|Op:Insert|Type: Technical_Assistance__c |Rows:1

12:33:49.549|LIMIT_USAGE_FOR_NS|[7]|DML|1|100

12:33:49.549|LIMIT_USAGE_FOR_NS|[7]|DML_ROWS|1|10000

12:33:49.578|DML_END|[7]

12:33:49.579|EXCEPTION_THROWN|[7]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

12:33:49.579|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

 

Class.Hours.testHours: line 7, column 1

External entry point

12:33:49.580|CUMULATIVE_LIMIT_USAGE

12:33:49.580|LIMIT_USAGE_FOR_NS|(default)|

  Number of SOQL queries: 0 out of 100

  Number of query rows: 0 out of 10000

  Number of SOSL queries: 0 out of 20

  Number of DML statements: 1 out of 100

  Number of DML rows: 1 out of 10000

  Number of script statements: 2 out of 200000

  Maximum heap size: 0 out of 3000000

  Number of callouts: 0 out of 10

  Number of Email Invocations: 0 out of 10

  Number of fields describes: 0 out of 100

  Number of record type describes: 0 out of 100

  Number of child relationships describes: 0 out of 100

  Number of picklist describes: 0 out of 100

  Number of future calls: 0 out of 10

  Number of find similar calls: 0 out of 10

  Number of System.runAs() invocations: 0 out of 20

 

12:33:49.580|CUMULATIVE_LIMIT_USAGE_END

 

12:33:49.580|CODE_UNIT_FINISHED|Hours.testHours

12:33:49.580|EXECUTION_FINISHED

"The Class"

 

public class KarmagedonHoras {
  

static testmethod void KarmagedonHoras() {

Asistencia_Tecnica__c[] a =new Asistencia_Tecnica__c [] {
new Asistencia_Tecnica__c(Name= 'Prueba12', Total_Horas_Profesionales__c=0, Equipo_Instalado__c='02iA0000000LhK3')
};
insert a;

User[] acctQuery10 = [SELECT Valor_Hora_Profesional__c FROM User WHERE User.id ='005A0000000i5tN'];

Horas_Profesionales__c[] tr1 =new Horas_Profesionales__c[]{
new Horas_Profesionales__c(Asistencia_Tecnica__c=a[0].id, Cantidad__c =1 )
};
insert tr1;

Asistencia_Tecnica__c[] acctQuery11 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery11[0].Total_Horas_Profesionales__c , 1000) ; 


tr1[0]. Cantidad__c = 2;

update tr1;

Asistencia_Tecnica__c[] acctQuery12 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery12[0].Total_Horas_Profesionales__c ,2000) ; 

delete tr1;

Asistencia_Tecnica__c[] acctQuery13 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery13[0].Total_Horas_Profesionales__c ,0) ;
}
}

 

"The Trigger"

 

trigger TriggerKarmagedonHoras on Horas_Profesionales__c(after insert, after update, after delete) 
{
Horas_Profesionales__c newTransaction;

if (Trigger.isInsert || Trigger.isUpdate)
 newTransaction =  System.Trigger.new[0];

if (Trigger.isDelete)
 newTransaction = System.Trigger.old[0];
 
  Asistencia_Tecnica__c a ;
 a = [select id, Total_Horas_Profesionales__c from Asistencia_Tecnica__c where id = 
 :newTransaction.Asistencia_Tecnica__c limit 1];

if (newtransaction.Total_Horas_Profesionales__c!= 0) {
if (Trigger.isInsert) {
if (a.Total_Horas_Profesionales__c== null) a.Total_Horas_Profesionales__c= 0;
 if (Trigger.isInsert && System.Trigger.new[0].Total_Horas_Profesionales__c!= null) {
  a.Total_Horas_Profesionales__c+= newtransaction.Total_Horas_Profesionales__c;
 }
} else {
 Horas_Profesionales__c oldTransaction =  System.Trigger.old[0];
 if (Trigger.isUpdate) {
   if (System.Trigger.new[0].Total_Horas_Profesionales__c!= null)
     a.Total_Horas_Profesionales__c+= newtransaction.Total_Horas_Profesionales__c;
   if (System.Trigger.old[0].Total_Horas_Profesionales__c!= null)
     a.Total_Horas_Profesionales__c-= oldtransaction.Total_Horas_Profesionales__c;
 }
 if (Trigger.isDelete && System.Trigger.old[0].Total_Horas_Profesionales__c!= null) { 
  a.Total_Horas_Profesionales__c-= oldtransaction.Total_Horas_Profesionales__c;
 }
}
update a;


}

 

-------------------------------------------------------

My First Post

Error

 

Error | Line 20 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

Error | Line 29 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

Error | Line 35 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

 

Cueck

trigger Hours on Hours_Professional__c(after insert, after update, after delete)

{

Hours_Professional__c newTransaction;

 

if (Trigger.isInsert || Trigger.isUpdate)

 newTransaction =  System.Trigger.new[0];

 

if (Trigger.isDelete)

 newTransaction = System.Trigger.old[0];

 

  Technical_Assistance__c a ;

 a = [select id, Total_Hours_Professional__c from Technical_Assistance__c where id =

 :newTransaction.Technical_Assistance__c limit 1];

 

if (newtransaction.Total_Hours_Professional__c!= 0) {

if (Trigger.isInsert) {

if (a.Total_Hours_Professional__c== null) a.Total_Hours_Professional__c= 0;

 if (Trigger.isInsert && System.Trigger.new[0].Total_Hours_Professional__c!= null) {

  a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;

 }

} else {

 Hours_Professional__c oldTransaction =  System.Trigger.old[0];

 if (Trigger.isUpdate) {

   if (System.Trigger.new[0].Total_Hours_Professional__c!= null)

     a.Total_Hours_Professional__c+= newtransaction.Total_Hours_Professional__c;

   if (System.Trigger.old[0].Total_Hours_Professional__c!= null)

     a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;

 }

 if (Trigger.isDelete && System.Trigger.old[0].Total_Hours_Professional__c!= null) {

  a.Total_Hours_Professional__c-= oldtransaction.Total_Hours_Professional__c;

 }

}

update a;

 

}

}

 

________________________________________________

 

How to do the best, which was my mistake in my own creation (My Frankenstein)

 

public class Hours {

static testmethod void testHours() {

 

Technical_Assistance__c [] a =new Technical_Assistance__c  [] {

new Technical_Assistance__c (Name= 'Test12', Total_Hours_Professional__c=0, Equipment_Installed__c='02iA0000000LhK3')

};

insert a;

 

Hours_Professional__c[] tr1 =new Hours_Professional__c[]{

new Hours_Professional__c(Technical_Assistance__c =a[0].id, Amount__c =1 )

};

 

insert tr1;

 

Technical_Assistance__c [] acctQuery11 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery11[0].Total_Hours_Professional__c,10000) ;

 

 

tr1[0]. Amount__c = 2;

 

update tr1;

 

Technical_Assistance__c [] acctQuery12 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery12[0].Total_Hours_Professional__c,20000) ;

 

delete tr1;

 

Technical_Assistance__c [] acctQuery13 = [SELECT Total_Hours_Professional__c FROM Technical_Assistance__c  WHERE Name ='Test12'];

 

System.assertEquals(acctQuery13[0].Total_Hours_Professional__c,0) ;

}

}

 

----------------------------------------------------------------------------------------------------------

Error

 

Class.Hours.testHours: line 7, column 1 External entry point

 

19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO

12:33:49.549|EXECUTION_STARTED

12:33:49.549|CODE_UNIT_STARTED|[EXTERNAL]|01pT00000009Grn|Hours.testHours

12:33:49.549|LIMIT_USAGE_FOR_NS|[4]|SCRIPT_STATEMENTS|1|200000

12:33:49.549|LIMIT_USAGE_FOR_NS|[7]|SCRIPT_STATEMENTS|2|200000

12:33:49.549|DML_BEGIN|[7]|Op:Insert|Type: Technical_Assistance__c |Rows:1

12:33:49.549|LIMIT_USAGE_FOR_NS|[7]|DML|1|100

12:33:49.549|LIMIT_USAGE_FOR_NS|[7]|DML_ROWS|1|10000

12:33:49.578|DML_END|[7]

12:33:49.579|EXCEPTION_THROWN|[7]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

12:33:49.579|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

 

Class.Hours.testHours: line 7, column 1

External entry point

12:33:49.580|CUMULATIVE_LIMIT_USAGE

12:33:49.580|LIMIT_USAGE_FOR_NS|(default)|

  Number of SOQL queries: 0 out of 100

  Number of query rows: 0 out of 10000

  Number of SOSL queries: 0 out of 20

  Number of DML statements: 1 out of 100

  Number of DML rows: 1 out of 10000

  Number of script statements: 2 out of 200000

  Maximum heap size: 0 out of 3000000

  Number of callouts: 0 out of 10

  Number of Email Invocations: 0 out of 10

  Number of fields describes: 0 out of 100

  Number of record type describes: 0 out of 100

  Number of child relationships describes: 0 out of 100

  Number of picklist describes: 0 out of 100

  Number of future calls: 0 out of 10

  Number of find similar calls: 0 out of 10

  Number of System.runAs() invocations: 0 out of 20

 

12:33:49.580|CUMULATIVE_LIMIT_USAGE_END

 

12:33:49.580|CODE_UNIT_FINISHED|Hours.testHours

12:33:49.580|EXECUTION_FINISHED

"The Class"

 

public class KarmagedonHoras {
  

static testmethod void KarmagedonHoras() {

Asistencia_Tecnica__c[] a =new Asistencia_Tecnica__c [] {
new Asistencia_Tecnica__c(Name= 'Prueba12', Total_Horas_Profesionales__c=0, Equipo_Instalado__c='02iA0000000LhK3')
};
insert a;

User[] acctQuery10 = [SELECT Valor_Hora_Profesional__c FROM User WHERE User.id ='005A0000000i5tN'];

Horas_Profesionales__c[] tr1 =new Horas_Profesionales__c[]{
new Horas_Profesionales__c(Asistencia_Tecnica__c=a[0].id, Cantidad__c =1 )
};
insert tr1;

Asistencia_Tecnica__c[] acctQuery11 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery11[0].Total_Horas_Profesionales__c , 1000) ; 


tr1[0]. Cantidad__c = 2;

update tr1;

Asistencia_Tecnica__c[] acctQuery12 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery12[0].Total_Horas_Profesionales__c ,2000) ; 

delete tr1;

Asistencia_Tecnica__c[] acctQuery13 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery13[0].Total_Horas_Profesionales__c ,0) ;
}
}

 

"The Trigger"

 

trigger TriggerKarmagedonHoras on Horas_Profesionales__c(after insert, after update, after delete) 
{
Horas_Profesionales__c newTransaction;

if (Trigger.isInsert || Trigger.isUpdate)
 newTransaction =  System.Trigger.new[0];

if (Trigger.isDelete)
 newTransaction = System.Trigger.old[0];
 
  Asistencia_Tecnica__c a ;
 a = [select id, Total_Horas_Profesionales__c from Asistencia_Tecnica__c where id = 
 :newTransaction.Asistencia_Tecnica__c limit 1];

if (newtransaction.Total_Horas_Profesionales__c!= 0) {
if (Trigger.isInsert) {
if (a.Total_Horas_Profesionales__c== null) a.Total_Horas_Profesionales__c= 0;
 if (Trigger.isInsert && System.Trigger.new[0].Total_Horas_Profesionales__c!= null) {
  a.Total_Horas_Profesionales__c+= newtransaction.Total_Horas_Profesionales__c;
 }
} else {
 Horas_Profesionales__c oldTransaction =  System.Trigger.old[0];
 if (Trigger.isUpdate) {
   if (System.Trigger.new[0].Total_Horas_Profesionales__c!= null)
     a.Total_Horas_Profesionales__c+= newtransaction.Total_Horas_Profesionales__c;
   if (System.Trigger.old[0].Total_Horas_Profesionales__c!= null)
     a.Total_Horas_Profesionales__c-= oldtransaction.Total_Horas_Profesionales__c;
 }
 if (Trigger.isDelete && System.Trigger.old[0].Total_Horas_Profesionales__c!= null) { 
  a.Total_Horas_Profesionales__c-= oldtransaction.Total_Horas_Profesionales__c;
 }
}
update a;


}

 

-------------------------------------------------------

My First Post

Error

 

Error | Line 20 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

Error | Line 29 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

Error | Line 35 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

 

Cueck