Select your Language

Create Rest API in Salesforce and used by third party(postman)

 Step 1: Create a rest API apex class in salesforce

@RestResource(urlmapping='/RestAccountUpdate/*')

Global class RestApi_AccountUpdate {

    @httpGet

    Global static string UpdateAccount()

    {      

        restRequest req= restcontext.request;

        restResponse res= restcontext.response;

        try{

            String accName = req.params.get('name');//access value from the url

            account a=[select id , name, rating from account where name=:accName];

            system.debug('acc list: '+a);

            a.Rating='cold';

            a.AccountNumber='wilson'+accname;

            update a;

            return a.name+' Successfully Updated';

        }

        catch (exception e)

        {

            return 'Updation failed due to :'+e.getMessage();

        }

    }

    @httpPost

    Global static string updateAcc(string Name, string Rating)// access value from API Body from third party

    {

        try{

            account a=[select id , name, rating from account where name=:Name];

            a.Rating=rating;

            update a;

            return 'Successfully Updated';

        }

        Catch(Exception e)

        {

            string str='Failed reason: '+ e.getMessage();

            return str;

        }

    }

// for bulk record update

 @httpPut

    global static string putmethod(list<account>acclist)

        

    {

        

            database.SaveResult[] accresult= database.update(acclist,false);

        string str='';

        for(database.SaveResult s: accresult)

        {

            if(s.IsSuccess())

            {

                str= str+ ' successfully update '+s.getId()+'\n';

            }

            list<database.Error>accError= s.getErrors();

            for(database.Error er: accError)

            {

                str= str+ ' Updation failed  '+er.getMessage()+ '  '+er.fields ;

            }

        }

        return str;

        

    }

}

=========================================================================

ThirdParty uses: (Postman)

1. Using Get method: in this method we can pass the value in the URL.

Ex: name=Max








2. Using Post method: In this we can pass the value in the body







3 . Using Put method : update bulk record using id




No comments:

Post a Comment