• KSKumaar
  • NEWBIE
  • 10 Points
  • Member since 2016
  • Salesforce developer

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
I am very new to SOAP API integration in salesforce.

I am unable to create related records having parent-child relationships. To get some awareness about this process, i gone through Force.com SOAP API Sample WSDL Structures page, here i got some information about Creating related records having a parent-child relationship using the create() API.

But this XML contains <Siebel_Id__c>1-HR68E</Siebel_Id__c> tag which is not understandable by me. Full XML is given below:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com" xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header>
    <urn:SessionHeader>
      <urn:sessionId>[sessionId retrieved from the login() call]</urn:sessionId>
    </urn:SessionHeader>
  </soapenv:Header>
  <soapenv:Body>
    <urn:create>
      <urn:sObjects xsi:type="urn1:Account">
        <Name>Sample Account Two</Name>
        <!-- Siebel_Id__c is the External Id field present on Account object -->
        <Siebel_Id__c>1-HR68E</Siebel_Id__c>
      </urn:sObjects>
      <urn:sObjects xsi:type="urn1:Contact">
        <FirstName>Anupam</FirstName>
        <LastName>Rastogi</LastName>
        <urn1:Account>
          <Siebel_Id__c>1-HR68E</Siebel_Id__c>
        </urn1:Account>
      </urn:sObjects>
    </urn:create>
  </soapenv:Body>
</soapenv:Envelope>

This XML is saying Siebel_Id__c is the External Id field present on Account object.
What is External Id field in Account? How can I use this field in XML?
I wrote a Rest api class and it is given below.
 
@RestResource(urlMapping='/threeobjects/*')
Global class MyRest_ThreeObjects_Getting {

    @HttpGet
    global static List<MultiWrapper> doGet(){
        List<MultiWrapper> listmw = new List<MultiWrapper>();
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        Map<Id, User> contactToUser = new Map<Id, User>();
        for (User u : [select Id, FirstName, LastName, UserName, Email,ContactId from User where ContactId != null limit 1] ) {
            contactToUser.put(u.ContactId, u);
        }
        for (Contact c : [select Id, AccountId, Lastname, Firstname, Email, Account.Id, Account.Name, Account.Phone from Contact where Id in :contactToUser.keySet()]) {
            listmw.add(new MultiWrapper(contactToUser.get(c.Id), c.Account, c));
        }
        return listmw;
    }
    Global class MultiWrapper {
       User us;
       Account acc;
       Contact con;
       Public MultiWrapper (User us, Account acc, Contact con){
           this.us = us;
           this.acc = acc;
           this.con = con;
       }
    }
    @HttpPut
    global static String doPut(){
        List<MultiWrapper> result = doGet();
        String jsonstring = json.serialize(result);
        User us;
        Account acc;
        Contact con;
        list<MultiWrapper> MultiWrapperList = (list<MultiWrapper>)JSON.deserialize(jsonstring,list<MultiWrapper>.class);
        for(MultiWrapper mm : MultiWrapperList){
           us = mm.us;
           acc = mm.acc;
           con = mm.con;
        }
        System.debug('Before queryl:::'+us);
        us = [select Alias from User where id=:us.Id];
        us.Alias = 'Exam';
        update us; 
        System.debug('After query:::'+us);
        return 'User successfully updated';
    }
}

And i wrote test class above class and this test class is covering for get method and giving exception in put method i.e,
System.NullPointerException: Attempt to de-reference a null object

Test class:
 
@IsTest
Private class Test_ThreeObjects_Getting {
    static testMethod void testDoGet() {
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        Account acc = new Account(Name='aa');
        insert acc;
        Contact con = new Contact(LastName='con aa', AccountId=acc.id);
        insert con;
        User u = new User(Lastname = 'Test Name',Alias='some',Email='some@soem.com',
Username='some@some.in',CommunityNickname='some',ProfileId='00e28000000OBjS',
                          ContactID = con.ID,TimeZoneSidKey='America/New_York',LocaleSidKey='en_US',
                          EmailEncodingKey='ISO-8859-1',LanguageLocaleKey='en_US');
        insert u;

        req.requestURI='/services/apexrest/threeobjects';
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;

        MyRest_ThreeObjects_Getting.MultiWrapper  mulWrap = new MyRest_ThreeObjects_Getting.MultiWrapper(u,acc,con);
        List<MyRest_ThreeObjects_Getting.MultiWrapper> results = MyRest_ThreeObjects_Getting.doGet();
    }
    static testMethod void testDoPut() {

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();

        req.requestURI='/services/apexrest/threeobjects';
        req.httpMethod = 'PUT';
        RestContext.request = req;
        RestContext.response = res;
        User us = [select Alias from User where id='00528000004fOcq'];
        us.Alias = 'some';
        update us;
        String results1 = MyRest_ThreeObjects_Getting.doPut(); //Giving error here
        System.assertEquals(results1,'User successfully updated');
    }
}
Could anyone please tell me why i am facing this exception and please help me to solve this.

Thanking you
 
I found the following details from this https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_xml_dom.htm

As in the page shows, following one is XML file.
<address>
    <name>Kirk Stevens</name>
    <street1>808 State St</street1>
    <street2>Apt. 2</street2>
    <city>Palookaville</city>
    <state>PA</state>
    <country>USA</country>
</address>
The following one is DomDocument class.
public class DomDocument {

    // Pass in the URL for the request
    // For the purposes of this sample,assume that the URL
    // returns the XML shown above in the response body
    public void parseResponseDom(String url){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        // url that returns the XML in the response body
        req.setEndpoint(url);
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        Dom.Document doc = res.getBodyDocument();

        //Retrieve the root element for this document.
        Dom.XMLNode address = doc.getRootElement();

        String name = address.getChildElement('name', null).getText();
        String state = address.getChildElement('state', null).getText();
        // print out specific elements
        System.debug('Name: ' + name);
        System.debug('State: ' + state);

        // Alternatively, loop through the child elements.
        // This prints out all the elements of the address
        for(Dom.XMLNode child : address.getChildElements()) {
           System.debug(child.getText());
        }
    }
}
I understood the above class except the two things which listed below. can you please how to achieve them?

a> How to pass the XML file as a url later as a parameter to parseResponseDom() in above class?
b> String name = address.getChildElement('name', null).getText(); In this line, in the place of namespace why we are giving null value in getChildElement(name,namespace)?
Help is appreciated.
Thank you
 
User-added image

As in the above pic shows, I don't want to display the Standard error message which i rounded in pic. How can i make disable it? I want to display only the message what we used generally in 
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Error Message'));

I can make this possible by using if - else condition (Please see the code which provided), but i want get it through using try - catch blocks only. And also please tell me that how we are getting Required fields are missing: [Account Name] message. Is that system validation rule or any other error message?
You can also see the code which i have used.
public void comittedResult() {
        try{
        aOne=[select id,name,phone,Industry from Account where ID=:accid];
       /* if (String.isBlank(accName)){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter account name'));
        }//!(accPhone.isNumeric())
        else if(!(pattern.matches('[0-9]+',accPhone))){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter correct phone number'));
        }
        else  {*/
        aOne.name = accName;
        aOne.phone = accPhone;
        update aOne;
        displayPopup = false;
        reflectRecords();
   //   }
     }catch(DMLException e){
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter Name'));
     }
     }

Thanks in advance.
KS Kumaar
Hi:
Since the Winter 19 release, I am getting the above error when I run any unit test that modified a user record.

We are using the User License type: Overage Authenticated Website

I cannot locate the permission "Edit Self-Service Users" I turned off the enhanced user management to look for it and also tried giving my Users a permission set with all permissions under Users section.

Doing some research on previous posts about the  "Edit Self-Service Users" that what was recommended.

Thanks in advance,
I am very new to SOAP API integration in salesforce.

I am unable to create related records having parent-child relationships. To get some awareness about this process, i gone through Force.com SOAP API Sample WSDL Structures page, here i got some information about Creating related records having a parent-child relationship using the create() API.

But this XML contains <Siebel_Id__c>1-HR68E</Siebel_Id__c> tag which is not understandable by me. Full XML is given below:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com" xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header>
    <urn:SessionHeader>
      <urn:sessionId>[sessionId retrieved from the login() call]</urn:sessionId>
    </urn:SessionHeader>
  </soapenv:Header>
  <soapenv:Body>
    <urn:create>
      <urn:sObjects xsi:type="urn1:Account">
        <Name>Sample Account Two</Name>
        <!-- Siebel_Id__c is the External Id field present on Account object -->
        <Siebel_Id__c>1-HR68E</Siebel_Id__c>
      </urn:sObjects>
      <urn:sObjects xsi:type="urn1:Contact">
        <FirstName>Anupam</FirstName>
        <LastName>Rastogi</LastName>
        <urn1:Account>
          <Siebel_Id__c>1-HR68E</Siebel_Id__c>
        </urn1:Account>
      </urn:sObjects>
    </urn:create>
  </soapenv:Body>
</soapenv:Envelope>

This XML is saying Siebel_Id__c is the External Id field present on Account object.
What is External Id field in Account? How can I use this field in XML?
I wrote a Rest api class and it is given below.
 
@RestResource(urlMapping='/threeobjects/*')
Global class MyRest_ThreeObjects_Getting {

    @HttpGet
    global static List<MultiWrapper> doGet(){
        List<MultiWrapper> listmw = new List<MultiWrapper>();
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        Map<Id, User> contactToUser = new Map<Id, User>();
        for (User u : [select Id, FirstName, LastName, UserName, Email,ContactId from User where ContactId != null limit 1] ) {
            contactToUser.put(u.ContactId, u);
        }
        for (Contact c : [select Id, AccountId, Lastname, Firstname, Email, Account.Id, Account.Name, Account.Phone from Contact where Id in :contactToUser.keySet()]) {
            listmw.add(new MultiWrapper(contactToUser.get(c.Id), c.Account, c));
        }
        return listmw;
    }
    Global class MultiWrapper {
       User us;
       Account acc;
       Contact con;
       Public MultiWrapper (User us, Account acc, Contact con){
           this.us = us;
           this.acc = acc;
           this.con = con;
       }
    }
    @HttpPut
    global static String doPut(){
        List<MultiWrapper> result = doGet();
        String jsonstring = json.serialize(result);
        User us;
        Account acc;
        Contact con;
        list<MultiWrapper> MultiWrapperList = (list<MultiWrapper>)JSON.deserialize(jsonstring,list<MultiWrapper>.class);
        for(MultiWrapper mm : MultiWrapperList){
           us = mm.us;
           acc = mm.acc;
           con = mm.con;
        }
        System.debug('Before queryl:::'+us);
        us = [select Alias from User where id=:us.Id];
        us.Alias = 'Exam';
        update us; 
        System.debug('After query:::'+us);
        return 'User successfully updated';
    }
}

And i wrote test class above class and this test class is covering for get method and giving exception in put method i.e,
System.NullPointerException: Attempt to de-reference a null object

Test class:
 
@IsTest
Private class Test_ThreeObjects_Getting {
    static testMethod void testDoGet() {
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        Account acc = new Account(Name='aa');
        insert acc;
        Contact con = new Contact(LastName='con aa', AccountId=acc.id);
        insert con;
        User u = new User(Lastname = 'Test Name',Alias='some',Email='some@soem.com',
Username='some@some.in',CommunityNickname='some',ProfileId='00e28000000OBjS',
                          ContactID = con.ID,TimeZoneSidKey='America/New_York',LocaleSidKey='en_US',
                          EmailEncodingKey='ISO-8859-1',LanguageLocaleKey='en_US');
        insert u;

        req.requestURI='/services/apexrest/threeobjects';
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;

        MyRest_ThreeObjects_Getting.MultiWrapper  mulWrap = new MyRest_ThreeObjects_Getting.MultiWrapper(u,acc,con);
        List<MyRest_ThreeObjects_Getting.MultiWrapper> results = MyRest_ThreeObjects_Getting.doGet();
    }
    static testMethod void testDoPut() {

        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();

        req.requestURI='/services/apexrest/threeobjects';
        req.httpMethod = 'PUT';
        RestContext.request = req;
        RestContext.response = res;
        User us = [select Alias from User where id='00528000004fOcq'];
        us.Alias = 'some';
        update us;
        String results1 = MyRest_ThreeObjects_Getting.doPut(); //Giving error here
        System.assertEquals(results1,'User successfully updated');
    }
}
Could anyone please tell me why i am facing this exception and please help me to solve this.

Thanking you