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
SteveOBSteveOB 

System.DmlException:Insert failed.....

Hello,

 

I tested the following code in Sandbox and it worked perfectly fine. Granted that the sandbox environment tends to ignore some Failures and Warnings that would otherwise be flagged when deploying to production. Which is exactly what I think is happening now when I try to deploy this code. Any help or suggestions on what i might be missing in this code would be greatly appreciated.

 

Code is as follows:

*********************

public class BUG_RelatedBug
{
    public static testmethod void test1()
    {
        BUG_RelatedBug obj = new BUG_RelatedBug();
        
        SFDC_Bug__c objBug = new SFDC_Bug__c();
        objBug.Problem__c = 'testBug';
        insert objBug;
        objBug.Problem__c= 'test';
                   
        SFDC_Related_Bug__c objRelatedBug = new SFDC_Related_Bug__c();
        objRelatedBug.SFDC_Bug__c = objBug.Id;
        insert objRelatedBug;
        
        obj.allBugs = objBug.Id;
        obj.bugId = System.currentPageReference().getParameters().get('id');
        obj.SaveAll();
        obj.goBack();
    }
    
    public BUG_RelatedBug()
    {
       try
       {
           bugId = System.currentPageReference().getParameters().get('id');
           bugName = [SELECT Name FROM SFDC_Bug__c WHERE SFDC_Bug__c.Id=: bugId LIMIT 1].Name;
       }
       catch(Exception ex)
       {
           // DO NOTHING
       }
    }
    
    public PageReference saveAll()
    {
        list<String> SplitBugArr = allBugs.split(',');
        list<SFDC_Related_Bug__c> listRelatedBug = new list<SFDC_Related_Bug__c>();
        if(SplitBugArr.size() > 0)
        {
            for(Integer i = 0; i < SplitBugArr.size(); i++)
            {
                SFDC_Related_Bug__c ObjRelatedBug = new SFDC_Related_Bug__c();
                ObjRelatedBug.SFDC_Bug__c = bugId;
                ObjRelatedBug.SFDC_Bug_Num__c = SplitBugArr[i];                       
                listRelatedBug.add(ObjRelatedBug);
                
                SFDC_Related_Bug__c ObjRelatedBug2 = new SFDC_Related_Bug__c();
                ObjRelatedBug2.SFDC_Bug__c = SplitBugArr[i];
                ObjRelatedBug2.SFDC_Bug_Num__c = bugId;                       
                listRelatedBug.add(ObjRelatedBug2);
                
                SFDC_Bug__c[] UpdateBug = [SELECT Name FROM SFDC_Bug__c WHERE SFDC_Bug__c.Id=: bugId LIMIT 1];
                if (UpdateBug.size()>0)
                {UpdateBug[0].Status__c = 'Pending - BUG';
                update UpdateBug;
                }
                               
               
            }
            
        }
        if(listRelatedBug.size() > 0)
        {
            insert listRelatedBug;
        }
        return goBack();
    }
    
   public pageReference goBack()
   {
       PageReference pageRef = new PageReference('/'+bugId);
       pageRef.setRedirect(true);
       return pageRef;
   }
    
    
    
    public string bugId
    {
        set
        {
            bugId = value;
        }
        get
        {
            return bugId;
        }
    }
    public string bugName
    {
        set
        {
            bugName = value;
        }
        get
        {
            return bugName;
        }
    }
     public string allBugs
     {
        set
        {
           allBugs = value;
        }
        get
        {
           return allBugs;
        }

     }
     
}

****************

 

I get the following Error Message:

 

"System.DmlException:Insert Failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [SFDC_Bug__c]: [SFDC_Bug__c]"

 

 

Thanks in Advance.

Paul.FoxPaul.Fox

Do you have a field on SFDC_Related_Bug__c called SFDC_Bug__c?

 

If so, did you migrate that at the same time or before this code?

Pradeep_NavatarPradeep_Navatar

It seems that you have not migrated all the fields from the sandbox to the main org in which you are deploying the code. Compare the orgs carefully and migrate all the changes that are incoporated in the sandbox.

 

Hope this helps.

SteveOBSteveOB

Hi,

 

Yes there exists a field called "SFDC_Bug__c" on the SFDC_Related_Bug__c Custom Object. This field existed prior to the development of the code as well.

krishnagkrishnag

 

public class BUG_RelatedBug
{
    public static testmethod void test1()
    {
        BUG_RelatedBug obj = new BUG_RelatedBug();
        
        SFDC_Bug__c objBug = new SFDC_Bug__c();
        objBug.Problem__c = 'testBug';
        insert objBug;
        objBug.Problem__c= 'test';
                   
        SFDC_Related_Bug__c objRelatedBug = new SFDC_Related_Bug__c();
        objRelatedBug.SFDC_Bug__c = objBug.Id;
        insert objRelatedBug;
        
        obj.allBugs = objBug.Id;
        obj.bugId = System.currentPageReference().getParameters().get('id');
        obj.SaveAll();
        obj.goBack();
    }
    
    public BUG_RelatedBug()
    {
       try
       {
           bugId = System.currentPageReference().getParameters().get('id');
           bugName = [SELECT Name,SFDC_Bug__c FROM SFDC_Bug__c WHERE SFDC_Bug__c.Id=: bugId LIMIT 1].Name;
       }
       catch(Exception ex)
       {
           // DO NOTHING
       }
    }
    
    public PageReference saveAll()
    {
        list<String> SplitBugArr = allBugs.split(',');
        list<SFDC_Related_Bug__c> listRelatedBug = new list<SFDC_Related_Bug__c>();
        if(SplitBugArr.size() > 0)
        {
            for(Integer i = 0; i < SplitBugArr.size(); i++)
            {
                SFDC_Related_Bug__c ObjRelatedBug = new SFDC_Related_Bug__c();
                ObjRelatedBug.SFDC_Bug__c = bugId;
                ObjRelatedBug.SFDC_Bug_Num__c = SplitBugArr[i];                       
                listRelatedBug.add(ObjRelatedBug);
                
                SFDC_Related_Bug__c ObjRelatedBug2 = new SFDC_Related_Bug__c();
                ObjRelatedBug2.SFDC_Bug__c = SplitBugArr[i];
                ObjRelatedBug2.SFDC_Bug_Num__c = bugId;                       
                listRelatedBug.add(ObjRelatedBug2);
                
                SFDC_Bug__c[] UpdateBug = [SELECT Name,SFDC_Bug__c FROM SFDC_Bug__c WHERE SFDC_Bug__c.Id=: bugId LIMIT 1];
                if (UpdateBug.size()>0) 
                {UpdateBug[0].Status__c = 'Pending - BUG';
                update UpdateBug; 
                }
                               
               
            }
            
        }
        if(listRelatedBug.size() > 0)
        {
            insert listRelatedBug;
        }
        return goBack();
    }
    
   public pageReference goBack()
   {
       PageReference pageRef = new PageReference('/'+bugId);
       pageRef.setRedirect(true); 
       return pageRef; 
   }
    
    
    
    public string bugId
    {
        set
        {
            bugId = value;
        }
        get
        {
            return bugId; 
        }
    }
    public string bugName
    {
        set
        {
            bugName = value;
        }
        get
        {
            return bugName; 
        }
    }
     public string allBugs
     {
        set
        {
           allBugs = value;
        }
        get
        {
           return allBugs; 
        }

     }
     
}

 

aslo where did u created the test data for SFDC_Bug__c .I dont see there is one here in the code.Antways try this code first and let em know what errror its showing.

 

krishnagkrishnag

sorry try this code it should work.ignore the first one.

 

 

 

public class BUG_RelatedBug
{
    public static testmethod void test1()
    {
        BUG_RelatedBug obj = new BUG_RelatedBug();
        
        SFDC_Bug__c objBug = new SFDC_Bug__c();
        objBug.Problem__c = 'testBug';
        objBug.SFDC_Bug__c = 'abcd';
        insert objBug;
        objBug.Problem__c= 'test';
                   
        SFDC_Related_Bug__c objRelatedBug = new SFDC_Related_Bug__c();
        objRelatedBug.SFDC_Bug__c = objBug.Id;
        insert objRelatedBug;
        
        obj.allBugs = objBug.Id;
        obj.bugId = System.currentPageReference().getParameters().get('id');
        obj.SaveAll();
        obj.goBack();
    }
    
    public BUG_RelatedBug()
    {
       try
       {
           bugId = System.currentPageReference().getParameters().get('id');
           bugName = [SELECT Name,SFDC_Bug__c FROM SFDC_Bug__c WHERE SFDC_Bug__c.Id=: bugId LIMIT 1].Name;
       }
       catch(Exception ex)
       {
           // DO NOTHING
       }
    }
    
    public PageReference saveAll()
    {
        list<String> SplitBugArr = allBugs.split(',');
        list<SFDC_Related_Bug__c> listRelatedBug = new list<SFDC_Related_Bug__c>();
        if(SplitBugArr.size() > 0)
        {
            for(Integer i = 0; i < SplitBugArr.size(); i++)
            {
                SFDC_Related_Bug__c ObjRelatedBug = new SFDC_Related_Bug__c();
                ObjRelatedBug.SFDC_Bug__c = bugId;
                ObjRelatedBug.SFDC_Bug_Num__c = SplitBugArr[i];                       
                listRelatedBug.add(ObjRelatedBug);
                
                SFDC_Related_Bug__c ObjRelatedBug2 = new SFDC_Related_Bug__c();
                ObjRelatedBug2.SFDC_Bug__c = SplitBugArr[i];
                ObjRelatedBug2.SFDC_Bug_Num__c = bugId;                       
                listRelatedBug.add(ObjRelatedBug2);
                
                SFDC_Bug__c[] UpdateBug = [SELECT Name,SFDC_Bug__c FROM SFDC_Bug__c WHERE SFDC_Bug__c.Id=: bugId LIMIT 1];
                if (UpdateBug.size()>0) 
                {UpdateBug[0].Status__c = 'Pending - BUG';
                update UpdateBug; 
                }
                               
               
            }
            
        }
        if(listRelatedBug.size() > 0)
        {
            insert listRelatedBug;
        }
        return goBack();
    }
    
   public pageReference goBack()
   {
       PageReference pageRef = new PageReference('/'+bugId);
       pageRef.setRedirect(true); 
       return pageRef; 
   }
    
    
    
    public string bugId
    {
        set
        {
            bugId = value;
        }
        get
        {
            return bugId; 
        }
    }
    public string bugName
    {
        set
        {
            bugName = value;
        }
        get
        {
            return bugName; 
        }
    }
     public string allBugs
     {
        set
        {
           allBugs = value;
        }
        get
        {
           return allBugs; 
        }

     }
     
}

 

 

SteveOBSteveOB

Hi, thanks for the feedback but this code did not work. I see you made some minor changes by adding the "SFDC_Bug__c" field to the testmethod of the "SFDC_Bug__c" object and to the "bugName select option. But in fact, the problematic field is associated with the "SFDC_Related_Bug__c" custom object by the API name "SFDC_Bug__c." Let me know if this helps to clarify the issue I am having thanks.

krishnagkrishnag

By seeing the error message i came to conclusion that SFDC_Bug__c is a custom object and it is having a field SFDC_Bug__c which is a required field.

 

thats what i think correct me if iam wrong.

SteveOBSteveOB

I see. Yeah I would assume the same except there is no such field on the SFDC_Bug__c custom object so this puzzles me.

krishnagkrishnag

the only way is open the custom object SFDC_Bug__c and check whether there are any workflows running on it, Because there may be some field update which is making soem field required which is blocking the insert.IF u find any field like that insert that field also while inserting the object in the test class.