-2

I am trying to build a after insert trigger on Contract object. Whenever user will create a Contract record trigger will generate/insert record of other object called Dim2. Dim2 have a lookup with Contract.

Here is my Trigger

trigger Contract2DIM2 on Contract (after insert) {
        if(Trigger.isInsert){
          for(Contract c : Trigger.New) {
            C2D2.insertDim2(c.ContractNumber);  
        }
}

Here is my class

public class C2D2 {
    public static void insertDim2(String Cnumber){
        try{
                c2g__codaDimension2__c a=new c2g__codaDimension2__c();
                a.Name=Cnumber;
                a.c2g__ReportingCode__c=Cnumber;
                a.Contract_Reference__c=Cnumber;
                insert a;             
        }catch(dmlexception e){
            system.debug('galti'+e.getMessage());
        }

    } 
}

It gives the following Error

4
  • 1
    What error are you getting. Can you be bit detail? Commented Oct 30, 2018 at 10:57
  • previous problem is solved but now I have another problem.I have another Lookup field on Contract called Dim2Reference now I want to add related Dim2 record to this field. ?? Commented Nov 6, 2018 at 10:01
  • Worth asking as a new question Commented Nov 6, 2018 at 10:06
  • The problem is I tried it Dim2Reference=dim2.id; but its not working because Contract record is already saved. Commented Nov 6, 2018 at 10:11

2 Answers 2

2

There are couple of things which I think are not perfect when it comes to writing on Salesforce Platform. I will point out the usual.

  1. Contact_Reference__c field on c2g__codaDimension2__c object is a lookup field . WHich means you can only put ID(15 or 18Digit valid SF record Id into it) As I can see you are inputting a number, which will give you exception
  2. Secondly Your Trigger code is not bulkified. If someone tried to use Bulk load using data loader your trigger will just get into 150DML limits. You have to bulkify your code.

    trigger Contract2DIM2 on Contract (after insert) {
        if(Trigger.isInsert){
    
            C2D2.insertDim2(Trigger.New);   //Bulkifying trigger by passing List to the Service method
        }
    }
    

    Service class code:

    public class C2D2 {
    public static void insertDim2(List<Contract> newList){
    
        List<c2g__codaDimension2__c> tobeInsertedDim2 = new List<c2g__codaDimension2__c>();
        for(Contract c : List<Contract> newList) {
            tobeInsertedDim2.add(new new c2g__codaDimension2__c(Name=c.ContractNumber ,c2g__ReportingCode__c =c.ContractNumber,
                Contract_Reference__c =c.Id )); //Notice Contact Reference I am setting as contractId
        }
        try{
            insert tobeInsertedDim2;         
        }catch(dmlexception e){
            system.debug('galti'+e.getMessage());
        }
    
    } 
    }
    
1
  • 1
    Thank you very much your solution is accurate.As you mentioned The problem was that I am assigning Number value to Look Up type which not appropriate.After assigning ID to lookup every thing is fine. Commented Nov 6, 2018 at 9:48
-3

Instead of passing a string into the class you should pass the contract object and then assign the id of the contract to the lookup field on the c2g__codaDimension2__c i.e. Contract_Reference__c

I have corrected your code here it should be

trigger Contract2DIM2 on Contract (after insert) {
        if(Trigger.isInsert){
          for(Contract c : Trigger.New) {
            C2D2.insertDim2(c);  
        }
}

public class C2D2 {
    public static void insertDim2(Contract ctrct){
        try{
                c2g__codaDimension2__c a=new c2g__codaDimension2__c();
                a.Name=ctrct.ContractNumber;
                a.c2g__ReportingCode__c=ctrct.ContractNumber;
                a.Contract_Reference__c=ctrct.ID;
                insert a;             
        }catch(dmlexception e){
            system.debug('galti'+e.getMessage());
        }
    } 
}

The problem is that the lookup field c2g__codaDimension2__c is expecting a "string" or "ID" but you are assigning it a number so you get a dml exception.

Please mark it as best answer if it helps you.

5
  • 2
    Please do not ask for best answer on Salesforce Stack Exchange. The community and the asker will provide that feedback where appropriate. While you're right about the immediate cause of this issue, the code you've posted still is running DML in a loop.
    – David Reed
    Commented Oct 30, 2018 at 11:34
  • My main concern in correcting code was to impress upon him the need to pass the proper salesforce ID and not make him follow best practices, that is a discussion for another time and post. Commented Oct 30, 2018 at 12:28
  • @user2246808 if you go to a doctor complaining of sore throat, and doctor checks and finds that you have fever as well. Should the doctor give you medicine just for sore throat and ignore fever which can be pretty bad if left untreated? Commented Oct 30, 2018 at 13:07
  • The patient is asking the doctor to look and tell him what ails him and not to teach him how to diagnose a disease. Commented Oct 30, 2018 at 13:20
  • Let's leave it here this is a platform for learning and not wit matching. Commented Oct 30, 2018 at 13:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .