+ Start a Discussion
koti91koti91 

Trigger on Insert adn update help plzzzz

Hi

 

I am new to salesforce and i need some help in writing a trigger

 

Object A and Object B . Object B has a look up for Object A

 

when ever record is inserted in to object A i need to create records in object B( Condition is: I have a start date and end date on object A . Number  of records need to be created are Start Date - End Date +1 on object B with each record having a date.ex: Stat Date- 3/12, End Date - 7/12 then i need to create 5 records on object B. with Date field populated.

Best Answer chosen by Admin (Salesforce Developers) 
Henry AkpalaHenry Akpala

Your requirement is not very clear.  Is the EndDate on ObjectA set at the time of creation or later?  If it is at the time of creation,

Then this is what you need to do in your trigger on ObjectA

 

trigger ObjectATrigger on ObjectA (after insert){

 

List<Contact> aList  = new List<Contact>();

 

for (Challenge__c a : Trigger.new){

DateTime stDate = a.Start_Date__c;

DateTime edDate = a.End_Date__c;

Integer i = 0;

edDate = edDate.addDays(1);

while(edDate > stDate){

aList.add(new Contact(firstname='FirstName', lastname='LastName'));

 

stDate = stDate.addDays(1);

i++;

System.debug('*******' + i);

}

 

}

 

//TODO insert for aList

}

 

hope this helps. 

-H