+ Start a Discussion
KD TejaKD Teja 

Create a row in Custom Object whenever a new contact created

Hello Everyone,

Please help me out from creating a new row in custom object whenever a new contact created in contact object.

1.I have a custom date field in contact object
2.I shouldn't have any relationship with new contact->custom object.
3.Whenever i create new contact with some date it should create a row with "contact name" and "date"
4.Whenever i change Date on an existing contact it should create another row with diff date in custom object.
5.I have a lookup field in custom object with Account Object.

Plesae help how to make this work using triggers

Thanks in Advance.
Best Answer chosen by KD Teja
RKSalesforceRKSalesforce
Hi Teja,

I am assuming you have a custom Object : Custom_Object__c with Fields Name, Account__c.
Please find below trigger code to achieve your requirement:
Trigger customRowGenerator on Contact(After Insert, After Update){
    
	List<Custom_Object__c> custObjList = New List<Custom_Object__c>();
    If(Trigger.IsInsert || Trigger.IsUpdate){
        For(Contact con: Trigger.New){
			Custom_Object__c custObjRecord = New Custom_Object__c();
            custObjRecord.Name = con.FirstNAme+ con.LastName + con.LastModifiedDate;
			custObjRecord.Account__c = con.AccountId;
			custObjList.add(custObjRecord);	
        }
    }     
     
    try{
        Insert custObjList;
    }
     Catch(Exception E){
        System.Debug('Error Message: ' + e.getMessage());
    }
}

PLease mark as best answer if helped.

Regards,
Ramakant

All Answers

RKSalesforceRKSalesforce
Hi Teja,

I am assuming you have a custom Object : Custom_Object__c with Fields Name, Account__c.
Please find below trigger code to achieve your requirement:
Trigger customRowGenerator on Contact(After Insert, After Update){
    
	List<Custom_Object__c> custObjList = New List<Custom_Object__c>();
    If(Trigger.IsInsert || Trigger.IsUpdate){
        For(Contact con: Trigger.New){
			Custom_Object__c custObjRecord = New Custom_Object__c();
            custObjRecord.Name = con.FirstNAme+ con.LastName + con.LastModifiedDate;
			custObjRecord.Account__c = con.AccountId;
			custObjList.add(custObjRecord);	
        }
    }     
     
    try{
        Insert custObjList;
    }
     Catch(Exception E){
        System.Debug('Error Message: ' + e.getMessage());
    }
}

PLease mark as best answer if helped.

Regards,
Ramakant
This was selected as the best answer
KD TejaKD Teja
Hello Ramakant,

It's working great. Thanks once again.

Regards,
Teja