trigger Update_Position
on Candidate__c (before update, before insert) {
list<Id> Ids = new
list<Id>();
list<Candidate__c>
cIds = new list<Candidate__c>();
for(Candidate__c c :
trigger.new) {
if(c.Position__c != null)
{
Ids.add(c.Position__c);
cIds.add(c); // The
ids of keyword objects are here.
System.debug(‘Ids.size()
is’+ Ids.size()+’cIDs is ‘+cIds[0].id);
}
}
list<Position__c> p
=[select id, name from Position__c where id IN:Ids];
for(integer
i=0;i<Ids.size();i++){
p[i].Candidate__c =
cIds[i].id;
}
upsert(p);
}
==============================================================
@isTest
private class
Update_Position_Test{
static testMethod void
verify_Candidate() {
Position__c p = new
Position__c();p.name = ‘Dummy position’;
p.Min_Pay__c=100;
p.Max_Pay__c=500;
upsert(p);
Candidate__c candidate =
[select id, name from Candidate__c where name =:'Dummy Candidate'];
candidate.position__c = p.id;
system.debug(‘p.id is ‘
+p.id);
test.startTest();
upsert candidate; // The
trigger gets called here.
test.stopTest();
Position__c positions =
[select id,name, Candidate__c from position__c where name =: 'Dummy position'];
System.assertEquals(candidate.id,positions.Candidate__c);
}
}
No comments:
Post a Comment