You need to sign in to do that
Don't have an account?
Azusfdc
I want when account is created automatically contact should creates using with invoking trigger to class below is code i tried any suggestions?
public class contactnameupdate { public static void accountmethod(list<account> acclist) { string s='http://tekslate.com/15-sample-triggers-different-scenarios/'; for(account a:acclist) { a.description=s; } } public static void contactmethod(list<contact> conlist) { list<account> accconlist=new list<account>(); for(contact c:conlist) { accconlist.add(new account(id=c.accountid,name=c.lastname)); } } }
trigger insertcontact on Account (before insert,after insert,after update) { if(trigger.isbefore && trigger.isinsert) { contactnameupdate.accountmethod(trigger.new); } if(trigger.isafter && trigger.isinsert) { //contactnameupdate.contactmethod(trigger.new); } }
I've modified your code and now it should work.
Please find the below Trigger and Class:
Modified the code to follow best practices and minor syntax issue.
Please do let me know if it helps you.
Regards,
Mahesh
Please take the latest code which I tested in my DE environment and working properly with all scenarios.
Please do let me know if it helps you.
Regards,
Mahesh
Try this
Trigger :
trigger AccountTrigger on Account (after insert, after update)
{
Trigger.isInsert && Trigger.isAfter)
{
ContactHandler.insertContact(Trigger.new);
}
if(Trigger.isUpdate && Trigger.isAfter)
{
ContactHandler.updateContact(Trigger.new);
}
}
Class Handler :
public class ContactHandler{
public static void insertContact(List<Account> accList)
{
try
{
System.debug('Insert Contact Method');
Map <String, Account> contatctNameVSAccount = new Map<String, Account>();
List<Contact> contList = new List<Contact>();
for(Account acc : accList)
{
Contact contObj = new Contact();
contObj.Lastname = acc.Name;
contObj.AccountId = acc.Id;
contatctNameVSAccount.put(contObj.LastName, acc);
contList.add(contObj);
}
insert contList;
System.debug('contList ' +contList);
}
catch(Exception e)
{
System.debug('Exception ' +e);
System.debug('Exception line ' +e.getLineNumber());
System.debug('Exception message ' +e.getMessage());
System.debug('Exception stack trace string ' +e.getStackTraceString());
System.debug('Exception type name ' +e.getTypeName());
}*/
}
/*public static void updateContact(List<Account> accList)
{
try
{
List<Id> accId = new List<Id>();
for(Account acc : accList)
{
accId.add(acc.Id);
}
List<Contact> contList = [SELECT Id,Name, Update_of_Contact__c FROM Contact WHERE AccountId in: accId];
for(Contact ac : contList)
{
//Update_of_Contact__c coustom field to check how many time contact update
if(ac.Update_of_Contact__c == null)
{
ac.Update_of_Contact__c = 1;
}
else
ac.Update_of_Contact__c = ac.Update_of_Contact__c+1;
}
update contList;
System.debug('contList '+contList);
}
catch(Exception e)
{
System.debug('Exception ' +e);
System.debug('Exception line ' +e.getLineNumber());
System.debug('Exception message ' +e.getMessage());
System.debug('Exception stack trace string ' +e.getStackTraceString());
System.debug('Exception type name ' +e.getTypeName());
}
}
}*/
}
Please do let me known if it helps you
Payal
"Try this code."
---Trigger ---apex class If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi