+ Start a Discussion
lamayclamayc 

many to many relationships

    I am having problems setting my many-to-many relationship. Right now I am using the enterprise.wsdl and writing in Java (I could  do it in Apex too, it really makes no difference). I have a custom object called Reject_Codes__c. On opportunity can have multiple reject codes, and likewise a reject code will link to many opportunities. I went into my reject object and created a lookup relationship to opportunity

    <fields>
        <fullName>Opportunity__c</fullName>
        <label>Opportunity</label>
        <referenceTo>Opportunity</referenceTo>
        <type>Lookup</type>
    </fields>

Child Relathionship Name: RejectCodes
Related List Label : Reject Codes

and on opportunity

    <fields>
        <fullName>RejectCodeLink__c</fullName>
        <label>Reject Code</label>
        <referenceTo>Reject_Codes__c</referenceTo>
        <relationshipLabel>Opportunities</relationshipLabel>
        <relationshipName>Opportunities</relationshipName>
        <type>Lookup</type>
    </fields>

Child Relathionship Name: Opportunities
Related List Label : Opportunities

to test it I am trying :

          Opportunity opp = ....
          Reject_Code_Maintenance__c obj = new Reject_Code_Maintenance__c();
          Reject_Code_Maintenance__c obj2 = new Reject_Code_Maintenance__c();
          QueryResult qr = new QueryResult();
          obj.setReject_Code__c("88");   // this is the external id -- note that first I tried using the salesforce Id using setId, but the api puked on this saying that Id wasn't indexed WTF?

          obj2.setReject_Code__c("79");
          SObject[] aa = new SObject[]{obj, obj2};
          qr.setRecords(aa);
          opp.setRejectCodes__r(qr);
          conn.update(new SObject[]{opp});

and I get the following :

 faultString: INVALID_FIELD: No such column 'RejectCodes__r' on entity 'opportunity'.

even though the enterprise.wsdl had the appropriate method (setRejectCodes__r(QueryResult qr))

What am I doing wrong please?

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
The fact that its called QueryResult should of been a hint, those fields exist for query only. You need to pass your obj,obj2 instances directly to the create call, having first set their Opportunity__c value.