• veerna551.3942206611176638E12
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
public with sharing class AccAndCons1 {
public list<contactWrapper> Wrappers{get;set;}
private Integer nextIdent=0;
public static Integer addCount {get; set;}
public  Account acc{get;set;}
public string name{get;set;}

public list<contact> cons=new list<contact>();
    public AccAndCons1(ApexPages.StandardController controller) {
    acc=(Account)controller.getRecord();
    
        wrappers=new List<contactWrapper>();
       
        for (Integer idx=0; idx<2; idx++)
      {
        wrappers.add(new ContactWrapper(nextIdent++));
       
      }
    }
   
    public void addRows()
   {
     for (Integer idx=0; idx<addCount; idx++)
   {
      wrappers.add(new ContactWrapper(nextIdent++));
   }
}
public PageReference save1()
{
 
  acc.name=name;
  // database.saveResult srres= database.insert(acc);
  insert acc;
   list<contact> cc=new list<contact>();
  // if(srres.isSuccess()){
  
   
for (ContactWrapper wrap : wrappers)
  {
 
   cons.add(wrap.con);
  
   
    
   }
  
   for(contact c:cons){
   c.AccountId=acc.id;
   cc.add(c);
   }
//} 
  

  upsert cc;

  return null;
  }
public class contactWrapper{
public integer iden{get;set;}
public Contact con{get;set;}
public id AccountId{get;set;}
  public contactWrapper(integer idenent){
    iden=idenent;
    con=new contact();
  }
}
}


test class:

@isTest
public class AccAndCons1_test{
 
   static testMethod void AccAndCons1_testOne(){
  
     Account a=new Account(name='xyz');
     upsert a;
    
     List<contact> con =new List<contact>();
     contact c1=new contact(lastname='sas',AccountId=a.id);
     contact c2=new contact(lastname='sas1',AccountId=a.id);
     con.add(c1);
     con.add(c2);
     upsert con;
     ApexPages.StandardController sc = new ApexPages.StandardController(a);
     AccAndCons1 aa=new AccAndCons1 (sc);
     aa.addRows();
     aa.save1();
    
     }
   }

when i run the test class it shows the error like:

System.DmlException: Insert failed. First exception on row 0 with id 0019000000xjtULAAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Stack Trace :Class.AccAndCons1.save1: line 33, column 1
Class.AccAndCons1_test.AccAndCons1_testOne: line 18, column 1


to insert multiple contacts for Account Simutaneously.
when i click save button it shows this error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name]: [Last Name]
Error is in expression '{!save}' in component <apex:commandButton> in page baskartask2

Class.AccAndCons1.save: line 41, column 1

<apex:page standardController="Account" extensions="AccAndCons1">
  <apex:form >
   <apex:pageBlock >
    <apex:pageBlockSection title="AccountInfo" columns="1">
     <apex:inputText value="{!Account.name}" label="Name"  />
    </apex:pageBlockSection>
    <apex:pageBlockSection title="conatctInfo" columns="2" >
    <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtTable">
    <apex:column headerValue="S.NO">
    <apex:outputText value="{!wrapper.iden}"/>
    </apex:column>
     <apex:column headerValue="FirstName">
            <apex:inputField value="{!wrapper.con.FirstName}"/>
         </apex:column>
         <apex:column headerValue="LastName">
            <apex:inputField value="{!wrapper.con.FirstName}"/>
         </apex:column>
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    <br></br>
    <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtTable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/>
      </apex:commandButton>
      <apex:commandButton value="Save" action="{!save}"/>
  
  </apex:pageBlock>
</apex:form>
</apex:page>





public with sharing class AccAndCons1 {
public list<contactWrapper> Wrappers{get;set;}
private Integer nextIdent=0;
public static Integer addCount {get; set;}
public  Account acc{get;set;}
public string name{get;set;}
public id accId{get;set;}
public list<contact> cons{get;set;}
    public AccAndCons1(ApexPages.StandardController controller) {
    this.accId=Apexpages.currentPage().getParameters().get('id');
    acc=(Account)controller.getRecord();
        wrappers=new List<contactWrapper>();
         cons=new List<contact>();
        for (Integer idx=0; idx<2; idx++)
      {
        wrappers.add(new ContactWrapper(nextIdent++));
       
      }
    }
   
    public void addRows()
   {
     for (Integer idx=0; idx<addCount; idx++)
   {
      wrappers.add(new ContactWrapper(nextIdent++));
   }
}
public PageReference save()
{


for (ContactWrapper wrap : wrappers)
  {
   wrap.AccountID=accId;
   cons.add(wrap.con);
   for(contact c:cons){
    system.debug(c.firstname);
    system.debug(c.lastname);
    }
  }
  insert cons;

  return null;
  }
public class contactWrapper{
public integer iden{get;set;}
public Contact con{get;set;}
public id AccountId{get;set;}
  public contactWrapper(integer idenent){
    iden=idenent;
    con=new contact();
  }
}
}
System.DmlException: Insert failed. First exception on row 0 with id 0019000000xjDWMAA2; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Class.AccAndCons.save: line 37, column 1 Class.AccAndCons_Test.AccAndCons_testMethod: line 22, column 1

apex code:
public with sharing class AccAndCons {
public account acc{get;set;}

    public AccAndCons(ApexPages.StandardController controller) {
    this.acc=(Account)controller.getRecord();
     index = new List<integer>();
          listCon  = new List<contact>();
          for(integer i=0;i<=1;i++){
           index.add(i);
          listCon.add(new Contact());

    }
    }


    public List<integer> index { get; set; }

    public List<contact> listCon { get; set; }
  
   public PageReference cancel1(){
 
   PageReference pg =new PageReference(' https://ap1.salesforce.com/001/o');

           pg.setredirect(true);

           return pg;

   }
 
 


    public PageReference save() {
    list<contact> cc=new list<contact>();
 
    acc.name=name;
    Database.SaveResult srList = Database.insert(acc);
    if(srList.isSuccess()){
   
     for(contact c:listCon ){
   
       c.AccountId=acc.id;
       cc.add(c);
     }
     
        insert cc;
       }
      
   
   
  
  
 
  
        return null;
    }


  

    public String name { get; set; }
}


test class:

@isTest
public class AccAndCons_Test{

    static testMethod void AccAndCons_testMethod(){
  
   
     Account a = new Account(name='chows');
  
     insert a;
     System.assertEquals(a.name,'chows');
     list<contact> con=new list<contact>();
     contact c1=new contact(lastName='test1',AccountID=a.id);
     contact c2=new contact(lastName='test2',AccountID=a.id);
     con.add(c1);
     con.add(c2);
    insert con;
     ApexPages.StandardController sc = new ApexPages.StandardController(a);
      
   
     AccAndCons aa=new AccAndCons(sc);
   
     aa.save();
  
     aa.cancel1();
    }


}
public with sharing class AccAndCons1 {
public list<contactWrapper> Wrappers{get;set;}
private Integer nextIdent=0;
public static Integer addCount {get; set;}
public  Account acc{get;set;}
public string name{get;set;}

public list<contact> cons=new list<contact>();
    public AccAndCons1(ApexPages.StandardController controller) {
    acc=(Account)controller.getRecord();
    
        wrappers=new List<contactWrapper>();
       
        for (Integer idx=0; idx<2; idx++)
      {
        wrappers.add(new ContactWrapper(nextIdent++));
       
      }
    }
   
    public void addRows()
   {
     for (Integer idx=0; idx<addCount; idx++)
   {
      wrappers.add(new ContactWrapper(nextIdent++));
   }
}
public PageReference save1()
{
 
  acc.name=name;
  // database.saveResult srres= database.insert(acc);
  insert acc;
   list<contact> cc=new list<contact>();
  // if(srres.isSuccess()){
  
   
for (ContactWrapper wrap : wrappers)
  {
 
   cons.add(wrap.con);
  
   
    
   }
  
   for(contact c:cons){
   c.AccountId=acc.id;
   cc.add(c);
   }
//} 
  

  upsert cc;

  return null;
  }
public class contactWrapper{
public integer iden{get;set;}
public Contact con{get;set;}
public id AccountId{get;set;}
  public contactWrapper(integer idenent){
    iden=idenent;
    con=new contact();
  }
}
}


test class:

@isTest
public class AccAndCons1_test{
 
   static testMethod void AccAndCons1_testOne(){
  
     Account a=new Account(name='xyz');
     upsert a;
    
     List<contact> con =new List<contact>();
     contact c1=new contact(lastname='sas',AccountId=a.id);
     contact c2=new contact(lastname='sas1',AccountId=a.id);
     con.add(c1);
     con.add(c2);
     upsert con;
     ApexPages.StandardController sc = new ApexPages.StandardController(a);
     AccAndCons1 aa=new AccAndCons1 (sc);
     aa.addRows();
     aa.save1();
    
     }
   }

when i run the test class it shows the error like:

System.DmlException: Insert failed. First exception on row 0 with id 0019000000xjtULAAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Stack Trace :Class.AccAndCons1.save1: line 33, column 1
Class.AccAndCons1_test.AccAndCons1_testOne: line 18, column 1


Hi,

I have 4 fields First Name, Last Name, Destination City, Destination State. These 4 columns have some duplicate values. I want to remove those Duplicate rows and these duplicate rows have different "Created Date".   have to delete only old dated rows. can someone help me. These records are already in database.

Lead IDCreate DateFirst NameLast NameDestination CityDestination State
00QU0000006ideK3/23/2008danramirez TX
00QU0000006idQI3/26/2008ErinJeffersReadingPA
00QU0000006igC03/25/2008ErinJeffersReadingPA

 

Thanks,
Uday

  • December 14, 2012
  • Like
  • 0