+ Start a Discussion
GasparGaspar 

Apex Trigger Help - Object Converter App

Hello, I need help writing a simple trigger that runs the Object Converter App without having to use the detail page button. The event would be when the Lead is converted and creates the Account, the trigger would continue the process (after insert) and convert the Account to custom object Organisation_c based on the AccntToOrg field map.  

 

The "Convert to Organisation" custom detail page button currently executes the following URL: 

 

/apex/objcnvtr__objcnvtr_smartengine?id={!Account.Id}&mid=AccntToOrg

 

The trigger code suggested by the Object Converter App is: 

 

global static sObject OBJCNVTR__SmartEngine.Convert(sObject oSObject, string s

trMID){}

 

So: global static sObject OBJCNVTR__SmartEngine.Convert(sObject Account, string AccntToOrg){}

 

When do I define the Target object? What else do I need to run this process?

 

Also, can this be achieved without a trigger?

 

Thanks in advance for your help!

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hey Gaspar,

 

Try this code this is working fine :-

 

trigger ObjectCnvrt on Account (after insert) {

List<Id> accIds = new List<Id>();
for (Account acc : Trigger.new){
accIds.add(acc.id);
}

List<Account> accList = [select name, OwnerId from Account where id in :accIds];
List<Organisation__c> ocList = new List<Organisation__c>();

for(Account a : accList){

Organisation__c oc = new Organisation__c( name=a.name, OwnerId=a.OwnerId);
//Populate all the mandatory fields for Organisation__c to insert a record
ocList.add(oc);


}

insert ocList;


}


The thing is you have to declare accIds before the Trigger.new.

All Answers

Vinit_KumarVinit_Kumar

You can have after insert Trigger on Account and query the fields based on id and then you can populate the custom object  and insert a record .Please try the below code:-

 

for (Account acc : Trigger.new){

accIds.add(acc.id);

 

}

 

List<Account> accList = [select name,BillingCity,BiilinCountry,OwnerId from Account where id in :accIds];

List<Organisation_c> ocList =  new List<Organisation_c>();

 

for(Account a : accList){

 

Organisation_c oc = new Organisation_c(name=a.name,OwnerId=a.OwnerId,BillingCity=a.BillingCity,------------);//Populate all the mandatory fields for Organisation__c to insert a record

ocList.add(oc);

 

 

}

 

insert ocList;

GasparGaspar

Hey Vinit,

 

Thanks so much for your help. I am getting a compile error: "Compile Error: Variable does not exist: accIds" on line List<Account> accList = [select name,BillingCity,BiilinCountry,OwnerId from Account where id in :accIds];

 

Any ideas?

Vinit_KumarVinit_Kumar

Hey Gasper,

 

Please declare a list of Id as accIds:-

 

List<Id> accIds = new List<Id>();

GasparGaspar
Hey Vinit, the trigger is now active but I am getting the following error when converting the lead. The trigger is set to After Insert - should it be After Update?

Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ObjectCnvrt: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.ObjectCnvrt: line 4, column 1: [] (System Code)
GasparGaspar
This is the current code:

trigger ObjectCnvrt on Account (after insert) {

for (Account acc : Trigger.new){
accIds.add(acc.id);


}
List<Id> accIds = new List<Id>();
List<Account> accList = [select name, OwnerId from Account where id in :accIds];
List<Organisations__c> ocList = new List<Organisations__c>();

for(Account a : accList){

Organisations__c oc = new Organisations__c( name=a.name, OwnerId=a.OwnerId);
//Populate all the mandatory fields for Organisation__c to insert a record
ocList.add(oc);


}

insert ocList;

}
Vinit_KumarVinit_Kumar

Hey Gaspar,

 

Try this code this is working fine :-

 

trigger ObjectCnvrt on Account (after insert) {

List<Id> accIds = new List<Id>();
for (Account acc : Trigger.new){
accIds.add(acc.id);
}

List<Account> accList = [select name, OwnerId from Account where id in :accIds];
List<Organisation__c> ocList = new List<Organisation__c>();

for(Account a : accList){

Organisation__c oc = new Organisation__c( name=a.name, OwnerId=a.OwnerId);
//Populate all the mandatory fields for Organisation__c to insert a record
ocList.add(oc);


}

insert ocList;


}


The thing is you have to declare accIds before the Trigger.new.

This was selected as the best answer
GasparGaspar
Amazing, it worked! Thanks so much!!!
GasparGaspar
One more thing! I need a test class for this, what do you think?

@isTest
private static void Test_ObjectCnvrt() {

//Create Account record
Account = new Account();
Account.Name = 'Test Account';
INSERT Account;

//Create Organisation record
Organisations__c objOrganisation = new Organisations__c();
objOrganisation.Name = 'Test Organisation';
INSERT objOrganisation;



Now what??
Vinit_KumarVinit_Kumar

Hey Gaspar,

 

Try below :-

 

@IsTest(SeeAllData=true)

public class ObjectCnvrt_test{

 

      static testmethod void MyUnittest(){

       Account acc=  new Account(name='Test Account');

       insert acc;

 

}

}

Vinit_KumarVinit_Kumar

My Bad,

 

Modify as below :-

 

@IsTest(SeeAllData=true)

public class ObjectCnvrt_test{

 

      static testmethod void MyUnittest(){

 

       User u = [select id from User where name=<some name>];

 

       Account acc=  new Account(name='Test Account',OwnerId=u.id);

       insert acc;

 

}

}