+ Start a Discussion
wt35wt35 

INVALID_FIELD_FOR_INSERT_UPDATE bad field names on insert/update call: type: [type]

Hi community,

 

I get the here-above message when trying to update opportunities.


When looking at the API guide, it seems to be specific to Person Accounts errors but I do not have Person Accounts enabled.

I could also see other threads but did not get any help for my specific scenario.

 

Debug Log lines:

 

 

15:41:54.093 (93278000)|DML_BEGIN|[42]|Op:Update|Type:Opportunity|Rows:1
15:41:54.126 (126267000)|DML_END|[42]
15:41:54.126 (126412000)|EXCEPTION_THROWN|[42]|System.DmlException: Update failed. First exception on row 0 with id 006S00000070MRG; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Opportunity: bad field names on insert/update call: type: [type]
15:41:54.133 (133797000)|POP_TRACE_FLAGS|[EXTERNAL]|01pS0000000EZwQ|ClosedDuplicate|
15:41:54.204 (133826000)|CUMULATIVE_LIMIT_USAGE
15:41:54.204|LIMIT_USAGE_FOR_NS|(default)

 

Apex Class:

 

global class ClosedDuplicate{

   webService static String myMethod(Opportunity contextOpp) {
       
        List<Opportunity> relevantOpps;
        String finalMessage;
        Profile userProfile = [SELECT Name FROM Profile WHERE Id = :Userinfo.getProfileId()];


        /*********************************************************/
        /*****************AUTHORIZED USER ************************/
        /*********************************************************/
        if(contextOpp.Owner.Name == 'NA BS User') {}
        
        if ( /*  (userProfile.Name.equals('NA Telesales Rep') ||
                userProfile.Name.equals('Na Telesales Manager') ||
                userProfile.Name.equals('NA Territory Manager - Outbound')) &&
                contextOpp.Owner.Name == 'NA BS User' */ true )
        {
            List<Opportunity> oppsToUpdate = new List<Opportunity>();
            oppsToUpdate.add(contextOpp);
            try{
            relevantOpps = [SELECT Id,StageName,Reason_For_Closed_Lost__c FROM Opportunity WHERE 
                AccountId = :contextOpp.AccountId AND
                IsClosed = false AND
                Owner.Name like 'NA BS User' AND
                Product_Target__c INCLUDES ('Advanced','ECS','Pro')];
            oppsToUpdate.addAll(relevantOpps);
            } catch(Exception e){
            finalMessage = 'No relevant opportunities have been closed because either none were found or an error has happened.';
            }

            if(oppsToUpdate.size() > 0)
            {
                for (Opportunity o : oppsToUpdate)
                {
                 o.StageName = 'Closed Lost';
                 o.Reason_For_Closed_Lost__c = 'Close - TS Duplicate';
                }
                try{
                update oppsToUpdate;
                finalMessage = oppsToUpdate.size() + ' opportunities have been closed';
                } catch(Exception e){
                finalMessage = 'No relevant opportunities have been closed';
                }
            }
            
        } 
           


        /*********************************************************/
        /********************NON-AUTHORIZED USER *****************/
        /*********************************************************/
        else{            
            finalMessage = 'No relevant opportunities have been closed because you do not have an authorized profile or the opportunity is not owned by NA BS User.';
        }


     return finalMessage;
    }

}

 

 

Thanks!!

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
wt35wt35

SO I finally found a solution, as mentioned in my previous post I managed to remove the "type" field in the record by keeping the id, calling the clear() method, and then re-assign the Id to the record.

 

That way I got a clear record withouth unnecessary firlds:

 

            Id contextOppId = contextOpp.Id;
            contextOpp.clear();
            contextOpp.Id = contextOppId;

 

All Answers

SaraagSaraag

I suspect contextOpp object is passed in without salesforce ID. If the caller queries for opportunity record and passes that as argument- contextOpp, make sure they are including Id in their query to retrieve it.

 

Saraag

Satyendra RawatSatyendra Rawat

Hi,

 

Make sure updated field is updatable, and current user have access current field and also check the picklist value.

wt35wt35

@Satya @Saraag Thanks for the hints, I will check them. However I discovered something odd and I think it is related to that:

 

In the debug log, I noticed that the oppportunity record I am updating contains all the necessary fields , plus an additional field called "type", which I do not want/need. I think that this field is automatically included because I am providing this record via a JavaScript call in a custom button:

 

var opp = new sforce.SObject("Opportunity");
opp.id = "{!Opportunity.Id}";

var myString = sforce.apex.execute("ClosedDuplicate","myMethod",{contextOpp:opp}); 

 

So the challenge is, how to get rid of that "type" field in the record.

wt35wt35

 

oppsToUpdate in Debug Log:

 

07:22:44.140 (140735000)|USER_DEBUG|[41]|DEBUG|###########oppsToUpdate (Opportunity:{StageName=Closed Lost, Reason_For_Closed_Lost__c=Close - TS Duplicate, Id=006S00000070MRG, type=Opportunity}

 

wt35wt35

SO I finally found a solution, as mentioned in my previous post I managed to remove the "type" field in the record by keeping the id, calling the clear() method, and then re-assign the Id to the record.

 

That way I got a clear record withouth unnecessary firlds:

 

            Id contextOppId = contextOpp.Id;
            contextOpp.clear();
            contextOpp.Id = contextOppId;

 

This was selected as the best answer