function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nani812nani812 

Insert Multiple Related Objects

Hi,

 

I have a custom object Frame_Agreements__c with two lookupfields(Account, Contract) to Account and Contract. I have written a trigger to insert the records in Frame_Agreements__c as soon as new contract is created. But its not working...Can anyone help me..

 

 

trigger ConRelate on Contract (after insert, after update){
//List<Contract> listContracts=new List<Contract>;
List<Frame_Agreement__c> Lac = new List<Frame_Agreement__c>();
Set<ID> Cid = new Set<ID>();
for(Contract ctr : Trigger.New)
{
CId.add(ctr.Id);
}
List<Contract> cont = [select ContractNumber,Contract.Account.Name from contract where Id in :CId];

for (Contract fContract : cont) {

Frame_Agreement__c fac = new Frame_Agreement__c();
//fac.Name = fContract.Name;
//fac.Id = fContract.Id;
fac.Account_Name__c= fContract.Account.Id;
fac.Contract_Name__c=fContract.Id;

Lac.add(fac);
}

/*if (cont.isEmpty() == false) {
update cont;
}*/
}

 

David@DeinDealDavid@DeinDeal

Hello, 

 

I see a little flaw in your code, change this line: 

 

fac.Account_Name__c= fContract.Account.Id;

 

to this: 

 

fac.Account_Name__c= fContract.AccountId; (remove the dot between Account and Id). The reason is that the AccountId is a field itself you don't get to it using the dot notation. 

 

Don't forget to populate AccountId in the query. 

 

I hope this helps, 

 

David

 

 

nani812nani812

Hi David,

 

Thanks for your suggestion. But still i am not able to insert the record into that junction object.

Peter_sfdcPeter_sfdc

Your code is trying to update the contracts, instead of inserting the junction object records: 

 

Lac.add(fac);
}
/*if (cont.isEmpty() == false) {
insert Lac;  <--these are the records you want to create, right? 
}*/
}

 

nani812nani812

You are right Peter,  I want to insert Junction Object records.

 

Earlier I used the same code which you have suggested..but sameproblem.

 

 

Regards

nani