1

I am trying to update a new Contract,

Using the following code:

[I have removed everything unnecessary,Just to make it more readable]

u = [SELECT Final_Email_Date__c, Email,FirstName, LastName, Company,Pass_Fail__c,OwnerID FROM Lead  WHERE User_Processed__c = False];
    public PageReference sendpassfail() {   
        Set<String> aSearchSet = new Set<String>();
        Set<String> tSearchSet = new Set<String> ();
        List<Lead> lList = u;
        for (Lead l : lList) {
          aSearchSet.add(l.company);
          tSearchSet.add(l.email);         
        }
        integer ii=1;

        Map<String,Account> companyToAccountMap = new Map<String,Account> ();
        for (Account a: [select id, Name from Account where name IN :aSearchSet])
        companyToAccountMap.put(a.name,a);


        Map<String,Contact> emailToContactMap   = new Map<String,Contact>();
        for (Contact c: [select id, FirstName, Email from Contact where Email IN :tSearchSet]){
        emailToContactMap.put(c.email,c);
        } 

        Set<String> pSearchSet = new Set<String>();
        Map<String,Trainee__c> emailToTraineeMap = new Map<String,Trainee__c>();
        for (Trainee__c tr:[select Email__c, Last_Name__c,First_Name__c,Institution__c,Volume_Serial_Number__c,Host_Name__c,Physical_Address__c from Trainee__c where email__c IN :tSearchSet]){
            emailToTraineeMap.put(tr.Email__c,tr);
            pSearchSet.add(tr.Physical_Address__c);
        }



        Set<Id> idSearchSet = new Set<Id> ();
        Map<String,Contact> emailDoubleCheckContactMap   = new Map<String,Contact>();
        for (Contact cdbl: [SELECT Email FROM Contact WHERE Id IN (SELECT CustomerSignedId FROM Contract WHERE Physical_Address__c IN :pSearchSet)]){
        emailDoubleCheckContactMap.put(cdbl.email,cdbl);
        idSearchSet.add(cdbl.Id);
        } 

        //To retrieve Contract based on Contact ID
        Map<String,Contract> ContactToContract = new Map<String,Contract>();
        for (Contract ctc: [Select CustomerSignedId, StartDate, Expiration_Date__c  From Contract WHERE CustomerSignedId IN :idSearchSet]) 
        ContactToContract.put(ctc.CustomerSignedId,ctc);



        Contract[] contractsTocreate = new Contract[]{};

        for (Lead inquery :lList){

        Contract ctr = new Contract(StartDate = myDate, Expiration_Date__c = enddate, CustomerSignedId = ContactToContract.get(emailToContactMap.get(inquery.email).ID).CustomerSignedId );
        contractsTocreate.add(ctr);

        }
        //Excute DML's

        update contractsTocreate;


     return null;
    }

When the code tries to update the "update contractsTocreate;"

i get an error "Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []"

But i am including the ID,

Can someone tell me why?

  • The variable name contractsToCreate is confusing. If you are trying to create that means insert and you should be using the insert statement. If you are updating you need to include the Id field and you might want to think about changing the variable name to contractsToUpdate. – Peter Knolle Dec 21 '13 at 21:16
2

I cannot see that you have populated the ID on this line

Contract ctr = new Contract(StartDate = myDate, Expiration_Date__c = enddate, CustomerSignedId = ContactToContract.get(emailToContactMap.get(inquery.email).ID).CustomerSignedId );

If you query the Id when you query for the Contracts and use that in the line above you should be fine.

All objects, standard and custom, has a field called ID - this is what is being used to locate the right records to update. To be able to use update in the code above you should populate this field.

  • in Contracts the CustomerSignedId is the Id! is that correct? – user2333346 Dec 21 '13 at 21:17
  • So i update this line for (Contract ctc: [Select Id, CustomerSignedId, StartDate, Expiration_Date__c From Contract WHERE CustomerSignedId IN :idSearchSet]) , But im still getting the same error, is this correct? – user2333346 Dec 21 '13 at 21:20
  • Add Id = ContactToContract.get(emailToContactMap.get(inquery.email).ID).Id to the contract that you update as well. – haagen Dec 21 '13 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.