+ Start a Discussion
fatmonkfatmonk 

Trigger I need some help on trigger

I am a new bie to trigger. I would like to create a trigger for Contracts. Currently the object existing Object are Opportunity is a Master Relation of two Custom Object Estimate__c and Expense__c and it also has a look up relatioship with Contract.

 

Each Contract can have multiple Estimate__c and Expense__c.

Estimate__c, Expense__c and Contract can be created under Opportunity in an order. When the Contract is Create and new record, I would like to have (Expense and Estimate show under Contract related list because they all share Opportunity id.)

I don't how to insert the Contract.id into Expesen__c.Contract__c or Estimate__c.contract__c. 

 

Thanks so much.

Luong

 

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi,

 

As per as my understanding so far, try the following code -

 

trigger EstimateContract on Contract (after insert,after update)
{
    Map<id,id> Opid= new Map<id,id>();

    for(contract c:Trigger.New){
        if(c.Opportunity__c!=null){
            Opid.put(c.Opportunity__c,c.id);
        }
    }
    
    List<Estimate__c>  eToUpdate = new List<Estimate__c>();
    // query  Estimate__c.Contract__c  which share parent Opportunity id;  Estimate__c.Contract__c is a null
    for(Estimate__c e : [select id,contract__c,Opportunity__c,name from Estimate__c  where Opportunity__c IN:Opid.keySet()])
    {
      if(e.contract__c == null)
      {
           e.contract__c = Opid.get(e.Opportunity__c);
           eToUpdate.add(e);
      }
    }
    
    if(eToUpdate.size()>0)
        update eToUpdate;

}

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

Let's say you have 2 custom objects Estimate__c and Expense__c.And thses objects has a lookup relationship to Contract and so field API name is - Contract__c (llokup to Contract object) in bothe custom objects.

 

//  example to supply contractid as parent object

Estimate__c est = new Estimate__c(name='testName',...............,Contract__c = contractId);

insert est;

 

Expense__c exp = new Expense__c(name='testName',...............,Contract__c = contractId);

insert exp;

 

 

fatmonkfatmonk

Thank you,

when I tried I got an error. I am wrong some where:

For example:

1. I create an opportunity and create an Estimates__c ;

2. Create an contract under opportunity. 

I grab the opportunity id from contract

here is my code:

 

id conid = '80060000000A9F2AAK'; // for example 

Contract con = 

[Select c.Opportunity__r.Name, c.Opportunity__c, c.id,c.Status, c.AccountId,Opportunity__r.id From Contract c where id=: conid];
opid = Contracts[0].Opportunity__c;

 

// 

Estimate__c es = [Select c.RecordType.Name,c.Opportunity__r.Name, c.Opportunity__c, c.Name,c.Opportunity__r.id,
c.Contract__c , c.Id From Estimate__c where c.opportunity__c =:opid and c.Contract__c = null ];

Estimate__c e = new Estimate__c (contract__c [0]= con.id)

insert e;

 

error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You Must Enter A Estimate: []

 

 

Thanks L
Alok_NagarroAlok_Nagarro

Hi,

 

As per exception i guess there is a field (Estimate) in Estimate__c object and this field is having cutom Validation applied.

So where you trying to insert Estimate__c object's record, just also give some value to Estimate field also along with other field values. Since Exception indicates that this field value cannot be null.

 

Ex-

Estimate__c e = new Estimate__c (contract__c [0]= con.id,Estimate = some value)

insert e;

fatmonkfatmonk

Thanks so much for you time and your kindness.

I am still having a problem with my first Trigger:

Here is the scenario:

In an Opportunity I have create few Estimate__c records with name (but have not assigned to any Contract because there is no contract yet);

after Estimate__c records have created then contract is create. I write a trigger which will trigger after a new contract and query all Estimate__c records with contract.opportunity__c.id then insert contractnumber or contractid into the Estimate__c records to establish the relationship between to object records

 

here is my code but there is Error on illegal sObj:

 

trigger EstimateContract on Contract (after insert,after update) {
set<id> Opid= new set<id>();

for(contract c:Trigger.New){
if(c.Opportunity__c!=null){
Opid.add(c.Opportunity__c);

}
}
Estimate__c eToInsert =[select e.id,e.contract__c,e.name from Estimate__c e where e.Opportunity__c =:Opid];
// insert child contract (contract__c ) into estimate__c.contract__c
Estimate__c es = new Estimate__c();
es.contract__c = contract.id;
for(Estimate__c e:eToInsert) {
if(e.contract__c == null) inser es;

}

}

 

 

fatmonkfatmonk

Thanks so much for you time and your kindness.

I am still having a problem with my first Trigger:

Here is the scenario:

In an Opportunity I have create few Estimate__c records with name (but have not assigned to any Contract because there is no contract yet);

after Estimate__c records have created then contract is create. I write a trigger which will trigger after a new contract and query all Estimate__c records with contract.opportunity__c.id then insert contractnumber or contractid into the Estimate__c records to establish the relationship between to object records

 

here is my code but there is Error on illegal sObj:

 

trigger EstimateContract on Contract (after insert,after update) {
set<id> Opid= new set<id>();

for(contract c:Trigger.New){
if(c.Opportunity__c!=null){
Opid.add(c.Opportunity__c);

}
}

 

Estimate__c es = new Estimate__c();

// Compile Error: Illegal assignment from Schema.SObjectField to Id at line 13 column 4
es.contract__c = contract.id;

 

 

// query  Estimate__c.Contract__c  which share parent Opportunity id;  Estimate__c.Contract__c is a null 
Estimate__c eToInsert =[select e.id,e.contract__c,e.name from Estimate__c e where e.Opportunity__c =:Opid];

 

 

 

// insert child contract (contract__c ) into estimate__c.contract__c:

for(Estimate__c e:eToInsert) {
if(e.contract__c == null) insert es;

}

}

 thanks again

 

Alok_NagarroAlok_Nagarro

Hi,

 

As per as my understanding so far, try the following code -

 

trigger EstimateContract on Contract (after insert,after update)
{
    Map<id,id> Opid= new Map<id,id>();

    for(contract c:Trigger.New){
        if(c.Opportunity__c!=null){
            Opid.put(c.Opportunity__c,c.id);
        }
    }
    
    List<Estimate__c>  eToUpdate = new List<Estimate__c>();
    // query  Estimate__c.Contract__c  which share parent Opportunity id;  Estimate__c.Contract__c is a null
    for(Estimate__c e : [select id,contract__c,Opportunity__c,name from Estimate__c  where Opportunity__c IN:Opid.keySet()])
    {
      if(e.contract__c == null)
      {
           e.contract__c = Opid.get(e.Opportunity__c);
           eToUpdate.add(e);
      }
    }
    
    if(eToUpdate.size()>0)
        update eToUpdate;

}

This was selected as the best answer
fatmonkfatmonk

Thanks so much. It works!!!!!